0

I have three commits and I tried reverting back to the first commit. In doing that, Git removed two files and I don't know why as they were in the first commit I tried reverting to. If the two files Git removed were in the original commit, why did it remove them?

This is the code I used:

git revert <commit id>

and this is the error message:

    Removing style.css
CONFLICT (modify/delete): pages/services.html deleted in (empty tree) and modified in HEAD. Version HEAD of pages/services.html left in tree.
Removing index.html
error: could not revert 9b23173... inital 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'
  • `git revert` reverts a commit, not to a commit. I assume you meant to revert the other commits instead. See also https://stackoverflow.com/a/4114122 – Hasturkun Jan 05 '21 at 16:10
  • 1
    `git revert` is for reverting *that commit* itself, not "reverting *to* the commit". You're looking for `git reset --hard` or such. – iBug Jan 05 '21 at 16:10
  • Ah okay, but I want to reset to the old commit while keeping the history just in case I want to go back again, would I still use reset --hard? –  Jan 05 '21 at 16:13
  • You can revert a range of commits. You can for example do a `git revert HEAD HEAD~1` to revert the current commit and the previous one. You can also use commit ranges, etc. – Leonardo Dagnino Jan 05 '21 at 16:23
  • 2
    Then you should use `git checkout` to view the repository state at the commit, while leaving your history intact (where branches are, etc.) – iBug Jan 05 '21 at 16:27
  • So once I checkout and commit the old commit to the HEAD, do I then just push it to the remote repository to make the updated head the origin/master too? –  Jan 05 '21 at 16:39
  • Linus Torvalds chose the wrong English-language verb for this command. Think of `git revert` as meaning *back out*, not *revert to*, and you'll be in better shape. Mercurial got this one right, as its verb is `hg backout`. – torek Jan 06 '21 at 07:37

1 Answers1

1

If what you want to take back the content of the project to a certain revision while keeping the later history, it can be done like this:

git checkout the-revision-id
git reset --soft the-branch-I-want-to-take-back
# at this point, all changes from the branch you want to take back and the revision you want to go to are in index, ready to be committed
git commit -m "taking everything back in time"
# if you like the result, move the pointer of the branch
git branch -f the-branch-I-want-to-take-back
git checkout the-branch-I-want-to-take-back
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • So checkout grabs the old commit and reset --soft resets the HEAD to that commit, but if all these commits were in the master branch, would it be master or this isn't best practice? –  Jan 05 '21 at 17:36
  • the thing is that in the end you create a _single_ revision **on top of** the branch that you want modify... so it's not like you are hacking something.... it's just a simple way to get the content you want so I don't see how you could be breaking something. – eftshift0 Jan 05 '21 at 17:42