Git Revert "revert is not possible because you have unrelated files"

I made a simple example for the text of some GIT functions. Only one text file in the repository:

1 

added and made a change

then changed txt. file:

 1 2 

added and made a change

Git log gives the following result:

 commit 00e59212a6... .. second commit commit 7c319d587.... .. first commit 

at this point I want to return to the first commit (but I do not want to use "reset --hard". I tried:

 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, you can handle such things very simply, so I wonder if these simple things need to go through a few steps in GIT?

Many thanks for your help.

+5
source share
1 answer

Well, I tried what you think you did, doing 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, everything went well. So I thought, AHA! She / she must have done something else, and neglected to mention it. OK, I’ll still find out what he / she did using my crystal ball. That it laid around is useless.

So merge, git said. Then create a branch and try to combine it with our main branch.

 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 what I knew, and hopefully brought a bit of humor to SO. If this is NOT what you did, could you tell us the exact steps you took? Since I cannot reproduce the error you have, and thus, I feel unfulfilled without being in a state of curse.

EDIT:

Since the OP insists that it definitely did not make the branch ...

 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' 

Ahhh, now I get it! And this ... this is Bizazare.

Ok, I checked and you are actually right, on my git --version 2.6.3 I could reproduce this behavior. In fact, I think you took a rather angular case. It so happened that after your second git commit, you made the saved changes to the test.txt file and saved these changes. Now you told him (is this?) On git revert first_commit_id , which actually created the test.txt file. I can’t say for sure, but this seems like a small mistake to me.

But! If you, in the second commit file, add, for example, test2.txt (so that you will have two files with the version in the git repository), then revert works.

+2
source

Source: https://habr.com/ru/post/1243707/


All Articles