撤销 Git 操作时出现“无法撤销,因为您有未合并的文件”的提示

11

我做了一个简单的示例来测试一些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 中是否需要几个步骤才能完成这些简单的事情?

非常感谢您的帮助。


2
似乎有其他东西添加了一个文件(例如编辑器备份文件)。尝试使用 git status 查看 git 认为正在发生什么。 - slothbear
git status --> 在分支 master 上;您当前正在还原提交 7c319d587。(解决冲突并运行“git revert --continue”)(使用“git revert --abort”取消还原操作);未合并的路径:(使用“git reset HEAD <file>..”取消暂存);被他们删除:test.txt;没有更改添加到提交(使用“git add”和/或“git commit -a”)。 - Math_reald
1个回答

4

我已经按照您所说的做了,完全照着您写的去尝试了

这段文字与IT技术无关。
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

正如您所看到的——一切都顺利进行。因此,我想:AHA!她/他一定做了其他的事情,但没有在SO上提及。噢,好吧,我会用我的水晶球找出她/他做了什么。它闲置着还有什么意义呢?

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

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版本2.6.3中,我可以复制这种行为。实际上,我认为您遇到了相当特殊的情况。发生的事情是,在您的第二次提交之后,git存储了您对文件test.txt所做的更改,并保留了这些更改。现在您告诉它(他?)去revert first_commit_id,它实际上创建了文件test.txt。我不能确定,但这在我看来似乎是一个小bug。

然而!如果您在第二次提交中添加文件,例如test2.txt(因此您在git存储库中有两个文件的版本控制),那么revert将起作用。


  1. 创建了一个txt文件/添加/提交
  2. 在该文件中添加了第二行/保存/添加/提交
  3. git revert "first commit" --> 出现错误: 不可能,因为您有未合并的文件
  4. 我从未更改或创建新分支
  5. git status --> 在主分支上; 您当前正在还原提交7c319d587。(解决冲突并运行“git revert --continue”)(使用“git revert --abort”取消还原操作); 未合并的路径:(使用“git reset Heat <file>..”取消暂存); 被他们删除:test.txt; 没有添加更改到提交(使用“git add”和/或“git commit -a”)
- Math_reald
让我补充一下回答,这样您可以确认您确实这样做了。我仍然无法复制您的操作。 - JustMe
所以,既然我可以复制它,并且认为这是一个小错误,除非您想要,否则我将向git维护者发出错误请求(在检查了最新的git之后)。 - JustMe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接