rename git branches

Let’s rename a git branch locally & remotely.

Let’s assume that you got git repository with multiple branches and you want to mark a branch as a master whereas existing master to something else.

  • Make note of the branch names and their respective remote
git branch -vv
  • Goto server repo and switch default branch
git symbolic-ref HEAD refs/heads/new-master
  • Goto local repo, checkout master
git checkout master
  • Rename it locally
git branch -mv master something
  • Remove remote branch
git push origin --delete master
  • Set new remote
git branch --unset-upstream
git push --set-upstream origin something
  • Goto server repo and switch default branch
git symbolic-ref HEAD refs/heads/something
  • Switch to branch which is going to be new master
git checkout new-master
  • Rename this branch
git branch -mv new-master master
  • Remove remote branch
git push origin --delete new-master
  • Set new remote
git branch --unset-upstream
git push --set-upstream origin master
  • Goto server repo and switch default branch to master
git symbolic-ref HEAD refs/heads/master
Advertisements

Git — Change commit author name/email

These commands are tested with git version 2.7.4.

GIT rebase command can be used to change git author name & email address but be careful that it will change commit id so if someone else is using it it will create problem.

git rebase -i -p < commit id of the one commit before your bad commits>

Here

-i is for Interactive mode as we want to deal each commit 
-p is to maintain the git tree otherwise git will flatten the git history and you won't be able to identify the point where git merge is carried out.

Change first column of the all the commit ids exposed via rebase to have “edit” command. Once you have typed edit to all required commits, exit from editor and issue

git rebase --continue

As you are only interested to change author name/email, you can use

git commit --amend --author "Correct Author <author@author.com>" --no-edit

Keep on issuing these commands alternatively until git rebase is not finished. Once it is done you will find new author name/email.

In case you are interested to push these new commands, use following: Be careful as we are deleting remote branch.

git push origin --delete branch-name
git push origin branch-name