如果我尝试还原已经还原的提交会发生什么?

时间:2014-09-25 20:00:16

标签: git github version-control versioning

我试图了解以下git-revert方案中发生的情况。

运行

git revert abcdef

我成功地恢复了提交(步骤1)。然后(步骤2),我做了一些更改并通过运行

创建了一个新的提交
touch a.txt && git commit -a -m 'test commit

现在(步骤3)我试图再次恢复已在步骤1中恢复的提交。

git revert abcdef

第3步会发生什么?

1 个答案:

答案 0 :(得分:0)

git-revert手册页描述了命令:

  

给定一个或多个现有提交,还原相关修补程序引入的更改,并记录一些记录它们的新提交。

如果对命令的作用有疑问,您可以随时在玩具库中进行一些实验,以解决问题。在下面(简单的简单)示例中,还原已经还原的提交是无操作;没有创建新的提交。

$ cd ~/Desktop
$ mkdir test
$ cd test
$ git init

$ touch README
$ git add README
$ git commit -m "add README"

$ git revert master
# save commit message

$ touch a.txt
$ git add a.txt
$ git commit -m "add a.txt"

$ git log --oneline
c1e798b add a.txt
a94219e Revert "add README"
27aba0d add README

$ git revert master~2
On branch master
nothing to commit, working directory clean

$ git log --oneline
c1e798b add a.txt
a94219e Revert "add README"
27aba0d add README

附录(基于OP's comment

  

还有一个问题。 git revert是否可能导致合并冲突?

是。在它的底部,git-revert包括应用补丁,这样的操作可能引起冲突。下面的玩具示例产生一个:

$ cd ~/Desktop && mkdir fruit && cd fruit
$ git init

# write one line and make a commit
$ printf "apples\n" > fruit.txt
$ git add fruit.txt
$ git commit -m "add fruit.txt"

# write a second line and make a commit
$ printf "oranges\n" >> fruit.txt 
$ git commit -am "add oranges to the list"

# in effect, replace the second line and make a commit
$ printf "apples\nbananas\n" > fruit.txt
$ git commit -am "replace oranges by bananas"

# inspect the log
$ git log --oneline
16e76af replace oranges by bananas
7736641 add oranges to the list
5c0257b add fruit.txt

# attempt to revert the commit that introduced the second line ("oranges")
$ git revert 7736641
error: could not revert 7736641... add oranges to the list
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'