Sobes.tech
Middle

How to rename a branch in git?

sobes.tech AI

Answer from AI

To rename a branch in Git, use the command git branch -m.

If you want to rename the current branch:

git branch -m new_branch_name

If you need to rename another branch without switching to it:

git branch -m old_branch_name new_branch_name

After renaming a local branch, if the branch has already been pushed to a remote repository, you need to update the remote repository:

# Delete the old branch from the remote repository

git push origin --delete old_branch_name

# Push the new branch

git push origin new_branch_name

# If necessary, set the upstream for the new branch

git push --set-upstream origin new_branch_name

Thus, renaming a branch is a local operation, but to synchronize with the remote repository, additional steps are required.