Branching in Git allows you to diverge from the main line of development and continue to work independently without affecting other branches. This guide provides a comprehensive overview of how to change branches in Git, including changing branch names both locally and remotely.
What is a branch in Git?
In Git, a branch represents an independent line of development within a repository, allowing you to diverge from the main project without affecting ongoing work. Each branch in Git encapsulates its own set of file modifications, commits, and subsequent history, distinct from other branches until merged. This feature is crucial for developers to experiment with new features, fix bugs, or safely integrate and test new code before committing it to the main project line.
Technically speaking, each branch is a lightweight movable pointer to a specific commit. When you create a new branch, Git simply creates a new pointer; it does not duplicate any files or directories. These pointers allow developers to switch between different contexts within the same repository.
How to change branches
Checking your current branch
Before changing branches, it's helpful to know which branch you're currently on. To see your current branch and all other branches that exist in your repository, you can use the following command:
git branch
The branch you are currently on will be marked with an asterisk (*).
Changing branches
To change branches, you use the git checkout
command, which will switch you from your current branch to another branch. If the branch does not exist locally, Git will try to find a matching branch in your remote repositories:
git checkout <branch-name>
Replace <branch-name>
with the name of the branch you want to switch to.
Example:
Suppose you are currently on the main
branch and want to switch to a branch named feature-login
. You would enter the following command in your terminal:
git checkout feature-login
This command will switch the current workspace to the feature-login
branch, updating the working directory to match the snapshot of that branch.
Creating a new branch and switching to it
If you need to create a new branch and immediately switch to it, you can use the -b
option with git checkout
:
git checkout -b <new-branch-name>
This command creates a new branch based on your current branch and switches to it.
How to change branch names in Git
Changing a branch name locally
If you need to rename a branch locally, you can use the git branch -m
command:
git branch -m <old-branch-name> <new-branch-name>
This is useful when you want to correct a typo in a branch name or update the branch name to reflect a change in the scope or direction of the feature you are working on.
Example:
To rename a branch from feature-lgooin
to feature-login
, you would use the following command:
git branch -m feature-lgooin feature-login
Changing a branch name remotely
Rename the branch locally: Use the
git branch -m
command to rename the branch locally. This changes the branch's name locally but does not affect the remote branch until you push the changes.Terminalgit branch -m <old-branch-name> <new-branch-name>Push the renamed branch to the remote repository: After renaming the branch locally, push the new branch to the remote repository. This step uploads the new branch to the remote server.
Terminalgit push origin <new-branch-name>Update the upstream tracking branch: You must reset the upstream branch for your local branch to track the new remote branch.
Terminalgit push --set-upstream origin <new-branch-name>Delete the old branch from the remote repository: To complete the renaming process, you should remove the old branch from the remote repository to avoid confusion and ensure that others do not continue to use it.
Terminalgit push origin --delete <old-branch-name>
This approach simplifies the process and correctly handles the renaming of a branch both locally and remotely, ensuring that the new branch name is tracked and the old branch is cleaned up from the remote repository.
For further reading on changing Git branches, see the official Git documentation.