2016-02-23 197 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