in Technology by
How can we rename a local Git branch?

1 Answer

0 votes
by

If you want to rename a branch while pointed to any branch, do:

git branch -m <oldname> <newname>

If you want to rename the current branch, you can do:

git branch -m <newname>

A way to remember this is -m is for "move" (or mv), which is how you rename files. Adding an alias could also help. To do so, run the following:

git config --global alias.rename 'branch -m'

If you are on Windows or another case-insensitive filesystem, and there are only capitalization changes in the name, you need to use -M, otherwise, git will throw branch already exists error:

git branch -M <newname>

Related questions

0 votes
    I don't want to rename a remote branch, as described in the Rename master branch for both local and remote ... and remote branch name Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How to rename and remove branch in remote Git?...
asked Aug 24, 2021 in Technology by JackTerrance
0 votes
    How can we create a new branch on both local and remote?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    How to push a new local branch to a remote Git repository and track it too?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    My git workspace is dirty, there are some local modifications. When I use the command git pull origin master it ... Ansible git module? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How can we create a remote Git branch?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    How can we check out a remote Git branch?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    When you run git fetch from my local repo, will it update your local code and target branch? (1)Yes, the command ... new commits and keep them in a separate branch in local repo...
asked Oct 26, 2020 in Technology by JackTerrance
0 votes
    When you run git fetch from my local repo, will it update your local code and target branch? A. Yes, the command ... new commits and keep them in a separate branch in local repo...
asked Dec 20, 2022 in Education by JackTerrance
0 votes
    I have an old, out-of-sync project code in the master branch. Several files have been deleted, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    How can we force “git pull” to overwrite local files?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    How to Reset local repository branch to be just like remote repository HEAD?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    Can you recover a deleted branch in Git?...
asked Nov 4, 2020 in Technology by JackTerrance
0 votes
    How can we stash only one of multiple changed files on my branch?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    What is a good practice to follow when you want to backup a local branch? A. Push to remote repo B. Move to another PC C. Create another copy D. None of the options...
asked Dec 20, 2022 in Technology by JackTerrance
...