はじめに
業務でよく使うgitコマンドをまとました。今後もどんどん更新していく予定です。
何か気になる点があれば気軽にコメントお願いします。
git add と git commitを同時に行う
$ git commit -am"commit message"`
直前のコミットを編集する
現在編集している差分を直前のコミットと一緒にしたい場合
$ git log
commit 2807055f737ee79962a7373996165598478704a3
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Oct 11 09:44:08 2016 +0900
fixed file1.text
$ git status
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: firstFile.text
1. commit --amend
コマンドを入力
$ git add firstFile.text
$ git commit --amend -m "fixed file1.text"
commit a12388f3d3548ac713fe52b0da87279260055fff
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Oct 11 09:44:08 2016 +0900
fixed file1.text
現在の差分が直前のコミットと一緒にされる。
$ git commit --amend
のみだとエディタが開かれてコミットメッセージを編集できる
直前のコミットメッセージを編集する。
上記のやり方で直前のコミットメッセージのみの編集も可能
$ git commit --amend -m "commit message"
$ git commit --amend
いずれかのコマンドで、直前のコミットメッセージを編集する
過去のコミットを編集する
$ git log
commit 121a4d7729700d363e56b681e27518bf4ac0f34b
Author: shoheiyokoyama <~~~@gmail.com>
Date: Fri Sep 30 11:10:56 2016 +0900
third commit
commit 3fbea2f615acf7878d76f94594932633d5e39e17
Author: shoheiyokoyama <~~~@gmail.com>
Date: Fri Sep 30 11:07:13 2016 +0900
second commit
commit 404a1c9c88824e0921e3795a49a7c5e2ce7c27f7
Author: shoheiyokoyama <~~~@gmail.com>
Date: Fri Sep 30 11:06:47 2016 +0900
first commit
1. rebaseコマンドで編集するコミットを表示
second commit
を編集する場合、一つ前のハッシュを入力
$ git rebase -i 404a1c9c88824e0921e3795a49a7c5e2ce7c27f7
pick 3fbea2f second commit
pick 121a4d7 third commit
...
もしくは
$ git rebase -i HEAD~~
HEAD
からHEAD~~
(2個前のコミット)を表示
$ git rebase -i HEAD~5
HEAD
から5個分のコミットを表示
2. 編集するコミットをedit
に
# eでもOK
edit 3fbea2f second commit
pick 121a4d7 third commit
...
保存すると、以下の出力がされコミットがチェックアウトされるのでここで過去のコミット(second commit
)を編集する
Stopped at 3fbea2f615acf7878d76f94594932633d5e39e17... second commit
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
3. 修正したコミットを保存(再度コミット)
$ git add .
$ git commit --amend
vimが起動し、second commit
のコミットメッセージを編集できる
second commit
# Please enter commit message for your cahnges. Lines starting
# with '#' ignored, and an empty message aborts the commit
#
# Date: ...
#
# ...
もしくは、次のコマンドでもコミット保存可能
$ git add .
$ git commit --amend -m"commit mesage"
4. コミットの編集を終了する
$ git rebase --continue
この編集が影響してコンフリクトが起きた場合は、3, 4の作業を繰り返してマージ作業を行う
git rebase
自体の作業を中止したい場合は
$ git rebase --abort
でrebaseを行う前に戻ることができる
過去のコミットをまとめる
$ git log
今回は、fixed sample-file
とfixed2 sample-file
のコミットをまとめる
commit d9d1012289748c614787399ec3d1b8e98172c156
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Oct 11 07:15:52 2016 +0900
fixed2 sample-file
commit 5de2cd2918e148a9317fc9192cfb635fd878fa49
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Oct 11 07:15:30 2016 +0900
fixed sample-file
commit 63b89bece5a508c418f7c3a0c00a15f1a4b04ec1
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Oct 11 07:14:50 2016 +0900
create sample-file
1. rebaseコマンドでまとめるコミットを表示
$ git rebase -i HEAD~~
pick 5de2cd2 fixed sample-file
pick d9d1012 fixed2 sample-file
...
2. まとめたいするコミットをsquash
に
この場合、fixed sample-file
とfixed2 sample-file
のコミットをまとめる
# sでもOK
pick 5de2cd2 fixed sample-file
s d9d1012 fixed2 sample-file
...
s
を入力した fixed2 sample-file
のコミットが一個前のコミットのマージされることになるので、まとめたいコミットの一つ前のコミットまで表示されるようにする。
3. コミットをまとめて上書き保存
# This is a combination of 2 commits.
# The first commit's message is:
fixed sample-file
# The is the 2nd commit's message:
fixed2 sample-file
...
この場合、コミットメッセージはfixed sample-file
として、まとめられる。コミットメッセージを変更したい場合は、fixed sample-file
を編集する
このようにコミットがまとめられていれば成功。
commit 422219b91022e006dc87766aab2e43cb4d670e89
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Oct 11 07:26:34 2016 +0900
fixed sample-file
fixed2 sample-file
commit 63b89bece5a508c418f7c3a0c00a15f1a4b04ec1
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Oct 11 07:14:50 2016 +0900
create sample-file
過去のコミットを取り消す
ワークディレクトリの内容を保持し取り消す
一つ前のコミットに戻す
$ git reset --soft HEAD^
ワークディレクトリの内容とコミットを取り消す
一つ前のコミットに戻す
$ git reset --hard HEAD^
最新のコミットからx件分のコミットを取り消す
$ git reset --hard HEAD~x
直前の状態に戻す(マージ作業時など)
$ git reset --hard HEAD
インデックスからファイルをアンステージする(addしたファイルを戻す)
$ git reset HEAD filename
コミットだけを取り込む
branch
一覧
$ git branch
* branch1
master
branch1
のコミット履歴
$ git log
commit 4ae31b629d788a576edd02426dabcb3a69ab62b2
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Nov 1 14:44:13 2016 +0900
commit2
commit d727ad50a8c673aacc727af6b9571697d99048db
Author: shoheiyokoyama <~~~@gmail.com>
Date: Tue Nov 1 14:43:46 2016 +0900
commit1
masterにcommit1
だけ取り込む場合
$ git checkout master
Switched to branch 'master'
$ git cherry-pick d727ad5
commit1
の内容をインデックスに追加する
$ git cherry-pick d727ad5 -n
masterに過去二番目までのコミットを取り込む
$ git cherry-pick branch1~2..branch1
競合した場合、競合箇所を修正する
現在の状態(ワーキングツリー・インデックス)を保存する
$ git stash
stashで保存した最新の状態を復元する
$ git stash pop
現在の状態(ワーキングツリー・インデックス)をメッセージ付きで保存する
$ git stash save "stash message"
stashで保存したリストを表示する
$ git stash list
stash@{0}: On master: stash message2
stash@{1}: On master: stash message
番号を指定して保存状態を復元する
$ git stash pop stash@{1}
番号を指定して保存状態を復元し、stahs llistにも残ったままにする
$ git stash apply pop stash@{1}
保存状態を削除する
$ git stash drop stash@{1}
保存状態をすべて削除する
$ git stash clear
タグでブランチを切る
$ git checkout -b tagname refs/tags/tagname