我试图理解在下面的git-revert场景中会发生什么。
通过跑
git revert abcdef我成功地恢复了提交(步骤1)。然后(步骤2),我做了一些更改,并通过运行
touch a.txt && git commit -a -m 'test commit现在(步骤3),我再次尝试恢复已在步骤1中恢复的提交。
git revert abcdef第三步会发生什么?
发布于 2014-09-25 21:20:42
因此,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'https://stackoverflow.com/questions/26047067
复制相似问题