Git Revert“无法恢复,因为您有未合并的文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35588521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git Revert "revert is not possible because you have unmerged files"
提问by Math_reald
I did a simple example to text some GIT functions. Just one text file in the repository:
我做了一个简单的例子来给一些 GIT 函数发短信。存储库中只有一个文本文件:
1
added and commited the change
添加并提交更改
then changed the txt. file to:
然后改了txt。文件到:
1
2
added and commited the change
添加并提交更改
Git log gives the following result:
Git日志给出以下结果:
commit 00e59212a6...
..
second commit
commit 7c319d587....
..
first commit
at that point I want to go back to the first commit (but I dont want to use "reset --hard". I tried:
那时我想回到第一次提交(但我不想使用“reset --hard”。我试过:
git revert 7c319d587
but i got the following error message:
但我收到以下错误消息:
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
I am new to GIT - before I used SVN, there you can handle such things very simple so I am wondering if this simple things need a few steps in GIT?
我是 GIT 的新手 - 在我使用 SVN 之前,您可以非常简单地处理这些事情,所以我想知道这些简单的事情是否需要在 GIT 中执行几个步骤?
Thank you very much for your help.
非常感谢您的帮助。
回答by JustMe
Well, I have tried what You think You did, by doing exactly what You wrote
好吧,我已经尝试了你认为你所做的,完全按照你写的去做
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
as You can see - all went well. Thus, I thought, AHA! She/he must have done something else, and neglected to mention it on SO. Oh well, I'll figure out what she/he did using my crystal ball anyway. What's the point of it laying around useless.
如您所见 - 一切顺利。因此,我想,啊哈!她/他一定做了别的事情,而忽略了在 SO 上提及它。哦,好吧,无论如何,我会弄清楚她/他用我的水晶球做了什么。闲着没用有什么意义。
So, merge, git said. Then, let's make a branch and try to merge it with our master branch.
所以,合并,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!*
Now, that was just me guessing, and bringing a little bit of humor to SO, I hope. If this is NOT what You did, could You please tell us exact steps You did? Since I can't replicate error You are having, and thus I feel unfulfilled not being able to hellp.
现在,这只是我的猜测,并为 SO 带来一点幽默,我希望。如果这不是你所做的,你能告诉我们你所做的确切步骤吗?由于我无法复制您遇到的错误,因此我感到无法满足。
EDIT:
编辑:
Since OP insists that definitely did not make a branch...
由于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'
Ahaaa, now i got it! And that...is bizzaare.
啊哈,现在我明白了!那……是bizzaare。
Ok, I checked and actually You are right, on my git --version
2.6.3 I could replicate this behavior. Actually, I think You went into quite corner case here. What happened is, that after Your second commit, git stored changes You made to file test.txt
and kept those changes. Now You told him(it?) to git revert first_commit_id
which actually created file test.txt. I can't tell for sure but this seems like a small bug for me.
好的,我检查过,实际上你是对的,在我的git --version
2.6.3 上我可以复制这种行为。实际上,我认为你在这里陷入了相当的困境。发生的事情是,在您第二次提交之后,git 存储了您对文件所做的更改test.txt
并保留了这些更改。现在你告诉他(它?)git revert first_commit_id
实际上创建了文件 test.txt。我不能肯定,但这对我来说似乎是一个小错误。
However!If You, in second commit - add file, for example test2.txt
(so You would have two files versioned in git repository), then revert
works.
然而!例如,如果您在第二次提交中添加文件test2.txt
(因此您将在 git 存储库中对两个文件进行版本控制),则revert
可以使用。