-1

i am extremely new to git and i am exploring git.

This is the chain of commands i typed.

C:\Users\User\Documents\GitHub> cd test
C:\Users\User\Documents\GitHub\test> git init
Initialized empty Git repository in C:/Users/User/Documents/GitHub/test/.git/
C:\Users\User\Documents\GitHub\test [master]> touch 2.txt
C:\Users\User\Documents\GitHub\test [master +1 ~0 -0 !]> git add .
C:\Users\User\Documents\GitHub\test [master +1 ~0 -0]> git commit -a
[master (root-commit) 1af7554] init commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.txt
C:\Users\User\Documents\GitHub\test [master]> notepad .\2.txt
C:\Users\User\Documents\GitHub\test [master]> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   2.txt

no changes added to commit (use "git add" and/or "git commit -a")
C:\Users\User\Documents\GitHub\test [master +0 ~1 -0]> git add .
C:\Users\User\Documents\GitHub\test [master +0 ~1 -0]> git commit -m "changed file 2"
[master 494394c] changed file 2
 1 file changed, 1 insertion(+)
C:\Users\User\Documents\GitHub\test [master]> git log
commit 494394c1c4b5131526ad5137febba54442211303
Author: email
Date:   Tue Jun 14 14:15:37 2016 +0800

    changed file 2

commit 1af755406aad60fb014c496548ea198d59a1af52
Author: email
Date:   Tue Jun 14 14:15:10 2016 +0800

    init commit
C:\Users\User\Documents\GitHub\test [master]> git revert 1af755
error: could not revert 1af7554... init commit
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'
C:\Users\User\Documents\GitHub\test [master +0 ~0 -0 !1 | +0 ~0 -1]> 3b0c5670

I still do not understand why did i get a git revert error

aceminer
  • 4,089
  • 9
  • 56
  • 104
  • 494394 is a commit that depends on 1af755. You cannot revert 1af755 only. When you revert 1af755, 2.txt is to be deleted. However 494394 needs the file to exist. So there is a conflict. You have to make a decision whether to keep the file or to delete it. – ElpieKay Jun 14 '16 at 06:30

1 Answers1

4

Because in commit 1af755 you created 2.txt, reverting the commit would mean that the file gets deleted. However, after that commit you have modified the file 2.txt, and those changes conflict with the deletion of that file. Therefore, you get a conflict, which you need to solve manually.

1615903
  • 32,635
  • 12
  • 70
  • 99
  • Hi please suggest how do i resolve it. I created the file at commit 1af755 but that would also mean if i revert back to this commit. I would get the file in this original state – aceminer Jun 14 '16 at 06:51
  • 1
    I think you have misunderstood what `git revert` does, and what you are trying to do is `git reset` instead. Look up the relevant documentation for both: https://git-scm.com/docs/git-revert and https://git-scm.com/docs/git-reset – 1615903 Jun 14 '16 at 08:22
  • And if you want to resolve the conflict at hand, see http://stackoverflow.com/q/161813/1615903 – 1615903 Jun 14 '16 at 08:23
  • Git reset resets all the commits. But I just want to take a previous commit as a new commit – aceminer Jun 14 '16 at 12:09
  • I don't understand what you mean by _"take a previous commit as a new commit"_. – 1615903 Jun 15 '16 at 05:22
  • Also, git reset does not _"reset all the commits"_, it sets your current branch to point to a specific commit. – 1615903 Jun 15 '16 at 05:24
  • I would like to "revert" the history to a previous commit. Say for example my history is A - > B - > C. I would like to revert to B by having the history now changed to A -> B - > C -> D where D is the same as B – aceminer Jun 15 '16 at 05:39
  • Ok, then you need to revert commit C. That creates a new commit that undoes the changes that you did in commit C. – 1615903 Jun 15 '16 at 05:42
  • Thats what i did but apparently it throws up an error as shown in my question – aceminer Jun 15 '16 at 06:42
  • No, you tried to revert commit B. Type `git revert 49439`, it will accomplish what you are after. – 1615903 Jun 15 '16 at 09:43