Renaming git branch Local and Remote

git checkout <old_name>
git branch -m <new_name>
git push origin -u <new_name>
git push origin --delete <old_name>
  1. We start by switching to the branch we want to rename
    git checkout <branch_name>
  2. Rename the local branch by
    git branch -m <new_name>
    With this, we have renamed the local branch but in case the branch is already pushed to the remote the following steps are needed to delete it from the remote.
  3. Push the renamed branch <new_name> to the remote and reset the upstream branch
    git push origin -u <new_name>
  4. Finally, delete the old branch<old_name> remote
    git push origin --delete <old_name>
echo "alias <new name>='/home/<full path to script>/rename_git_branch.sh'" >> ~/.bashrc#example: echo "alias gitrename='~/scripts/rename_git_branch.sh'" >> ~/.bashrc

Connect

Email: 24ankitw@gmail.com
Linkedin: https://www.linkedin.com/in/awadhwana/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store