Git
GitHub
Version Control
Coding
Software Development

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.

Browse interview questions

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:

bash
git checkout -

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:

bash
git checkout feature/login
git checkout main
git checkout -

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.

bash
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:

bash
git checkout @{-1}

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.

bash
git checkout -

Possible result:

text
error: Your local changes to the following files would be overwritten by checkout

In that case, you need to resolve the working tree state first. Common options are:

bash
git status
git add .
git commit -m "Save work in progress"

Or, if the changes are not ready to commit:

bash
git stash push -m "temp switch"
git checkout -

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.

If you lose track of where you were, a few commands help:

bash
git branch --show-current
git reflog --decorate --oneline
git status

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 reflog if you need to inspect more than the immediately previous location.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.