Git – How to Rename a Remote Branch

git

I have created a local branch X that at some point I pushed to remote server (origin)

git push origin X

I realized it's a bad idea to have branch named X and want it to be called Y
The problem is that I've already pushed the branch to the repository.

Is it safe to delete it from the server and push it under a new name like this ?

push origin :X

// 'clone ' the branch under a new name locally ( X-> Y)

push origin Y

What will happen to the people that already fetched from the server. Say that they already
made a branch locally based on the old X name BUT they did not make any changes locally or pushed them to the server for the old X branch.

Best Answer

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.

Related Question