LoginSignup
4
5

More than 5 years have passed since last update.

tigとgit(初心者、初学者向け)

Last updated at Posted at 2018-06-17

はじめに

tigとgitの導入から使い方までを大まかにまとめました。
tigなんかは案外知らない人も多かったりするので、激推ししておきます。
githubについても書きたかったのですが、長くなってしまったのでまた今度にします。

環境

MacOS HighSierra 10.13.4
Homebrew 1.6.8-25-g9b21422
git version 2.17.1

gitって何?

プロジェクト管理ツールの事です。
git hogeの各コマンドを駆使してプロジェクトを管理します。
何が便利ってプロジェクトの開発や修正をしている時に、"ここ直したらなんかバグった!"、"一旦戻りたい!"的な事をいい感じに管理する事が出来ます。
また、複数人でのプロジェクト管理で誰がどこを書いているのかというのも確認出来るので、とてもとても便利です。
素のgitですとlocalでのリポジトリ管理になります。githubやらなんやらのリモートリポジトリと組み合わせて使うことが主になります。

git導入

※homebrewがinstallされている前提で進めます

  • brewでinstall
$ brew install git
Updating Homebrew...
==> Auto-updated Homebrew!
Updated Homebrew from 9b21422b7 to 9831e6745.
Updated 3 taps (heroku/brew, homebrew/core, homebrew/cask).
==> New Formulae
openapi-generator
==> Updated Formulae
awscli ✔                   docker-machine             libheif                    rpm
heroku/brew/heroku ✔       draco                      libphonenumber             saltstack
annie                      etcd                       memcacheq                  simutrans
berkeley-db                ffmpeg                     mitmproxy                  spotbugs
bitcoin                    fluent-bit                 moc                        srt
blackbox                   geckodriver                nvi                        stern
bogofilter                 gnu-cobol                  octave                     traefik
clojurescript              grails                     offlineimap                twarc
cppcheck                   hadolint                   open-cobol                 webalizer
crystal-lang               jack                       packmol                    wpscan
davix                      jenkins-job-builder        pkcs11-helper              wtf
dbxml                      jigdo                      planck                     xmake
dcm2niix                   kubeless                   pony-stable                xonsh

==> Downloading https://homebrew.bintray.com/bottles/git-2.17.1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring git-2.17.1.high_sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
  /usr/local/share/zsh/site-functions

Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/git
==> Summary
🍺  /usr/local/Cellar/git/2.17.1: 1,501 files, 35.6MB
  • このままだとMacDefaultのGitを使用してますので、使用するgitを指定してあげます。
$ sudo vim /etc/paths #管理者権限でpathの書き換えを行う。
Password:?
  • vimで開いて最上部に/usr/local/binを記述してください。
  • ターミナルを再起動すると反映されます。
$ which git #whichコマンドでどこのgitを見ているかを確認。
/usr/local/bin/git
$ git version
git version 2.17.1
  • 最新版がインストールされたことを確認出来ます。
  • git configでメールアドレスと名前を登録してください。
$ git config --global user.name "名前"
$ git config --global user.email メールアドレス

git使い方

  • git管理したいディレクトリに移動し、git initコマンドで使用することが出来ます。
  • ls -aで.gitが出来ている事が確認出来ます。
$ git init
Initialized empty Git repository in /Users/tashirosota/Desktop/workspace/test_dir/.git/
$ ls -a
.   ..  .git
  • .gitignoreを作ることによりgitで管理しないファイルを指定することが出来ます。
$ vim .gitignore
 .DS_Store  
  • Macですと.DS_Storeは排除しておくと良いでしょう。

gitコマンドに関して

※よく使うものだけ

  • git add 変更ファイル名
    • 変更したファイルをステージング環境に追加します。
    • git add -a
    • tigの方が楽なので細かな説明は省略します。
  • git commit
    • ステージング環境に追加してあるファイルを保存します。
    • git commit後にはvimに移りますので保存名を記入してください。
    • tigの方が楽なので細か
  • git status
    • ファイル変更状況を確認出来ます。
    • tigの方が楽
  • git reset

    • commitを取り消します。
    • git reset --soft HEAD^直前のcommitを取り消します。
    • git reset --hard HEAD^2つ前のcommitの後まで戻ります。softのステージングも取り消すイメージです。
  • git branch

    • ブランチの状況を確認します。
    • git branch -a-aオプションでリモートも含めたすべてのブランチを確認できます。
  • git checkout

    • ブランチを移動します。
    • git checkout -b hogehoge_branch-bオプションでブランチの新規作成&移動。
    • git checkout hogehoge_branchでブランチの移動。
  • git marge

    • git marge hogehoge_branch 現在のブランチに対して引数で指定したhogehoge_branchをマージします。
  • git rebase

    • git rebase -i hogehoge_branch 現在のブランチの派生元を変更します。派生元のブランチは最後の引数で指定します。
  • git log

    • commit履歴の確認

