other people will keep a pointer to branch X (as origin/X
) in their repository until they run git remote prune origin
. it's left as a stale branch
when fetching/pulling they will get a second point to (newly created) branch Y (as origin/Y
)
branching and merging is not affected by this.
so, as long as all commits are still reachable from your branch, renaming is fine.
As mentioned, delete the old one on GitHub and re-push, though the commands used are a bit more verbose than necessary:
git push origin :name_of_the_old_branch_on_github
git push origin new_name_of_the_branch_that_is_local
Dissecting the commands a bit, the git push
command is essentially:
git push <remote> <local_branch>:<remote_branch>
So doing a push with no local_branch specified essentially means "take nothing from my local repository, and make it the remote branch". I've always thought this to be completely kludgy, but it's the way it's done.
As of Git 1.7 there is an alternate syntax for deleting a remote branch:
git push origin --delete name_of_the_remote_branch
As mentioned by @void.pointer in the comments
Note that you can combine the 2 push operations:
git push origin :old_branch new_branch
This will both delete the old branch and push the new one.
This can be turned into a simple alias that takes the remote, original branch and new branch name as arguments, in ~/.gitconfig
:
[alias]
branchm = "!git branch -m $2 $3 && git push $1 :$2 $3 -u #"
Usage:
git branchm origin old_branch new_branch
Note that positional arguments in shell commands were problematic in older (pre 2.8?) versions of Git, so the alias might vary according to the Git version. See this discussion for details.
Best Answer
To rename the current branch:
To rename a branch while pointed to any branch:
-m
is short for--move
.To push the local branch and reset the upstream branch:
To delete the remote branch:
To create a
git rename
alias:On Windows or another case-insensitive filesystem, use
-M
if there are only capitalization changes in the name. Otherwise, Git will throw a "branch already exists" error.