Renaming a branch in GitHub is a common task when you want to change the name of an existing branch to better reflect its purpose or to comply with a new naming convention. Here's a step-by-step guide on how to rename a branch in GitHub, both locally and remotely, using the command line and the GitHub web interface.
Renaming a Branch Locally
Before you proceed with renaming a branch, ensure that you're on the branch you want to rename. You can check your current branch with the following command:
git branch --show-current
Once you've confirmed you're on the correct branch, you can rename it locally using the following command:

git branch -M new-name
Replace new-name with the desired name for your branch. After running this command, your local branch will be renamed, but the change won't be reflected in the remote repository yet.
Pushing the Renamed Branch
To push the renamed branch to the remote repository, you'll need to use the --set-upstream flag with the git push command. Here's how:
git push --set-upstream origin new-name
This command will push the renamed branch to the remote repository and set it as the upstream branch for your local branch.

Renaming a Branch Remotely
If you want to rename a branch directly in the remote repository, you can use the GitHub web interface. Here's how:
- Navigate to your repository on GitHub.
- Click on the Branches tab.
- Find the branch you want to rename in the list and click on the pencil icon next to it.
- Enter the new name for your branch and click Rename branch.
Renaming the branch remotely will update the branch name in the remote repository, but it won't update any local clones of the repository. If you have local clones, you'll need to update them manually, as described in the next section.
Updating Local Clones
After renaming a branch remotely, any local clones of the repository will still have the old branch name. To update the local clones, you'll need to fetch the updated branch names from the remote repository and update your local branches accordingly. Here's how:

- Fetch the updated branch names from the remote repository:
git fetch --all
- Update your local branch to track the renamed remote branch:
git branch -u origin/new-name
Replace new-name with the new name of your branch.
Deleting the Old Branch
After renaming your branch, you might want to delete the old branch to keep your repository organized. Here's how to delete the old branch both locally and remotely:
- Delete the old branch locally:
git branch -d old-name
- Delete the old branch remotely:
- Navigate to your repository on GitHub.
- Click on the Branches tab.
- Find the old branch in the list and click the trash can icon next to it.
Deleting the old branch will help keep your repository clean and organized, but it's important to ensure that no one is still using the old branch before deleting it.
Conclusion
Renaming a branch in GitHub is a straightforward process that can be done both locally and remotely. Whether you're using the command line or the GitHub web interface, the steps outlined in this article will help you rename a branch with ease. By following these best practices, you can keep your repository organized and make it easier for others to understand and contribute to your project.





















