Re-open deleted Git branch

Ben picture Ben · Aug 6, 2014 · Viewed 8.6k times · Source

I'm using git flow for my project and I had to make a hotfix branch. When it was completed, I made a pull request to merge it into master and then deleted the branch.

I've since realized that it also needed to be merged into develop. However, I deleted the branch from the remote as well as from my PC. Is there any way I can reopen the branch to make a pull request to merge it to develop?

I'd like to avoid merging master into develop since it also includes other changes that I've made in release branches (bumping version numbers, etc).

Or perhaps there is another, better solution? In general, what is a good practice to use git flow with pull requests?

Answer

Greg Hewgill picture Greg Hewgill · Aug 6, 2014

Deleting a branch just removes the label that was attached to the commit. The commit that used to be the head of the branch is still present. You just need to put the label back.

If you merged hotfix into master and it created a merge commit at aabbcc, then you can resurrect hotfix using:

git branch hotfix aabbcc^2

The ^2 is shorthand for "the second parent". If you didn't create a merge commit, then just re-create hotfix using the last of its commits:

git branch hotfix ddeeff

Of course, if you know ddeeff even in the case of a merge commit above, you can use the last commit directly.