(master)$ git reset --hard HEAD~1 HEAD is now at 688a4d0 WIP
(master)$ git log --oneline --all --graph * 688a4d0 (HEAD -> master) WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
好的,最近的commit就扔掉了,仿佛没有存在过。
Reverting Commits
如果commit已经share with others,就不能直接reset了,而是用revert,保留log中的记录。
(master)$ git revert HEAD~3..HEAD [master 12ad6f2] Revert "WIP" 1 file changed, 1 deletion(-) [master 83527ab] Revert "Update terms of service and Google Map SDK version." 1 file changed, 1 deletion(-) [master 667bde8] Revert "WIP" 1 file changed, 1 deletion(-)
但是这就有三条commit history了,一点都不clean:
1 2 3 4 5 6 7 8 9 10 11 12
(master)$ git log --oneline --all --graph * 667bde8 (HEAD -> master) Revert "WIP" * 83527ab Revert "Update terms of service and Google Map SDK version." * 12ad6f2 Revert "WIP" * 688a4d0 WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
所以可以不commit,反做的记录都在staging area里:
1 2 3 4
(master)$ git revert --no-commit HEAD~3..
(master|REVERTING)$ git status -s M file1.txt
用—continue来继续,并手动将commit message改为”Revert bad code.“:
(master)$ git log --oneline --all --graph * 5322dae (HEAD -> master) Revert bad code. * 688a4d0 WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
Recovering Lost Commits
不小心reset了HEAD:
1 2
(master)$ git reset --hard HEAD~6 HEAD is now at 127cf78 Fix a typo
(master)$ git reflog 127cf78 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~6 5322dae HEAD@{1}: commit: Revert bad code. 688a4d0 HEAD@{2}: reset: moving to HEAD~3 667bde8 HEAD@{3}: revert: Revert "WIP" 83527ab HEAD@{4}: revert: Revert "Update terms of service and Google Map SDK version." 12ad6f2 HEAD@{5}: revert: Revert "WIP" 688a4d0 HEAD@{6}: reset: moving to 688a4d0 fd52d6e HEAD@{7}: revert: Revert "Update terms of service and Google Map SDK version." f2139a9 HEAD@{8}: revert: Revert "WIP" 688a4d0 HEAD@{9}: reset: moving to HEAD~1 475ea30 HEAD@{10}: commit: . 688a4d0 HEAD@{11}: commit: WIP b0201fc HEAD@{12}: commit: Update terms of service and Google Map SDK version. db94b15 HEAD@{13}: commit: WIP 175d394 HEAD@{14}: commit: Add a reference to Google Map SDK. 24f3009 HEAD@{15}: commit: Change the color of restaurant icons. 127cf78 (HEAD -> master) HEAD@{16}: commit: Fix a typo 17de14d HEAD@{17}: commit: Render restaurants the map. 00315a1 HEAD@{18}: commit (initial): Initial commit
用 commit ID 或者 entry ID 都可以恢复(此处用entry ID):
1 2 3 4 5 6 7 8 9 10 11 12 13
(master)$ git reset --hard HEAD@{1} HEAD is now at 5322dae Revert bad code.
(master)$ git log --oneline --all --graph * 5322dae (HEAD -> master) Revert bad code. * 688a4d0 WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
假如有个Pointer是feature:
1
$ git reflog show feature
可以看到这个pointer的所有历史。
Amending Commit
Amending the Last Commit
假如现在有个 commit:
1 2 3 4 5 6 7
(master)$ echo cafes >> file1.txt
(master)$ git commit -am "Render cafes on the map" warning: LF will be replaced by CRLF in file1.txt. The file will have its original line endings in your working directory [master b4c943f] Render cafes on the map 1 file changed, 1 insertion(+)
交了之后发现我们不是想写 cafes,而是想写 blue cafes 啊,但是这是个小错,能不能小修补?当然可以。
(master)$ git log --oneline --all --graph * a60e09a (HEAD -> master) Render cafes on the map * 5322dae Revert bad code. * 688a4d0 WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
(master)$ git show HEAD commit a60e09a0bff95afbb3c83ca20591cec3c9afe638 (HEAD -> master) Author: Stone <masaike@qq.com> Date: Fri Apr 30 14:38:19 2021 +0800
(master)$ git log --oneline --all --graph * 5bbe8a7 (HEAD -> master) Render cafes on the map * 5322dae Revert bad code. * 688a4d0 WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
(master)$ git commit -m "Render cafes on the map." [master e47642a] Render cafes on the map. 1 file changed, 1 insertion(+)
(master)$ git log --oneline --all --graph * e47642a (HEAD -> master) Render cafes on the map. * 5322dae Revert bad code. * 688a4d0 WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
(master)$ git show HEAD commit e47642a4cd59cfec5697d82acbbfa7372547823e (HEAD -> master) Author: Stone <masaike@qq.com> Date: Fri Apr 30 14:42:11 2021 +0800
(master)$ git log --oneline --all --graph * e47642a (HEAD -> master) Render cafes on the map. * 5322dae Revert bad code. * 688a4d0 WIP * b0201fc Update terms of service and Google Map SDK version. * db94b15 WIP * 175d394 Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
(master)$ git show 175d394 commit 175d3944118590daf26ba25c46da4061c0ad641b Author: Stone <1140525895@qq.com> Date: Fri Apr 30 10:01:56 2021 +0800
(master)$ git rebase -i 24f3009 Stopped at 175d394... Add a reference to Google Map SDK. You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
所以会需要你edit完这个commit用—amend修正了再continue。
修改内容是增加一个文件license.txt:
1 2 3 4 5 6 7 8 9
(master|REBASE 1/6)$ echo license > license.txt
(master|REBASE 1/6)$ git add .
(master|REBASE 1/6)$ git commit --amend [detached HEAD 0eb205c] Add a reference to Google Map SDK. Date: Fri Apr 30 10:01:56 2021 +0800 2 files changed, 2 insertions(+) create mode 100644 license.txt
可以看到分了支了:
1 2 3 4 5 6 7 8 9 10 11 12 13
(master|REBASE 1/6)$ git log --oneline --all --graph * 0eb205c (HEAD) Add a reference to Google Map SDK. | * e47642a (master) Render cafes on the map. | * 5322dae Revert bad code. | * 688a4d0 WIP | * b0201fc Update terms of service and Google Map SDK version. | * db94b15 WIP | * 175d394 Add a reference to Google Map SDK. |/ * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
(master|REBASE 1/6)$ git rebase --continue Successfully rebased and updated refs/heads/master.
(master)$ git log --oneline --all --graph * 0d35c4a (HEAD -> master) Render cafes on the map. * 6f2b578 Revert bad code. * fce1354 WIP * a3f2e62 Update terms of service and Google Map SDK version. * e67e73d WIP * 0eb205c Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
(master)$ git log --oneline --all --graph * 0d35c4a (HEAD -> master) Render cafes on the map. * 6f2b578 Revert bad code. //------------- * fce1354 WIP //------------- * a3f2e62 Update terms of service and Google Map SDK version. * e67e73d WIP //------------- * 0eb205c Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
rebase到0eb205c,在弹出的窗口直接删掉第1,3,4行就行了:
1 2 3 4 5 6 7 8 9
(master)$ git rebase -i 0eb205c error: could not apply a3f2e62... Update terms of service and Google Map SDK version. Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort". Could not apply a3f2e62... Update terms of service and Google Map SDK version. Auto-merging file1.txt CONFLICT (content): Merge conflict in file1.txt
Normal merge conflict for'file1.txt': {local}: modified file {remote}: modified file
(master|REBASE 1/2)$ git rebase --continue [detached HEAD b153f53] Update terms of service and Google Map SDK version. 1 file changed, 1 insertion(+) error: could not apply 0d35c4a... Render cafes on the map. Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort". Could not apply 0d35c4a... Render cafes on the map. Auto-merging file1.txt CONFLICT (content): Merge conflict in file1.txt
Normal merge conflict for'file1.txt': {local}: modified file {remote}: modified file
好了:
1 2 3 4 5 6 7 8 9 10 11 12 13
(master|REBASE 2/2)$ git rebase --continue [detached HEAD 3b55d73] Render cafes on the map. 1 file changed, 1 insertion(+), 1 deletion(-) Successfully rebased and updated refs/heads/master.
(master)$ git log --oneline --all --graph * 3b55d73 (HEAD -> master) Render cafes on the map. * b153f53 Update terms of service and Google Map SDK version. * 0eb205c Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
Rewording Commit Messages
比如要修改17de14d和b153f53的commit message:
1 2 3 4 5 6 7 8
(master)$ git log --oneline --all --graph * 3b55d73 (HEAD -> master) Render cafes on the map. * b153f53 Update terms of service and Google Map SDK version. * 0eb205c Add a reference to Google Map SDK. * 24f3009 Change the color of restaurant icons. * 127cf78 Fix a typo * 17de14d Render restaurants the map. * 00315a1 Initial commit
(master)$ git rebase -i 17de14d^ [detached HEAD 38b49e0] Render restaurants on the map. Date: Fri Apr 30 09:59:03 2021 +0800 1 file changed, 1 insertion(+) [detached HEAD dd32e6e] Add a reference to Google Map SDK v1.0.0. Date: Fri Apr 30 10:01:56 2021 +0800 2 files changed, 2 insertions(+) create mode 100644 license.txt Successfully rebased and updated refs/heads/master.
看看,改好了:
1 2 3 4 5 6 7 8
(master)$ git log --oneline --all --graph * 526429c (HEAD -> master) Render cafes on the map. * e3efdfd Update terms of service and Google Map SDK version. * dd32e6e Add a reference to Google Map SDK v1.0.0. //-----v1.0.0------ * 1ca0331 Change the color of restaurant icons. * cacde15 Fix a typo * 38b49e0 Render restaurants on the map. //------on-------- * 00315a1 Initial commit
依然,00315a1之后的全部commit,ID都变了。
Re-ordering Commits
1 2 3 4 5 6 7 8
(master)$ git log --oneline --all --graph * 2879b9f (HEAD -> master) Render cafes on the map. * 95cd9e7 Update terms of service and Google Map SDK version. * 398859b Add a reference to Google Map SDK v1.0.0. * 1ca0331 Change the color of restaurant icons. * cacde15 Fix a typo * 38b49e0 Render restaurants on the map. * 00315a1 Initial commit
要将398859b调到38b49e0前面去:
git rebase后弹出的窗口将398859b对应行移到最上边(选中该行,Atl+↑ 调整):
1 2 3 4 5 6 7 8 9 10 11
(master)$ git rebase -i 00315a1 Successfully rebased and updated refs/heads/master.
(master)$ git log --oneline --all --graph * 3a8e00e (HEAD -> master) Render cafes on the map. * 7a97000 Update terms of service and Google Map SDK version. * 031bd2e Change the color of restaurant icons. * f8bce71 Fix a typo * 8eb9241 Render restaurants on the map. //--------------- * 2e926d2 Add a reference to Google Map SDK v1.0.0. //--------------- * 00315a1 Initial commit
Squashing Commits
1 2 3 4 5 6 7 8
(master)$ git log --oneline --all --graph * 3a8e00e (HEAD -> master) Render cafes on the map. * 7a97000 Update terms of service and Google Map SDK version. * 031bd2e Change the color of restaurant icons. * f8bce71 Fix a typo * 8eb9241 Render restaurants on the map. * 2e926d2 Add a reference to Google Map SDK v1.0.0. * 00315a1 Initial commit
(master)$ git rebase -i 2e926d2 [detached HEAD dffab6e] Render restaurants on the map. Date: Fri Apr 30 09:59:03 2021 +0800 1 file changed, 3 insertions(+) Successfully rebased and updated refs/heads/master.
1 2 3 4 5 6
(master)$ git log --oneline --all --graph * 42732b9 (HEAD -> master) Render cafes on the map. * 56eb52d Update terms of service and Google Map SDK version. * dffab6e Render restaurants on the map. * 2e926d2 Add a reference to Google Map SDK v1.0.0. * 00315a1 Initial commit
Nice.
Method 2: fixup
git rebase -i 弹出窗口,用fixup,就不需要手动删改commit message(Git不会pick用了fixup的那一条的message)。而是直接就用8eb9241的commit message:
log同上。
Splitting a Commit
将549d310拆成两个:
1 2 3 4 5 6
(master)$ git log --oneline --all --graph * de27a35 (HEAD -> master) Render cafes on the map. * 549d310 Update terms of service and Google Map SDK version. * 8014c85 Render restaurants on the map. * 2e926d2 Add a reference to Google Map SDK v1.0.0. * 00315a1 Initial commit
(master)$ git rebase -i 549d310^ Stopped at 549d310... Update terms of service and Google Map SDK version. You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
(master|REBASE 1/2)$ git log --oneline --all --graph * de27a35 (master) Render cafes on the map. * 549d310 (HEAD) Update terms of service and Google Map SDK version. * 8014c85 Render restaurants on the map. * 2e926d2 Add a reference to Google Map SDK v1.0.0. * 00315a1 Initial commit
(master|REBASE 1/2)$ git commit -m "Add terms of service." [detached HEAD 1d17606] Add terms of service. 1 file changed, 4 insertions(+)
(master|REBASE 1/2)$ git add .
(master|REBASE 1/2)$ git commit -m "Update Google Map SDK version." [detached HEAD 55d127c] Update Google Map SDK version. 1 file changed, 1 insertion(+) create mode 100644 sdkversion.txt
(master|REBASE 1/2)$ git log --oneline --all --graph * ed8737e (HEAD) Update Google Map SDK version. * 44220a0 Add terms of service. | * de27a35 (master) Render cafes on the map. | * 549d310 Update terms of service and Google Map SDK version. |/ * 8014c85 Render restaurants on the map. * 2e926d2 Add a reference to Google Map SDK v1.0.0. * 00315a1 Initial commit
(master|REBASE 1/2)$ git rebase --continue Successfully rebased and updated refs/heads/master.
整挺好:
1 2 3 4 5 6 7
(master)$ git log --oneline --all --graph * a6ea6fd (HEAD -> master) Render cafes on the map. * ed8737e Update Google Map SDK version. //--------------- * 44220a0 Add terms of service. //--------------- * 8014c85 Render restaurants on the map. * 2e926d2 Add a reference to Google Map SDK v1.0.0. * 00315a1 Initial commit