Git is an essential tool for version control in modern software development, and managing branches is a crucial part of any Git workflow. Whether you’re collaborating with a team or working on multiple features in a project, switching between branches efficiently is important for maintaining productivity.
Original source of this article can be found on AxxellanceBlog @ How to change the git branch on a project using terminal
In this blog post, we’ll walk you through the process of changing the Git branch on a project using the terminal. You’ll learn how to use the git checkout
and git switch
commands, as well as some tips to ensure smooth switching between branches.
Why Use Git Branches?
Before we dive into the specifics of switching branches, it’s important to understand why branching is so critical in Git:
- Isolated Development: Git branches allow you to work on features, fixes, or experiments in isolated environments, ensuring that changes don’t interfere with the main project.
- Collaborative Workflows: Branching allows multiple developers to work on different parts of the project simultaneously without conflicts.
- Version Control: Branches help manage versions of your codebase and facilitate easier rollbacks or merging of changes.
1. Check Current Branch
Before switching to a different branch, it's useful to first check which branch you're currently working on. You can do this with the following command:
git branch
This command will display a list of all branches in your repository. The branch you are currently on will be highlighted with an asterisk (*
).
2. Switching Between Branches with git checkout
The most commonly used command for switching branches in Git is git checkout
. This command allows you to switch to an existing branch or create a new branch.
Switch to an Existing Branch
To switch to an already existing branch, simply run:
git checkout <branch-name>
For example, if you want to switch to a branch named feature/login
, you would run:
git checkout feature/login
Git will automatically switch to the specified branch, and your working directory will be updated with the content of that branch.
Create and Switch to a New Branch
If you want to create a new branch and switch to it at the same time, use the -b
flag:
git checkout -b <new-branch-name>
For example, to create and switch to a new branch called feature/signup
, run:
git checkout -b feature/signup
This command creates the new branch and switches you to it, making it easy to start working on new features or fixes right away.
Check the Switch
Once you’ve switched branches, you can use the git branch
command again to confirm you’re on the correct branch. The new branch will be highlighted with an asterisk (*
).
3. Using git switch
(New and Recommended)
In recent versions of Git, the git switch
command was introduced to simplify branch switching. The git checkout
command is versatile, but git switch
is specifically designed to handle tasks related to branch switching.
Switch to an Existing Branch
To switch to an existing branch with git switch
, use the following command:
git switch <branch-name>
For example:
git switch feature/login
This will switch to the feature/login
branch, just like git checkout
.
Create and Switch to a New Branch
To create and switch to a new branch using git switch
, use the -c
flag:
git switch -c <new-branch-name>
For example, to create and switch to a new branch named feature/signup
:
git switch -c feature/signup
The git switch
command is intended to be simpler and more intuitive than git checkout
, so it’s recommended for newer Git versions.
4. Stash Your Changes Before Switching Branches
If you have uncommitted changes in your working directory and need to switch branches, Git may prevent you from doing so to avoid conflicts. In this case, you can either commit or stash your changes.
To stash your changes:
git stash
This command saves your changes temporarily and reverts your working directory to match the current branch’s last commit. You can later apply the stashed changes with:
git stash pop
Once the changes are stashed, you can freely switch branches.
5. Handling Conflicts When Switching Branches
When switching between branches, Git may encounter conflicts if the changes you’ve made conflict with the branch you are switching to. If this happens, Git will notify you of the conflict, and you’ll need to manually resolve the issues before proceeding.
To resolve conflicts:
-
Open the files with conflicts and decide whether to keep your changes, the branch’s changes, or a combination of both.
-
After resolving the conflicts, add the resolved files to the staging area:
git add <file-name>
-
Finally, commit the changes:
git commit
6. Deleting Branches After Switching
Once you’ve finished working on a branch, you may want to delete it. To delete a branch locally after switching away from it, run:
git branch -d <branch-name>
If the branch hasn’t been merged, and you want to force the deletion, use:
git branch -D <branch-name>
Conclusion
Switching branches in Git is an essential skill for every developer, especially when working on multiple features or bug fixes. Whether you use git checkout
or the newer git switch
command, switching between branches allows you to isolate your work, collaborate more efficiently, and keep your project organized.
By following the steps outlined in this guide, you'll be able to navigate Git branches smoothly using the terminal. Whether you're switching between existing branches or creating new ones, these commands will help you manage your development workflow effectively.