Skip to content
Fix Code Error

How do I rename a local Git branch?

March 13, 2021 by Code Error
Posted By: Anonymous

I don’t want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories.

How can I rename a local branch which hasn’t been pushed to a remote branch?

In case you need to rename remote branch as well:
How do I rename both a Git local and remote branch name

Solution

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>
Answered By: Anonymous

Related Articles

  • Why do I have to "git push --set-upstream origin "?
  • Why call git branch --unset-upstream to fixup?
  • Git - Pushing code to two remotes
  • Why does Git say my master branch is "already up to…
  • Git workflow and rebase vs merge questions
  • Git Using Remote Branch
  • Why do git fetch origin and git fetch : behave differently?
  • How to add a jpa repository in bean for spring mvc?
  • How can I find the location of origin/master in git,…
  • Checkout another branch when there are uncommitted…
  • Various ways to remove local Git changes
  • What is "git remote add ..." and "git push origin master"?
  • Rename master branch for both local and remote Git…
  • How do I force Maven to use my local repository…
  • What exactly does the "u" do? "git push -u origin…
  • In plain English, what does "git reset" do?
  • How to recover stashed uncommitted changes
  • How can I reconcile detached HEAD with master/origin?
  • Git merge with force overwrite
  • Change a Git remote HEAD to point to something…
  • Your configuration specifies to merge with the from…
  • How do I make a Git commit in the past?
  • What is a tracking branch?
  • Git submodule update
  • Difference between git checkout --track…
  • moving committed (but not pushed) changes to a new…
  • What are the differences between git remote prune,…
  • How do I delete a Git branch locally and remotely?
  • How to pull in changes from skeleton sub-repository…
  • Git: Create a branch from unstaged/uncommitted…
  • Why does git perform fast-forward merges by default?
  • How do I rename both a Git local and remote branch name?
  • How to create the branch from specific commit in…
  • Setting up and using Meld as your git difftool and mergetool
  • powershell - how to find and rename a specific field…
  • git pull while not in a git directory
  • How do you get git to always pull from a specific branch?
  • Merge, update, and pull Git branches without using checkouts
  • “tag already exists in the remote" error after…
  • How does PHP 'foreach' actually work?
  • Retrieve specific commit from a remote Git repository
  • Check if pull needed in Git
  • Powershell: A positional parameter cannot be found…
  • Vue.js: insert value into textfield but don't bind it
  • git lfs push to github failure on Ubuntu 18.04
  • Git error: src refspec master does not match any
  • Git Push error: refusing to update checked out branch
  • How do I add files and folders into GitHub repos?
  • How does origin/HEAD get set?
  • Git push won't do anything (everything up-to-date)
  • Git, How to reset origin/master to a commit?
  • What is your most productive shortcut with Vim?
  • master branch and 'origin/master' have diverged, how…
  • unable to deploy next js to azure
  • Why did my Git repo enter a detached HEAD state?
  • git bring some "older" changes to a new branch after…
  • How to track untracked content?
  • Autowiring fails: Not an managed Type
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • npm ERR! fetch failed status code 502
  • How do I clone a single branch in Git?
  • Git merge master into feature branch
  • How to merge my branch to master if some other…
  • What's the difference between HEAD^ and HEAD~ in Git?
  • Smart way to truncate long strings
  • How do i update a javascript variable as its value changes?
  • data.table vs dplyr: can one do something well the…
  • git pull from master into the development branch
  • In Git, what is the difference between origin/master…
  • Changing the JFrame title
  • Start redis-server with config file
  • Push git commits & tags simultaneously
  • Why do I need to explicitly push a new branch?
  • Need to reset git branch to origin version
  • Rebasing remote branches in Git
  • Why does git status show branch is up-to-date when…
  • What's the difference between "git reset" and "git…
  • Finding a branch point with Git?
  • Git branching: master vs. origin/master vs.…
  • Git push: "fatal 'origin' does not appear to be a…
  • How to fix Git error: object file is empty?
  • Git merge errors
  • How do you stop tracking a remote branch in Git?
  • How do you push a Git tag to a branch using a refspec?
  • git push says "everything up-to-date" even though I…
  • Why does git say "Pull is not possible because you…
  • The definitive guide to form-based website authentication
  • How to squash commits in git after they have been pushed?
  • How to handle Vue 2 memory usage for large data (~50…
  • How to modify existing, unpushed commit messages?
  • Does "git fetch --tags" include "git fetch"?
  • Git Pull is Not Possible, Unmerged Files
  • git status shows modifications, git checkout --…
  • How do I completely rename an Xcode project (i.e.…
  • How to fetch all Git branches
  • Does pushing a git tag also push the commit?
  • Git reset --hard and push to remote repository
  • How to clone all remote branches in Git?
  • Remote branch is not showing up in "git branch -r"

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I use ‘git reset –hard HEAD’ to revert to a previous commit?

Next Post:

Redirect from an HTML page

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error