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' 
7
  • git revert reverts a commit, not to a commit. I assume you meant to revert the other commits instead. See also stackoverflow.com/a/4114122 Commented Jan 5, 2021 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. Commented Jan 5, 2021 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? Commented Jan 5, 2021 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. Commented Jan 5, 2021 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.) Commented Jan 5, 2021 at 16:27

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.