超级有用的git reset --hard和git revert命令
但是这样做也有问题,可能之前本地的,没有提交的修改,都消失了。可以尝试git revert命令
reset是指将当前head的内容重置,不会留任何痕迹。
Sets the current head to the specified commit and optionally resets the index and working tree to match.
会将最新的3次提交全部重置,就像没有提交过一样。
根据--soft --mixed --hard,会对working tree和index和HEAD进行重置。
revert是撤销某次提交,但是这次撤销也会作为一次提交进行保存(这样就不会丢失原来修改过,但是没有提交的内容?)。
//
非常好的一篇文章
http://alpha-blog.wanglianghome.org/2010/07/30/git-partial-rollback/
如何使用git回退部分修改
人总有犯错误的时候,如果发现不小心把私货提交到公共代码库,改如何挽回呢?
例如如下代码库:
$ mkdir git-partial-revert
$ cd git-partial-revert
$ echo "hello a" >a.txt
$ echo "hello b" >b.txt
$ git init
Initialized empty Git repository in /home/liang/project/git/git-partial-revert/.git/
$ git add *.txt
$ git commit -m "initial version"
[master (root-commit) b5e1a24] initial version
$ git log
commit b5e1a24fc65202977b903435
Author: Liang Wang <a@b.c>
Date:
以下是一次错误的修改。
$ echo "hello a from b" >>a.txt
$ echo "hello b from a" >>b.txt
$ git commit -a -m "hello a from b"
[master df3144e] hello a from b
$ git push origin master
$ git log
commit df3144e3168f6ec189ed0b2b
Author: Liang Wang <a@b.c>
Date:
commit b5e1a24fc65202977b903435
Author: Liang Wang <a@b.c>
Date:
错误在于只应该提交对于a.txt的修改,b.txt的修改是私货,应该留在本地,以后提交。
第一种方法:纯手工修改。取决于要修改的内容多少,这可能是最简单也可能是最笨的方法。
如果错误发生在最新的commit里面,可以使用git reset修改。如下:
$ git reset b5e1a24f -- b.txt
Unstaged changes after reset:
M
$ cat b.txt
hello b
hello b from a
$ git log
commit df3144e3168f6ec189ed0b2b
Author: Liang Wang <lwang1@marvell.com>
Date:
commit b5e1a24fc65202977b903435
Author: Liang Wang <a@b.c>
Date:
$ git diff
diff --git a/b.txt b/b.txt
index b1bdbca..ab47375 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
+hello b from a
$ git status
# On branch master
# Changes to be committed:
#
#
#
#
# Changed but not updated:
#
#
#
#
#
$ git diff --cached
diff --git a/b.txt b/b.txt
index ab47375..b1bdbca 100644
--- a/b.txt
+++ b/b.txt
@@ -1,2 +1 @@
-hello b from a
$ git commit -m "revert change in b"
[master d49f9f2] revert change in b
$ git push origin master
$ git status
# On branch master
# Changed but not updated:
#
#
#
#
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
diff --git a/b.txt b/b.txt
index b1bdbca..ab47375 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
+hello b from a
$ git log --stat
commit d49f9f2531ed9106ea53006b
Author: Liang Wang <a@b.c>
Date:
commit df3144e3168f6ec189ed0b2b
Author: Liang Wang <a@b.c>
Date:
commit b5e1a24fc65202977b903435
Author: Liang Wang <a@b.c>
Date:
这时b的修改就从公共代码库上拿掉了,但是还保留在本地。
或者也可以使用git revert。下面操作的缺点是没有保留对于b.txt的修改。如何保留修改留给读者作为习题 :-)
$ git revert -n df3144e31
Finished one revert.
$ git status
# On branch master
# Changes to be committed:
#
#
#
#
#
$ git diff --cached
diff --git a/a.txt b/a.txt
index 2c5e468..74614c9 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1 @@
-hello a from b
diff --git a/b.txt b/b.txt
index ab47375..b1bdbca 100644
--- a/b.txt
+++ b/b.txt
@@ -1,2 +1 @@
-hello b from a
$ git reset HEAD a.txt
Unstaged changes after reset:
M
$ cat a.txt
hello a
$ git checkout -- a.txt
$ cat a.txt
hello a
hello a from b
$ cat b.txt
hello b
$ git commit -m "revert change in b"
[master 5f6a2e1] revert change in b
$ git log --stat
commit 5f6a2e16bfd59281d0150e36
Author: Liang Wang <a@b.c>
Date:
commit df3144e3168f6ec189ed0b2b
Author: Liang Wang <a@b.c>
Date:
commit b5e1a24fc65202977b903435
Author: Liang Wang <a@b.c>
Date:
如果错误的修改已经不是最新的commit,则只能使用git revert。
/
我们teamleader的总结,很不错的!!!
http://guibin.iteye.com/blog/101436
更多推荐
所有评论(0)