Skip to content

Instantly share code, notes, and snippets.

@topmangarbuja
Last active May 3, 2024 06:28
Show Gist options
  • Save topmangarbuja/243efd86a1b49a59e5e63d4087d7dfc5 to your computer and use it in GitHub Desktop.
Save topmangarbuja/243efd86a1b49a59e5e63d4087d7dfc5 to your computer and use it in GitHub Desktop.

How to rename branch

Step 1: rename local branch

If you are in different branch other than old-branch,

git branch -m <old-branch> <new-branch>

eg. git branch -m load_data feature/load_data

If you are in the old-branch,

git branch -m <new-branch>

eg. git branch -m feature/load_data

Step 2: delete remote old branch

git push <remote> -d <old-branch>

e.g. git push origin -d load_data

Step 3: unset the old upstream name of the new branch in local

git branch --unset-upstream

Step 4: push new branch to remote

git push

NOTE: your git config needs to have the following setting =>
      push.default = true
      or
      push.autosetupremote = true

Note

Instead of Step 3 and 4, you can run the following command directly

git push <remote> -u <new_branch>
or
git push <remote> --set-upstream <new_branch>

e.g. 
git push origin -u feature/load_data
or
git push origin --set-upstream feature/load_data

 -u or --set-upstream tells Git to set the upstream branch for the current local branch to the specified new remote branch.
 This set up a tracking relationship between the current location branch and the remote branch you're pushing to.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment