Rename branch in Git repository

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:

Rename branch in Git repository

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:

– Now we will use the “git branch” command to rename the current branch to the new name, 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:

– And the last thing we need to do is reset upstream to local branch track remote branch for branch example_huongdanjava.

Rename branch in Git repository

Result:

Rename branch in Git repository

Add Comment