Scenario:
Got multiple commits, I want to revert the commits back to 'dc1d935'(Keep 'dc1d935', revert other later commits):
// On master branch
$ git log --oneline -8
b41e6bd (HEAD -> master) Commit fifth line
90b77e8 Commit fourth line
3839548 Commit third line
dc1d935 Commit second line
2d7be83 Commit first line
$ ls -al
total 13
drwxr-xr-x 1 zzheng 1049089 0 xxx ./
drwxr-xr-x 1 zzheng 1049089 0 xxx ../
drwxr-xr-x 1 zzheng 1049089 0 xxx .git/
-rw-r--r-- 1 zzheng 1049089 57 xxx demoFile.txt
$ cat demoFile.txt
Line first
Second line
Third line
Fourth line
Fifth line
- Step 1:
Checkout a new branch:
$ git checkout -b dev
Switched to a new branch 'dev'
- Step 2:
Reset to the commit 'dc1d935'
$ git reset --hard dc1d935
HEAD is now at dc1d935 Commit second line
$ git log --oneline -8
dc1d935 (HEAD -> dev) Commit second line
2d7be83 Commit first line
$ git reflog --oneline
dc1d935 (HEAD -> dev) HEAD@{0}: reset: moving to dc1d935
b41e6bd (master) HEAD@{1}: checkout: moving from master to dev
b41e6bd (master) HEAD@{2}: commit: Commit fifth line
90b77e8 HEAD@{3}: commit: Commit fourth line
3839548 HEAD@{4}: commit: Commit third line
dc1d935 (HEAD -> dev) HEAD@{5}: commit: Commit second line
2d7be83 HEAD@{6}: commit (initial): Commit first line
- Step 3:
Reset to HEAD@{1}
$ git reset --soft HEAD@{1}
$ git status
On branch dev
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: demoFile.txt
$ git diff --cached
diff --git a/demoFile.txt b/demoFile.txt
index e028882..a1f7f87 100644
--- a/demoFile.txt
+++ b/demoFile.txt
@@ -1,5 +1,2 @@
Line first
Second line
-Third line
-Fourth line
-Fifth line
- Step 4:
The cached diff is exactly the reversed change of what I want, so add a commit
$ git commit -m "Reverse commit back to 'dc1d935'"
[dev 014a4d0] Reverse commit back to 'dc1d935'
1 file changed, 3 deletions(-)
$ git log --oneline -8
adb947c (HEAD -> dev) Reverse commit back to 'dc1d935'
b41e6bd (master) Commit fifth line
90b77e8 Commit fourth line
3839548 Commit third line
dc1d935 Commit second line
2d7be83 Commit first line
$ git show HEAD
commit adb947cd36e14866627594ff33fe31e3f15c324c (HEAD -> dev)
Author: xxx
Date: xxx
Reverse commit back to 'dc1d935'
diff --git a/demoFile.txt b/demoFile.txt
index e028882..a1f7f87 100644
--- a/demoFile.txt
+++ b/demoFile.txt
@@ -1,5 +1,2 @@
Line first
Second line
-Third line
-Fourth line
-Fifth line
- Step 5:
Merge changes to master
$ git checkout master
Switched to branch 'master'
$ git merge dev
Updating b41e6bd..adb947c
Fast-forward
demoFile.txt | 3 ---
1 file changed, 3 deletions(-)
$ git log --oneline -8
adb947c (HEAD -> master, dev) Reverse commit back to 'dc1d935'
b41e6bd Commit fifth line
90b77e8 Commit fourth line
3839548 Commit third line
dc1d935 Commit second line
2d7be83 Commit first line
Pay attention to the git reset
param:
git reset --hard
git reset --mixed
-
git reset --soft
gitRebaseParam.jpg
Another easier approach:
$ git log --oneline -10
b41e6bd (HEAD -> master) Commit fifth line
90b77e8 Commit fourth line
3839548 Commit third line
dc1d935 Commit second line
2d7be83 Commit first line
If I want to keep 'dc1d935' and revert other later commits:
- Step 1:
Usegit revert
to get a reversed commit
git revert 3839548^..b41e6bd --no-commit
// Param '--no-commit' will add the inverse changes to the staging, but will not make a commit.
$ git status
On branch master
You are currently reverting commit 3839548.
(all conflicts fixed: run "git revert --continue")
(use "git revert --skip" to skip this patch)
(use "git revert --abort" to cancel the revert operation)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: demoFile.txt
$ git diff --cached
diff --git a/demoFile.txt b/demoFile.txt
index e028882..a1f7f87 100644
--- a/demoFile.txt
+++ b/demoFile.txt
@@ -1,5 +1,2 @@
Line first
Second line
-Third line
-Fourth line
-Fifth line
- Step 2:
Make a commit
$ git commit -m "Revert to 'dc1d935'"
[master fa60dfa] Revert to 'dc1d935'
1 file changed, 3 deletions(-)
$ git log --oneline -8
fa60dfa (HEAD -> master) Revert to 'dc1d935'
b41e6bd Commit fifth line
90b77e8 Commit fourth line
3839548 Commit third line
dc1d935 Commit second line
2d7be83 Commit first line
$ cat demoFile.txt
Line first
Second line
网友评论