gitフロー

$ mkdir git_test&&cd $_
$ pwd
/Users/user_name/desktop/workspace/git_test
$ git init
Initialized empty Git repository in /Users/user_name/Desktop/workspace/git_test/.git/
$ vim .gitignore #ignoreファイルを作成し追跡対象外のファイルを記載(DS_Store等)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore

nothing added to commit but untracked files present (use "git add" to track)
$ git add . #変更のある全てのファイルをステージング環境に追加
$ git status #git statusの確認
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .gitignore
$ git commit -m 'add gitignore' #ステージング環境にあるファイルを保存
[master (root-commit) b80d36c] add gitignore
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore
$ git status
On branch master
nothing to commit, working tree clean
$ git branch #ローカルbranchの確認 *が現在のブランチ
* master
$ git checkout -b test_branch #ブランチを新規作成、移動。
Switched to a new branch 'test_branch'
$ git branch
  master
* test_branch
$ touch test.txt #適当なファイルを作りcommitまでする。
$ git add .
$ git commit -m 'add test.txt'
[test_branch 1723fdc] add test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.tx
$ git checkout master #masterブランチに戻ってtesst_branchでのcommitをマージする。
Switched to branch 'master'
$ git merge test_branch
Updating b80d36c..1723fdc
Fast-forward
 test.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
$ git log #logコマンドでtest_branchでのcommitが確認できる。
commit 1723fdc01ef088a840222ead0968262dd25ed507 (HEAD -> master, test_branch)
Author: Tashiro <g4160hc@gmail.com>
Date:   Mon Jun 18 07:57:11 2018 +0900

    add test.txt

commit b80d36c9c9d34eb18853bfd27631b6e37ee55459
Author: Tashiro <g4160hc@gmail.com>
Date:   Mon Jun 18 07:52:21 2018 +0900

    add gitignore

tigって何?

gitのaddやcommit,log diffをボタンひとつで管理できるcuiツールの事。本当に便利。激推し。すき。gitを逆さに読んだだけな安直感含め好き

tig導入

  • brewを使ってインストールしていきます。
$ brew install tig
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core, homebrew/cask).
==> Updated Formulae
gdbm ✔              hadolint            openfortivpn        sip
imagemagick@6 ✔     hlint               pipenv              vim@7.4
openssl ✔           imagemagick         pre-commit
python ✔            krakend             python@2
gnu-smalltalk       nzbget              rclone

==> Downloading https://homebrew.bintray.com/bottles/tig-2.3.3.high_sierra.bottl
Already downloaded: /Users/tashirosota/Library/Caches/Homebrew/tig-2.3.3.high_sierra.bottle.tar.gz
==> Pouring tig-2.3.3.high_sierra.bottle.tar.gz
==> Caveats
A sample of the default configuration has been installed to:
  /usr/local/opt/tig/share/tig/examples/tigrc
to override the system-wide default configuration, copy the sample to:
  /usr/local/etc/tigrc

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/tig/2.3.3: 15 files, 582.3KB

tig使い方

  • tig用に適当なファイルを作っておきます。
  • とりあえずtigって打ってみましょう
$ pwd
/Users/user_name/desktop/workspace/git_test
$ touch tig.txt<img width="572" alt="スクリーンショット 2018-06-18 8.12.51.png" src="https://qiita-image-store.s3.amazonaws.com/0/253317/64c4bbd2-af8f-665a-c2fb-d9c75858ed11.png">

$ tig 
  • 画面遷移してこのように表示されます。
  • git logが見れる。もう便利。
    スクリーンショット 2018-06-18 8.15.31.png

  • 任意のcommitでenterを押します。

  • git diffが表示されます。あら便利。
    スクリーンショット 2018-06-18 8.08.16.png

  • 基本的にはqで戻れます。

  • top画面で今度はsを押します。

  • git statusが表示されます。すごい。
    スクリーンショット 2018-06-18 8.15.52.png

  • addしたいファイルのところでuを押します。

  • ファイルがaddされました。因みにここでenterを押しても個別のdiff行えます。
    スクリーンショット 2018-06-18 8.15.09.png

  • commitがしたいです。shift+cを押します。

  • commit message入力に移行しますので適当な文字列を入力して下さい。

[master b21c981] add git test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tig.txt
Press Enter to continue #enterで続行
  • tig top画面でcommitが増えている事が確認できます。これはすごいなぁ。 スクリーンショット 2018-06-18 8.23.00.png

おわりに

僕が普段使っているコマンドのみを載せておりますので、他にもいい感じのコマンドやらオプションはまだまだあると思います。
ご指摘、アドバイス待ってます。tig良いです。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5