When working with Git in projects, sometimes you will need to change the branch name you created in Git repository.
For example, I have a repository on GitHub named questions-management and this repository has a branch named example as follows:
Now I will rename this branch to the new name, example_huongdanjava!
To do this, I will first clone this remote repository to local, then use some Git commands as follows:
– First, I will switch the current branch of this repository to branch example using the “git checkout” command:
1 |
git checkout -b example |
– Now we will use the “git branch” command to rename the current branch to the new name, example_huongdanjava:
1 |
git branch -m example_huongdanjava |
– Next, we need to run the following command to delete this branch example on remote and push the branch with the new name from local to remote:
1 |
git push origin :example example_huongdanjava |
– And the last thing we need to do is reset upstream to local branch track remote branch for branch example_huongdanjava.
1 |
git push origin -u example_huongdanjava |
Result: