How to rename a local Git branch?

0

 If you are working in any software project then you must be knowing about the git. There are many advantages of git in maintaining the version control system. There are so many commands to clone the directory, to make any changes in the directory, to push some code, to pull some code .

While working on the local system you may have faced this problem which is how to rename a local git branch . Below there are multiple commands that can be used in renaming the dir.

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>

If you want to push the local branch and reset the upstream branch:


git push origin -u <newname>

And finally if you want to Delete the remote branch:


git push origin --delete <oldname>

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>

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !