S. Lee
S. Lee

Reputation: 11

Another option for Git revert

I am having an issue where I am trying to use git revert to create a new commit that essentially duplicates an old commit a few steps back in the master branch. I've been stuck on this problem for quite a while as every time i try to run git revert <sha> i first get this error:

error: Your local changes would be overwritten by revert.
hint: Commit your changes or stash them to proceed.
fatal: revert failed`

I tried to add, commit, and push and try again but I get the same error and it keeps having me merge the same conflicts over and over. I tried git stash, but still no luck.

git stash
Saved working directory and index state WIP on (no branch): <branch>
git revert <sha>
error: could not revert <sha> 
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'`

At this point, I would just like to clone the repository at the commit I want as the current and add it as the head. I am trying to retain the history of my commits otherwise I would use git checkout.

Any sugggestions would be greatly appreciated!

Upvotes: 1

Views: 6906

Answers (2)

Tobias Kienzler
Tobias Kienzler

Reputation: 27393

Since you state duplication of a commit in master, I assume you're working on a different branch and your history looks something like this:

A-B-C-D   master
 \E-ζ-F*  dev

where ζ is the almost-duplicate of C and F* are some following commits plus a dirty work-tree. Since you mention "it keeps having me merge the same conflicts over and over" maybe you tried merging master into dev, which might result in a conflict due to ζ and C not being actual duplicates - maybe some comments added conflict etc.

So my best guess is you'd actually like to have something like

A-B-C-D      master
 \    ↓
  E--D'-F'*  dev  (or maybe E--F--D'*)

i.e. merge master onto dev without the almost-duplicate commit ζ.

You already did git stash to get rid of the dirty state. If you're the only user who pulled your branch so far, you can consider rewriting history via git rebase -i E and simply purging ζ from history, to obtain a temporary branch E-F. Then git merge master to obtain E-F-D' and optionally git rebase -i E again to swap F and D'. Finally, git stash pop to get back your worktree, hopefully without any merge conflicts.

If rewriting history is not an option to you, you can git merge master directly to obtain E-ζ-F-D' after fixing the merge conflict. No need to revert the change, but have the merge comment include information about this, and maybe add a git note to ζ.

Upvotes: 1

RudyOnRails
RudyOnRails

Reputation: 1937

Similar to what @phd said, you have two different errors. The 1st made sense because you had uncommitted changes. The 2nd did not make sense to your intentions, because you were in the middle of a merge conflict and not on a branch. Not sure if you are still in the situation, but I would have ran a git merge --abort then git stash then git revert <sha> and you should be golden.

Upvotes: 1

Related Questions