undo feature branch merge
Solution 1:
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