【问题标题】:Git Revert "revert is not possible because you have unmerged files"Git Revert“无法恢复,因为您有未合并的文件”
【发布时间】:2016-02-23 21:27:01
【问题描述】:

我做了一个简单的例子来给一些 GIT 函数发短信。存储库中只有一个文本文件:

1

添加并提交更改

然后更改了txt。文件到:

1
2

添加并提交更改

Git 日志给出以下结果:

commit 00e59212a6...
..
second commit

commit 7c319d587....
..
first commit

那时我想回到第一次提交(但我不想使用“reset --hard”。我试过了:

git revert 7c319d587

但我收到以下错误消息:

revert is not possible because you have unmerged files
hint: Fix them up in the work tree, and the use "git add/rm
hint: as appropriate to mark resolution and make a commit

我是 GIT 新手 - 在我使用 SVN 之前,你可以非常简单地处理这些事情,所以我想知道这些简单的事情是否需要在 GIT 中执行几个步骤?

非常感谢您的帮助。

【问题讨论】:

  • 这听起来像是其他东西添加了一个文件(例如编辑器备份文件)。尝试git status 了解 git 的想法。
  • git status --> 在分支master上;您当前正在恢复提交 7c319d587。 (修复冲突并运行“git revert --continue”)(使用 git revert --abort 取消恢复操作);未合并的路径:(使用“git reset Heat ..”取消暂存);被他们删除:test.txt;没有添加到提交的更改(使用“git add”和或“git commit -a)

标签: git revert


【解决方案1】:

嗯,我已经尝试了你认为你所做的,完全按照你写的去做

cd /tmp
mkdir so35588521
cd so35588521
git init
# Initialized empty Git repository in /tmp/so35588521/.git/
touch 1
echo "1" > 1
git add .
git commit -m "first"
# [master (root-commit) 173f431] first
#  1 file changed, 0 insertions(+), 0 deletions(-)
# create mode 100644 1
touch 2
touch 3
git add .
git commit -m "second"
# [master a9fdcc9] second
#  2 files changed, 0 insertions(+), 0 deletions(-)
#  create mode 100644 2
#  create mode 100644 3
git log
# commit a9fdcc9338c9b3de25c211580a35ab63d1d57c2e
# Author: Me and my email
# Date:   Tue Feb 23 22:46:50 2016 +0100
git revert a9fd
# [master dd5a86e] Revert "second"
# 2 files changed, 0 insertions(+), 0 deletions(-)
# delete mode 100644 2
# delete mode 100644 3

如您所见 - 一切顺利。因此,我想,啊哈!她/他一定做了其他事情,而忽略了在 SO 上提及它。哦,好吧,无论如何,我会弄清楚她/他用我的水晶球做了什么。把它放在无用的地方有什么意义。

所以,合并,git 说。然后,让我们创建一个分支并尝试将它与我们的 master 分支合并。

git checkout -b a_branch
# Switched to a new branch 'a_branch'
echo "2" > 1
git status
# On branch a_branch ...
git add .
git commit -m "third"
# [a_branch 96d3a7a] third
#  1 file changed, 1 insertion(+), 1 deletion(-)
git checkout master
# Switched to branch 'master'
echo "3" > 1
git add .
git commit -m "fourth"
# [master 7f72eec] fourth
#  1 file changed, 2 insertions(+), 1 deletion(-)
#### Now we have first commit, and second, and revert second, and two commits: third on **a_branch** branch and fourth on **master**. 
git merge a_branch
# Auto-merging 1
# CONFLICT (content): Merge conflict in 1
# Automatic merge failed; fix conflicts and then commit the result.
# *Oh DANG* whyy? Git stop, don't do that it hurts, revert!
git revert last_commit_id_from_git_log
# error: revert is not possible because you have unmerged files.
# hint: Fix them up in the work tree, and then use 'git add/rm <file>'
# hint: as appropriate to mark resolution and make a commit.
# fatal: revert failed
# *Git nope!*

现在,这只是我的猜测,我希望给 SO 带来一点幽默。如果这不是您所做的,您能否告诉我们您所做的确切步骤?由于我无法复制您遇到的错误,因此我感到无法解决问题。

编辑:

既然 OP 坚持肯定没有做分支...

rm *
rm -rf .git
git init
echo -e "line one\n" > 1
git add .
git ci -m "first"
echo -e "line two\n" >> 1
git ci -a -m "second"
git log
# commit 23d710610d98c1046844870557208f335e76a933
# Author: ME <me@home>
# Date:   Tue Feb 23 23:31:28 2016 +0100
#
#     second
# 
# commit 22873fb5fbf06d80a1779fe6740060c660d68714
# Author: ME <me@home>
# Date:   Tue Feb 23 23:30:47 2016 +0100
#
#    first

git revert "first"
# fatal: bad revision 'first'  # but OK i assume You used id, so,
git revert 22873fb5fbf06d80
# error: could not revert 22873fb... first
# 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'

啊哈,现在我明白了!那……这很奇怪。

好的,我检查了,实际上你是对的,在我的 git --version 2.6.3 上,我可以复制这种行为。实际上,我认为你在这里进入了非常极端的情况。发生的事情是,在您的第二次提交之后,git 存储了您对文件 test.txt 所做的更改并保留了这些更改。现在你告诉他(它?)git revert first_commit_id 实际上创建了文件 test.txt。我不能确定,但​​这对我来说似乎是一个小错误。

但是! 如果您在第二次提交中添加文件,例如 test2.txt(因此您将在 git 存储库中有两个文件版本化),那么 revert 可以工作。

【讨论】:

  • :) 谢谢:1)创建了一个 txt 文件/添加/提交 2)在该文件中添加了第二行 / 保存 / 添加 / 提交 3) git revert "first commit" --> got错误:不可能,因为您有未合并的文件 4)我从未更改或创建新分支 5)git status --> 在分支主机上;您当前正在恢复提交 7c319d587。 (修复冲突并运行“git revert --continue”)(使用 git revert --abort 取消恢复操作);未合并的路径:(使用“git reset Heat ..”取消暂存);被他们删除:test.txt;没有添加到提交的更改(使用“git add”和或“git commit -a)
  • 让我将其添加到答案中,这样您就可以确认您确实做到了。我仍然无法复制你所做的。
  • 所以,既然我可以复制它,并考虑这个小错误,我将向 git 维护人员发出错误请求(在检查最前沿的 git 之后),除非你愿意:)
猜你喜欢
  • 1970-01-01
  • 2020-10-25
  • 2019-03-09
  • 2021-04-02
  • 2018-11-22
  • 2018-06-17
  • 1970-01-01
相关资源
最近更新 更多