About this question
I need to rename a Git branch both locally and remotely. What steps should I follow to ensure the branch is renamed properly in both places?
I need to rename a Git branch both locally and remotely. What steps should I follow to ensure the branch is renamed properly in both places?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask DevOps Expert
Answered on Jan 14, 2025
Renaming a Git branch both locally and remotely requires a few steps to ensure that the branch name is updated correctly in both locations. Here’s a step-by-step guide:
Renaming a Local Branch:
Switch to a Different Branch:
First, ensure you're not on the branch you want to rename. Switch to another branch:
git checkout main # or any other branch
Rename the Local Branch:
Use the following command to rename your local branch:
git branch -m old-branch-name new-branch-name
Renaming the Remote Branch:
Delete the Old Remote Branch:
Push the new local branch to the remote repository and delete the old remote branch:
git push origin
ld-branch-name
Push the Renamed Local Branch:
Push the renamed local branch to the remote:
git push origin new-branch-name
Reset the Upstream Branch (Optional):
If you want to set the upstream tracking branch for the new branch, use:
git push --set-upstream origin new-branch-name
Clean Up (Optional):
If others are working on the same repository, let them know about the branch rename. They’ll need to fetch the new branch and delete the old one locally:
git fetch --prune
git branch -d old-branch-name # deletes the local reference to the old branch
By following these steps, the branch will be renamed both locally and remotely, and the changes will be reflected across all users working on the repository.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common DevOps interview questions, answered
Guides, tips and career advice on DevOps from JanBask experts.
DevOps How to Become a DevOps Engineers: Learning Path, Critical skills & More
Looking for answer to how to become a DevOps engineer in 2025? Here is the detailed article about DevOps career path, DevOps…
DevOps Top 50+ Jenkins Interview Questions And Answers For 2025
Here are the Top 50 Jenkins interview questions frequently asked in top MNCs which will help you land an ideal job! These…
DevOps Best 37 DevOps Tools To Learn And Master In 2025: Docker, Git & More
Want to know what are Devops tools and their uses? Or looking for the best DevOps list for automation, monitoring, & development?…
DevOps DevOps Engineer Job Description: Roles and Responsibilities [2025 Updated]
Discover the essential roles and responsibilities of a DevOps Engineer by exploring the detailed DevOps job description, and…