How to rename remote branch in Git

Git is known for its confusing and difficult commands. If you want to rename a remote branch, say a branch that is on Github, from the comfort of your shell then you are in luck for one such experience! 🙂

This is the invocation to rename a branch old_branch on a remote named foo_remote to a new branch name new_branch:

$ git push foo_remote foo_remote/old_branch:refs/heads/new_branch :old_branch

If you find yourself renaming remote branches a lot, then it is better to add this as a Git command. To do that, save this piece of shell script code as a file named git-rename-remote-branch:

The file can be named anything as long as it begins with a git- prefix. Make it executable and place it in any directory that is in your PATH.

Now, you can rename a remote branch with a sane command:

$ git rename-remote-branch foo_remote old_branch new_branch

Tried with: Git 2.7.4 and Ubuntu 14.04

Advertisements

One thought on “How to rename remote branch in Git

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s