Skip to content

How to Rename a Git Branch Locally and Remotely

Rename the current or another local Git branch, push the new remote branch, set upstream tracking, and safely delete the old remote branch.

Published Updated

Rename the current branch with git branch -m new-name. Then publish it with git push -u origin new-name and, after collaborators have switched, remove the old remote name with git push origin --delete old-name.

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 branchRename a local remote git branch githubGit rename remote branch without deletingRename remote branch GitHubRename local branchGitLab rename branchGit delete remote branchBitbucket rename branch