how to finish reverting a commit, and how to revert a lot of commits

Viewed 21076

This has to be simple, but I can't find it in git-scm.

I've been making a lot of small commits to a public project, and all of my work is bad. I want to remove everything that I've done. Some I've just committed locally, some I've pushed to 'origin master'.

The first commit (a week ago) is bdbad86... with the most recent being e82401b...

I want to just make all these go away. I've tried to revert one.

git status  
# On branch master  
# You are currently reverting commit e82401b.  
#   (all conflicts fixed: run "git revert --continue")  
#   (use "git revert --abort" to cancel the revert operation)  
  1. I can't figure out how to finish this reverting.
  2. I don't want to have to do each commit separately, I want to blow them all away.
3 Answers

Instead of running

git revert --continue

Just run

git commit -m "commit message here"

If that doesn't work, make sure you've staged all the files involved in the revert. Even the files that you reverted and then discarded changes to.

This question is already answered correctly, but I just want to address the first point of op's question -- I can't figure out how to finish this reverting. if you too were stuck in revert state like me and got here searching for an answer.

it can be straightforward as git revert --continue. but only if you're reverting some changes inside a file. if you get stuck in revert state, that's most likely because you're trying to revert a commit that created a file and that file is modified in any of the subsequent commit.

take an example:

1c40cd3 added fileB
4e06828 added fileA
faf29dd (origin/main, origin/HEAD, main) Merged in feature5 (pull request #13)
6e735e3 (origin/feature5, feature5) updated file.txt
fb51fca added file.txt
744ba74 Initial commit

now, if you're trying to revert fb51fca (added file.txt), it will show you something like:

$ git revert fb51fca
CONFLICT (modify/delete): file.txt deleted in parent of fb51fca (added file.txt) and modified in HEAD.  Version HEAD of file.txt left in tree.
error: could not revert fb51fca... added file.txt
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".

$ git status
On branch feature6
You are currently reverting commit fb51fca.
  (fix conflicts and run "git revert --continue")
  (use "git revert --skip" to skip this patch)
  (use "git revert --abort" to cancel the revert operation)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add/rm <file>..." as appropriate to mark resolution)
        deleted by them: file.txt

you can clearly see file.txt deleted in fb51fca (added file.txt) and modified in HEAD. Version HEAD of file.txt left in tree.. now, you can do git add -A and git commit ... BUT

what revert does is goes back in history (in the specified commit), removes the changes that were done in that commit. and makes a new commit. but the issue is, you created a file, now revert is deleting it. in the end, there is no file left to stage and as you know, git doesn't allow empty directories or empty commits.

but there's a workaround for empty commits. as ryan suggested, you can do git commit -m "empty commit to escape revert" --allow-empty. git revert probably doesn't use this --allow-empty flag and that's probably the reason it can't do a commit gets stuck in revert state.

either way, if you get stuck in the revert state, you're probably doing something wrong. trying to revert something that doesn't make sense.

Related