Is there any way to git checkout previous branch?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Yes. Git has a built-in shortcut for switching back to the previously checked-out branch. It is one of the fastest branch-navigation features in day-to-day work, and it is especially useful when you are bouncing between a feature branch and main, or between review and fix branches.
The Shortcut: git checkout -
The classic command is:
The hyphen means "the previous branch or checkout target." In practice, it behaves a lot like cd - in a shell: it toggles back to where you were just before.
For example:
After the last command, you are back on feature/login.
If you run it again, Git toggles back to main. That makes it a very efficient way to jump between two branches repeatedly.
Modern Git Also Supports git switch -
If you prefer the newer branch-focused command, the same shortcut works with git switch.
Modern Git also documents - as shorthand for @{-1}, which refers to the previously checked-out branch or target. If your team already prefers git switch over git checkout, this is usually the clearer form because switch is dedicated to branch changes rather than mixing branch and file-restoration behavior.
Understanding @{-1}
The explicit reference syntax is:
That means "the last place I switched from." In most workflows you will just use -, but @{-1} is useful to know because it makes the behavior less mysterious and can help when reading Git documentation or reflog-oriented examples.
Git also supports older positions such as @{-2} for the second-most-recent checkout target, though that is less common in everyday work.
What Happens If You Have Local Changes
Switching back to the previous branch can fail if doing so would overwrite uncommitted work.
Possible result:
In that case, you need to resolve the working tree state first. Common options are:
Or, if the changes are not ready to commit:
This is an important practical detail: the shortcut is convenient, but it still obeys Git's safety rules about protecting local changes.
Previous Branch Versus Previous Commit
The shortcut refers to the previous checkout target, which is usually a branch but can also be another commit or detached state. If you checked out a tag or commit directly, the "previous location" may not be a normal branch name.
That is why the article title says "previous branch," but the actual mechanism is slightly broader. In most normal branch-based work the distinction does not matter, but it explains why Git implements the feature through checkout history rather than a special branch-only variable.
Helpful Related Commands
If you lose track of where you were, a few commands help:
git branch --show-current tells you the active branch. git reflog shows recent HEAD moves, including branch switches, which is useful if you need more than just the immediate previous location.
Common Pitfalls
The most common mistake is forgetting that local modifications can block the switch. Another is assuming the shortcut means "previous named branch" in every case, when it actually means the previous checkout target. Developers also sometimes use older git checkout syntax for file restoration and branch switching interchangeably, which is one reason many teams prefer git switch for branch movement in newer Git versions.
Summary
- Use
git checkout -to jump back to the previous branch quickly. - In newer Git,
git switch -does the same job in a branch-focused command. - '
-is shorthand for@{-1}, the previous checkout target.' - The switch can fail if uncommitted changes would be overwritten.
- Use
git reflogif you need to inspect more than the immediately previous location.
Related reading
- Is there any way to git checkout previous branch?
- Is there some way to work with git using .NET application?
- Issue with adding common code as git submodule already exists in the index
- Java merge 2 collections in O1
- Javascript Git client
- K8S Failed to pull image from local repo
- Kafka - How to commit offset after every message using High-Level consumer?
- Kafka - Offset Commit & Seek
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.