Have you ever created a Git local branch and realized that you made a typo in its name or forgot to include the ticket number? Fortunately, Git provides an easy way to rename branches using the --move option. In this article, we'll walk you through how to use this option to rename your Git local branches with some practical examples..
Fix Example 1: Renaming the current branch using --move flag
Say you had created a branch with the name - "fix-for-alignmetn" as there is a typo in it you want to change it to "fix-alignment"
Git Command Example:# git branch --move fix-for-alignmetn fix-alignment
As you can see we have made use of the "git branch" command followed by the --move flag and then the old branch name followed by the new branch name.
Fix Example 2: Renaming the current branch using -m flag
Git Command Example:# git branch -m fix-alignment
You can also make use of the shorthand version of the move flag with is -m
Note: If you are renaming the current branch, you do not need to provide the old branch name.
Fix Example 3: Rename a branch that is not current.
Say you are now pointing to the main/master branch and want to rename a different local branch, you can make use of the same syntax as you saw in example 1.
Renaming the branch can help keep your codebase organized and make it easier to track changes.
Be cautious while changing the master branch nameChanging the name of a branch like master/main/mainline/default will break the integrations, services, helper utilities and build/release scripts that your repository uses. Before you do this, make sure you consult with your collaborators. Also, make sure you do a thorough search through your repo and update any references to the old branch name in your code and scripts.
Read - https://git-scm.com/book/en/v2/Git-Branching-Branch-ManagementYou can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Change the default git branch name from master to main
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- How to Install Git on Windows
- Setup Git + Visual Studio Code Tutorial
- How to undo last Git Commit on Local Repository?
- Install GitHub Command Line Tool on Mac
- How to Create a Git Branch in 6 Different ways
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Git Config Command - A Deep Dive
- Step-by-Step: How to delete a git branch from local as well as remote origin
- How to Add Git Bash Option to Windows Terminal List
- Command to Clone Repository Using Git Bash
- How to remove or unstage a file from git staged area
- Get the file location of git config values
- GitHub: How to Search Code
- How to check your installed version of Git
- Fix: error: src refspec master does not match any failed to push some refs to Git
- Git: Step-by-Step - How to Push Local Brach to GitHub
- How to Merge Branch into Master Branch
- fix fatal: --local can only be used inside a git repository error
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken
- Change the default diff or commit editor for git
- Git Fix: fatal: refusing to merge unrelated histories Error
- How to install Roblox Game on Mac [Intel/M1/M2] - MacOS
- Docker - Incompatible CPU detected - M1/M2 Mac (macOS Sonoma) - Docker
- [Fix] Microsoft Teams Error code - 2:-1012 - Teams
- 28: Program to Lowercase a String in Python - Python-Programs
- How to Enable Cookies in Safari on Mac - MacOS
- Bash - How to check if a Command Failed? - Bash
- Remove all stopped containers in Docker using prune command - Docker
- Spring Boot + Redis Cloud Configuration and Setup Tutorial - Java