I just renamed my local branch using
git branch -m oldname newname
but this only renames the local version of the branch. How can I rename
On the Git branch, run:
git branch -m old_name new_name
This will modify the branch name on your local repository:
git push origin :old_name new_name
This will push the modified name to the remote and delete the old branch:
git push origin -u new_name
It sets the local branch to track the remote branch.
This solves the issue.
Just remove the old branch and create new one.
Example (solely renaming the remote branch):
git push origin :refs/heads/oldname
git push origin newname:refs/heads/newname
You also probably should rename local branch and change settings for where to push/pull.
Another way is to rename the following files:
.git/refs/head/[branch-name]
to .git/refs/head/new-branch-name
..git/refs/remotes/[all-remote-names]/[branch-name]
to .git/refs/remotes/[all-remote-names]/new-branch-name
.Rename head and remotes both on your local PC and on origins(s)/remote server(s).
If your current branch-name contains slashes (/
) Git will create the directories like so:
current branch-name: "awe/some/branch"
.git/refs/head/awe/some/branch
.git/refs/remotes/[all-remote-names]/awe/some/branch
wish branch-name: "new-branch-name"
branch
file from .git/refs/*/awe/some/
..git/refs/head/
.branch
file from all of .git/refs/remotes/*/awe/some/
..git/refs/remotes/*/
.branch
files to new-branch-name
..git/refs/head/new-branch-name
.git/refs/remotes/[all-remote-names]/new-branch-name
awe/some/branch
to new-branch-name
(local and remote!)Information: This way might not be the best, but it still works for people who might have problems with the other ways
You can do that without the terminal. You just need to create a branch with the new name, and remove the old after.
Create a branch
In your repository’s branch selector, just start typing a new branch name. It’ll give you the option to create a new branch:
It’ll branch off of your current context. For example, if you’re on the bugfix branch, it’ll create a new branch from bugfix instead of master. Looking at a commit or a tag instead? It’ll branch your code from that specific revision.
Delete a branch
You’ll also see a delete button in your repository’s Branches page:
As an added bonus, it’ll also give you a link to the branch’s Pull Request, if it has one.
I just copied this content from: Create and delete branches
1. Rename your local branch.
If you are on the branch you want to rename:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
2. Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:
git push origin -u new-name
So the conclusion is:
git branch -m new-name
git push origin :old-name new-name
git push origin -u new-name