0

Say I have a file that has been added to over time and committed into Git each time. How does one simply revert one of the earlier commits but retain the all the following commits?

For example, a blank file.txt is added and committed. Each subsequent commit adds an increasing number. I wish to simply undo the first number added (1) but retain the others (2 & 3). It seems like I should use revert but I'm a little lost.

➜ ls
file.txt

➜ cat file.txt
1
2
3

➜ git log --oneline
afc6136 Add 3
d0cbec0 Add 2
1d6a4ff Add 1
b6c3a92 Init file.txt

➜ git revert 1d6a4ff
error: could not revert 1d6a4ff... Add 1
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'

➜ cat file.txt
<<<<<<< HEAD
1
2
3
=======
>>>>>>> parent of 1d6a4ff... Add 1
skube
  • 5,867
  • 9
  • 53
  • 77

1 Answers1

0

Good news: you're on the right track!

However, since all your changes were very close to eachother, you get a conflict. In essence, git is saying "too much has changed around here since the commit we're reverting, and I don't feel comfortable guessing -- please tidy things up and tell me when you're done".

Each conflicted chunk is marked as such:

<<<<<<< some commit
(chunk's content in some commit)
=======
(chunk's content in some other commit)
>>>>>>> some other commit

You should edit the chunk to look like you want it, remove the separators (the "<<<", "===", and ">>>" lines), then git add the file and git commit it (as suggested by the message git threw you).

In your case, the whole file is inside the chunk -- that's not always the case. Try with a bigger base file (as in "more lines") in your initial commit, and you'll see what I mean.

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32