Skip to content

How to Rename a Local and Remote Git Branch

Haikel Fazzani Haikel Fazzani
2025-02-05

Branches in Git allow developers to work on new features or bug fixes in isolation without affecting the main codebase. However, sometimes a branch name might become outdated, contain typos, or simply no longer accurately reflect the work being done. Renaming a branch ensures clarity and maintainability within your project.

Step 1: Renaming a Local Git Branch

Renaming a local branch is straightforward and typically the first step in the process.

Renaming the Current Branch

If you are currently checked out on the branch you wish to rename, use the following command:

git branch -m <new-branch-name>

For example, if you are on the feature/login branch and want to rename it to feature/authentication:

git branch -m feature/authentication

Renaming a Different Branch

If you are currently on a different branch (e.g., main or develop), you can rename the target branch using the following syntax:

git branch -m <old-branch-name> <new-branch-name>

Example:

git branch -m old-feature-name new-feature-name

Step 2: Synchronizing with the Remote Repository

Renaming a local branch does not automatically update the remote repository. This is the most crucial part of the process, especially for DevOps engineers managing CI/CD pipelines and collaborative environments.

To update the remote repository, you need to perform three actions: delete the old remote branch, push the new local branch, and set up tracking.

2.1: Delete the Old Remote Branch

First, delete the outdated branch from the remote repository. This requires forcing the deletion using the -d flag, specifying the remote (usually origin) and the old branch name.

git push origin --delete <old-branch-name>

2.2: Push the New Local Branch to the Remote

Now, push your newly renamed local branch to the remote repository.

git push origin <new-branch-name>

2.3: Reset Upstream Tracking

Finally, you need to tell your local repository to track the new remote branch. This ensures that git pull and git push commands work correctly without requiring you to specify the remote branch every time.

git push origin -u <new-branch-name>

Alternatively, you can use the following command if you prefer the git branch --set-upstream-to syntax:

git branch --set-upstream-to=origin/<new-branch-name> <new-branch-name>

A Complete Workflow Summary

For a clear and concise workflow, here is the complete sequence of commands:

  1. Rename the local branch: git branch -m <new-branch-name>

  2. Delete the old branch on the remote: git push origin --delete <old-branch-name>

  3. Push the new branch to the remote: git push origin <new-branch-name>

  4. Set up the upstream tracking for the new branch: git push origin -u <new-branch-name>

DevOps Considerations and Best Practices

As a DevOps expert, renaming branches has implications for your automated workflows and CI/CD pipelines.

Conclusion

Renaming a Git branch is a routine operation in software development. By following these steps, you can ensure a smooth transition from the old branch name to the new one, both locally and on your remote repository. A clean and consistent Git history is essential for efficient collaboration and reliable DevOps practices. Mastering these commands will contribute significantly to maintaining a well-organized and professional codebase.

Git rename remote branch Rename a local remote git branch github Git rename remote branch without deleting Rename remote branch GitHub Rename local branch GitLab rename branch Git delete remote branch Bitbucket rename branch

More Insights.