Git
How to Change Branches in Git Using Command Line?
Switching between branches is an essential part of using Git, especially when managing different features, fixing bugs, or experimenting with new ideas. Git’s branching system allows developers to keep their main codebase clean and organized while developing features in isolation.
In this blog, we’ll cover how to check which branch you’re on, switch to different branches, and manage branch switching in a collaborative project.
What Are Git Branches?
Git branches are individual lines of development in a repository. They allow you to isolate changes from the main codebase, experiment with new features, or collaborate with others without affecting the main project.
In Git, the main branch (often called main
or master
) usually holds the stable version of your code. Feature branches or other named branches contain development work or experiments, allowing you to make changes without impacting the main branch until you’re ready to merge them.
Step 1: Check Your Current Branch
Before switching branches, it’s useful to know which branch you’re currently on.
- Use
git status
:
git status
This command will display information about the current branch and the state of your working directory. You’ll see a message like On branch <branch-name>
at the top.
- Use
git branch
:
git branch
This command lists all branches in the repository, and the current branch is marked with an asterisk (*
).
Step 2: Switch to an Existing Branch
Once you know the available branches, you can switch to the desired one.
- Use
git checkout
:
git checkout <branch-name>
Replace <branch-name>
with the name of the branch you want to switch to. For example, if you want to switch to a branch called feature-login
, you would type:
git checkout feature-login
Note: As of Git 2.23, there’s a new command called git switch
, which is specifically designed for switching branches and is simpler for this purpose. git checkout
is still widely used, but git switch
offers a more intuitive syntax.
- Using
git switch
:
git switch <branch-name>
This command works similarly to git checkout
but is limited to switching branches, making it a little easier to use.
Step 3: Create and Switch to a New Branch
Often, you might want to create a new branch and start working on it immediately. You can do this in one step with either git checkout
or git switch
.
- Using
git checkout -b
:
git checkout -b <new-branch-name>
For example:
git checkout -b feature-signup
This command creates a new branch called feature-signup
and switches to it immediately. The -b
flag tells Git to create the branch if it doesn’t already exist.
- Using
git switch -c
:
git switch -c <new-branch-name>
This command is equivalent to git checkout -b
but uses the newer git switch
syntax.
Step 4: Handling Changes When Switching Branches
When switching branches, there are a few scenarios to keep in mind regarding uncommitted changes in your working directory.
- Committed Changes: If all changes are committed, Git will allow you to switch branches without issue.
- Uncommitted Changes: If you have uncommitted changes, Git may prevent you from switching branches to avoid potential conflicts. In this case, you have a few options:
- Commit the Changes: Save your changes by committing them to the current branch. Then you can switch branches safely.
git add . git commit -m "Save changes before switching branches"
- Stash the Changes: If you want to keep your changes but not commit them, you can use
git stash
to temporarily store them. Once you switch to the new branch, you can reapply the stashed changes.git stash git checkout <branch-name> git stash pop
- Discard the Changes: If you don’t need the changes, you can discard them with
git reset
orgit checkout .
commands. Be careful with this option, as it will delete any uncommitted changes.
Step 5: Viewing All Branches and Current Branch
If you’re unsure of the branch name or want to see all branches, use git branch -a
to list all branches, including remote ones.
git branch -a
- Local branches are displayed without any prefix.
- Remote branches (e.g., branches on GitHub) are displayed with a
remotes/origin/
prefix.
Step 6: Delete a Branch After Merging
Once you’re finished with a branch and have merged it back into main
or master
, you may want to delete it.
- Delete a Local Branch:
git branch -d <branch-name>
The -d
flag is for deleting a branch that has already been merged.
- Force Delete a Branch:
If the branch hasn’t been merged but you still want to delete it, use-D
:
git branch -D <branch-name>
Summary of Commands for Switching Branches in Git
Command | Description |
---|---|
git branch | Lists all branches and shows the current branch. |
git checkout <branch-name> | Switches to an existing branch. |
git switch <branch-name> | Switches to an existing branch (newer syntax). |
git checkout -b <branch-name> | Creates and switches to a new branch. |
git switch -c <branch-name> | Creates and switches to a new branch (newer syntax). |
git stash | Stashes uncommitted changes temporarily. |
git branch -d <branch-name> | Deletes a branch that has been merged. |
git branch -D <branch-name> | Force deletes a branch, even if it’s not merged. |
Conclusion
Switching branches in Git is a fundamental part of managing code and collaborating with others. Using git checkout
or git switch
, you can easily change branches and keep your development organized, allowing for clean, efficient workflows. Whether you’re fixing bugs, developing new features, or collaborating on larger projects, these Git branch management techniques will help you work seamlessly and avoid conflicts.
By practicing these commands and understanding the workflow, you’ll be ready to handle complex projects with ease!