firstaidgit icon indicating copy to clipboard operation
firstaidgit copied to clipboard

Feature Request: How to recover a branch when you rename another branch to the same name

Open Razoxane opened this issue 8 years ago • 0 comments

I use Git Flow, and sometimes need to have multiple hotfix branches running concurrently (I know that's not how Git Flow is meant to work, but sometimes you gotta do what you gotta do!) Git Flow doesn't allow you to start a new hotfix while the old one is still open, so I rename the hotfix branch I'm parking, so I can start a new one and come back to this one later.

A number of times I've mistakenly run git branch -m [new_branch_name] and forgotten to provide the second parameter. The result is that whatever branch I'm currently on (usually develop!) gets renamed to [new_branch_name], even if a branch of that name already exists.

There doesn't appear to be an 'undo branch rename' function, and renaming the current branch back to its original name doesn't restore the overwritten one. Also, branch renames don't appear to show up in git reflog, so it can be difficult to know which branch you have just renamed, unless you know all the branches you have open.

So far, my fix has been:

  1. git branch -m develop - Rename the mistakenly renamed branch back to its original name.
  2. git log --all - Slog through the commit logs to find the last commit on the overwritten branch.
  3. git branch [original_branch_name] [sha] - restore the last commit to the required branch name.

I'm lucky that I always tag commits with the issue number I'm working on, and I name my branches with the issue number, so it's easy to see what commit was the last one for that overwritten branch.

Questions:

  • Is there anything I can do to make git branch -m require two parameters, not just one?
    • Perhaps aliases?
  • Is there a better way to revert an accidental branch rename than the process I've outlined above?
  • Is there anything that people who don't have as clear a tagging/naming system can do to easily find the last commit for the overwritten branch name?
  • Is there a way to find out the history of branch names?

Razoxane avatar Aug 24 '16 01:08 Razoxane