Reputation: 2823
I've been working on a new feature on a branch for the last while. When the feature was finished, I squash-merged the feature branch back to master, and deleted the feature branch. The hash of the merge commit is 7ba3dc2fea43cd33c4d5ba11baf2765cbbdb0284.
I now need to revert this merge. Specifically, what I want to achieve is:
Some additional information which may be relevant:
How can I move the changes for the new feature back to a feature branch?
Upvotes: 0
Views: 364
Reputation: 774
You can try the below approach
Revert a commit
$ git revert --no-commit '<commitnumber>'
# Files that were modified in the commit will be changed in your working directory
$ git commit -a -m "Revert commit <commit number>"
Create a feature branch and cherry pick commit
$ git checkout -b 'Feature Branch name'
$ git cherry-pick '<commitnumber>'
$ git commit
Most of the time you would end up with some conflicts based on the number files which have been changed in the git repo in subsequent commits
Upvotes: 0
Reputation: 97718
For once, the fact that you used a squash merge rather than a true merge is going to make your life easier. (I'm assuming that by "squash-merge", you mean the --squash
argument to git merge
, or equivalent functionality in other UIs.)
A "squash merge" doesn't actually reference the original commits in any way, it just creates a new commit which applies all the changes they would have brought in. So to undo that commit, you can create a new branch from master, and use git revert to create a new commit that undoes all the changes from the squash merge commit.
To get the old branch back, you need to find the commit hash it was pointing at when you deleted it. If you used a "Pull Request" or "Merge Request" in a web UI (Github / Gitlab / BitBucket), it will be listed there (and might even have a "restore branch" link). If you had it checked out locally, it will probably be visible in git reflog
. Once you've found it, just type git branch feature-foo-resurrected abc123def
, replacing "abc123def" with the commit hash you've found.
As a last resort, you could create a branch based on the squash commit, by using git cherry-pick to create a new commit with the same content. (Just pointing a branch at that commit won't help, because that's already in the history of "master", so will show as "nothing to merge".)
So the whole sequence might look like:
git switch master
git pull --ff-only origin
# create a branch which undoes the squash merge
git switch -c revert-feature-foo
git revert 7ba3dc2fea
git push origin
# now create a branch which *redoes* the squash merge on top of that
# only needed if you can't find the original feature branch
git switch -c feature-foo-resurrected
git cherry-pick 7ba3dc2fea
git push origin
Upvotes: 1