首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我试图还原已经被恢复的提交,会发生什么?

如果我试图还原已经被恢复的提交,会发生什么?
EN

Stack Overflow用户
提问于 2014-09-25 20:00:16
回答 1查看 1.5K关注 0票数 1

我试图理解在下面的git-revert场景中会发生什么。

通过跑

代码语言:javascript
复制
git revert abcdef

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

代码语言:javascript
复制
touch a.txt && git commit -a -m 'test commit

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

代码语言:javascript
复制
git revert abcdef

第三步会发生什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-25 21:20:42

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

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

当对给定的命令有疑问时,您可以在玩具库中进行一些实验,以修复想法。在下面的示例(无可否认很简单)中,恢复已经恢复的提交是不操作的;没有创建新的提交。

代码语言:javascript
复制
$ 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包括应用补丁,这样的操作可能导致冲突。下面的玩具示例提供了一个例子:

代码语言:javascript
复制
$ 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'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26047067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档