LoginSignup
1
0

More than 3 years have passed since last update.

Gitのよく使うコマンド

Last updated at Posted at 2018-02-14

基本的な使い方はレファレンス本から取得しましょう!

レファレンス本Pro Git

特にここは読みましょう:Gitの基本

基本設定

nameとemailは設定しておこう

gitのcommit時に表示させる名前とemailになるため基本設定しておきましょう。

git config --global user.name "XXXX XXXX"
git config --global user.email "xxxxx@github.com"

ブランチ(about branch)

  • 作業を行うワークディレクトリ
    • Tracked:管理対象
      • Unmodified:修正してない(statusに表示されない)
      • Modified:修正されている
      • Staged:Commitに記録しようとしている状況
    • Untracked:管理対象ではない

  • 自由に名前をつけることができる
  • 名前の変更も可能(コミット履歴を残して後でcherry-pickするときに有効に使える)

リモートブランチ(remote branch)

ブランチ関連コマンド

ブランチリスト表示

$ git branch --list or git branch

ブランチ削除

$ git branch -d(D) branch_name

remoteのブランチ削除

$ git push remote_name :branch_name

ブランチ名変更

$ git branch -m origin_branch_name rename_branch_name
EX) git branch -m xxxxx xxxxx_work

status

ファイルの状況を確認したい場合に利用

$ git status

Tracked状況にあるファイルのみ表示したい場合

$ git status -uno(untracked file no)

phpファイルのみ確認したい場合

$ git status -- "*.php"

状況を簡単に見たい場合

$ git status -s
M  package.json

fetch

レポジトリにあるブランチの最新のgit情報を取得する

# git fetch remote_name branch_name
$ git fetch origin master
From github.com:test
* branch            master    -> FETCH_HEAD

checkout

ブランチを変更したり、Modified状況になっているファイルをCommit状況に戻す場合に利用

ブランチ切り替え

git checkout branch_name

ファイルをCommit状況に戻す

git checkout file_name

RemoteからFETCHしたブランチにブランチを作成しながら切り替えしたい場合

git checkout -b branch_name FETCH_HEAD

add

Staged状況になってないファイルをStaged状況に変更(Tracked)

$ git add file_name

phpファイルのみ追加したい場合

$ git add -- "*.php"

変更した箇所を確認しながら追加したい場合

$ git add -p
→[y] or [n] 
→[q] 途中でやめる

diff

Modified状況にあるファイルの変更点を比較したい場合

$ git diff

Staged状況にいるファイルのみ変更点を比較したい場合

$ git diff --cached

Staged状況にいるPHPファイルのみ変更点を比較したい場合

$ git diff --cached -- "*.php"

commit

ワークディレクトリにstaged状況になっているファイルをメッセージとともに記録する

$ git commit -m "Commit Message"

push

commitした内容をremoteのリポジトリにあげるときに利用する

$ git push origin branch_name

remoteリポジトリにあるブランチ削除する時も利用

$ git push origin :branch_name

merge

別ブランチにある内容をマージするブランチに持ってくる

# 現在developブランチの場合
$ git merge branch_name

基本的にmergeは自分のlocalで行わず、github上で行った方がrevetするとき楽

pull

fetch & mergeを自動的にやってくれる

$ git pull origin master

cherry-pick

特定のcommitを現在のブランチに持ってくるときに使う

$ git cherry-pick commit_hash_value

revert

該当commitを戻したい場合に利用する

$ git revert commit_hash

マージコミットの場合

$ git revert -m 1(n) commit_hash

rebase

主に複数のcommitを統合するときに利用する

# 最近のcommit2つを統合したい
$ git rebase -i HEAD~2

pick 60709da Moving license into its own file
squash 30e0ccb Changed the tagline in the binary, too.

# Rebase 60709da..30e0ccb onto 60709da
#
# Commands:
#  p, pick = use commit
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#   

pickをsquashに変更して保存すると1つのcommitに統合され新しいcommit_hashが発行される!!ここ重要!!新しく発行されるため既存のcommitをremoteに上げているのならやっちゃ行けない!!

stash

一時的に現在の作業内容を保存するときに利用

$ git stash

メッセージ付きでの保存

$ git stash save "保存メッセージ"

stashリスト表示

$ git stash list

stashを適応

$ git stash apply --index

stash削除

$ git stash drop

stash適応&削除

$ git stash pop --index 

log

commitヒストリーを見たい場合に利用する

$ git log

commitのdiff結果を見たい場合(最新2つ)

$ git log -p 2

commitの統計を見たい場合

$git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

 Rakefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

commitヒストリーを一行で見たい場合

 $ git log --pretty=oneline

commitヒストリーをmerge commitを除いて見たい場合

$ git log --pretty=oneline --no-merges

便利な設定

Aliasで効率よく、colorを利用してメリハリをつけミスを減らす

Alias

# 最後のcommitのLogを確認したい場合
$ git config --global alias.last "log -1 HEAD"
# Tracked状況のみ確認したい
$ git config --global alias.stg "status -uno"
# Staged状況を戻す
$ git config --global alias.unstg "reset HEAD --"
# commitログをTree構造で確認できる
$ git config --global alias.logall "log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
# commitログを1行でみる
$ git config --global alias.plog "log --pretty='format:%C(yellow)%h %C(green)%cd %C(reset)%s %C(red)%d %C(cyan)[%an]' --date=iso"

Color

# gitがカラーで表示される
$ git config --global color.ui true
1
0
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
1
0