Git: How to Rename a Git Branch Locally and Remotely

If you made an error while typing the branch name or decided to change the branch name after pushing it to the remote repository, there is a method to rename the branch both locally and remotely.

1. Rename the Local Branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Switch to the branch
git checkout <old_name>
# Rename the local branch
git branch -m <new_name>
# Switch to the branch git checkout <old_name> # Rename the local branch git branch -m <new_name>
# Switch to the branch
git checkout <old_name>

# Rename the local branch 
git branch -m <new_name>

2. Delete the Old Branch Remotely and Push the New Branch Name

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Delete the old branch
# Push the new branch name
git push origin :<old_name> <new_name>
# Delete the old branch # Push the new branch name git push origin :<old_name> <new_name>
# Delete the old branch
# Push the new branch name
git push origin :<old_name> <new_name>

3. Reset the Upstream Branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Reset the upstream
git push origin -u <new_name>
# Reset the upstream git push origin -u <new_name>
# Reset the upstream
git push origin -u <new_name>
Git: How to Rename a Git Branch Locally and Remotely
Be The First

Join our list to get instant access to new articles and weekly newsletter.

Tagged on:         
0 0 votes
Article Rating
Subscribe
Notify of
guest


0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x