2016-02-23 198 views
5

我做了一个简单的例子来文本一些GIT函数。在仓库中只有一个文本文件:Git恢复“恢复是不可能的,因为你有未合并的文件”

1 

添加COMMITED变化

再变TXT。文件到:

1 
2 

添加COMMITED变化

的Git日志提供了以下结果:

commit 00e59212a6... 
.. 
second commit 

commit 7c319d587.... 
.. 
first commit 

在这一点上我想回到第一次提交(但我不想我试过:

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

+0

git status - >在分支主机上;您正在恢复提交7c319d587。 (修复冲突并运行“git revert --continue”)(使用git revert --abort取消恢复操作); unmerged paths :(使用“git reset Heat ..”来取消);由他们删除:test.txt;没有更改添加到提交(使用“git add”和或“git commit -a) –

回答

2

好了,我已经试过你认为你做,做什么你写

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!* 

现在,这只是我的猜测,并带来了一点幽默感,所以我希望。如果这不是你所做的,你能告诉我们确切的步骤吗?由于我无法复制您正在使用的错误,因此我觉得无法完成。

编辑:

由于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 <[email protected]> 
# Date: Tue Feb 23 23:31:28 2016 +0100 
# 
#  second 
# 
# commit 22873fb5fbf06d80a1779fe6740060c660d68714 
# Author: ME <[email protected]> 
# 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,现在我知道了!而那.​​.....是比萨饼。

好的,我检查了一下,你是对的,在我的git --version 2.6.3我可以复制这种行为。其实,我认为你在这里进入了一个非常角落的案例。发生了什么事是,在你的第二次提交后,git存储了你对文件test.txt所做的更改并保留了这些更改。现在你告诉他(它?)到git revert first_commit_id哪个实际上创建了文件test.txt。我无法确定,但这对我来说似乎是一个小错误。

但是!如果你在第二次提交 - 添加文件,例如test2.txt(所以你会有两个文件版本在git仓库中),然后revert工程。

+0

:)谢谢:1)创建了一个txt文件/ added/committed 2)向该文件添加了第二行/ saved/added/commited 3)git恢复“第一次提交” - >有错误:是不可能的,因为你有未合并的文件4)我从来没有改变或创建一个新的分支5)git状态 - >在分支大师;你目前正在恢复提交7c319d587。 (修复冲突并运行“git revert --continue”)(使用git revert --abort取消恢复操作); unmerged paths:(使用“git reset Heat ..”来停用);由他们删除:test。 txt;没有更改添加到提交(使用“git添加”和或“git提交-a) –

+0

让我只是添加回答,所以你可以证实你做到了这一点。我仍然无法复制你所做的。 – JustMe

+0

因此,因为我可以复制它,并考虑这个小错误,所以我会为git维护者发出bug请求(在检查出血边缘的git之后),除非你想:) – JustMe