What does "git checkout -" do?
Categories:
Understanding 'git checkout -': Your Shortcut to Previous Branches

Explore the utility of git checkout -
for quickly switching between your last two Git branches, enhancing your workflow efficiency.
In the world of Git, efficient navigation between branches is crucial for a smooth development workflow. While git checkout <branch-name>
is the standard command for switching to a specific branch, Git offers a powerful shortcut that often goes underutilized: git checkout -
. This command provides a quick and convenient way to jump back to your previously active branch, saving keystrokes and improving productivity. This article will delve into what git checkout -
does, how it works, and why it's a valuable addition to your Git toolkit.
The Magic Behind 'git checkout -'
The hyphen (-
) in git checkout -
is a special reference that Git interprets as the 'previous branch' or 'last checked out branch'. Git maintains a record of your branch history, specifically the last branch you were on. When you execute git checkout -
, Git looks up this record and switches your working directory and HEAD to that previous branch. It's essentially a toggle between your current branch and the one you were on immediately before it.
# Assume you are on 'main'
git checkout feature/new-feature
# Now you are on 'feature/new-feature'
git checkout -
# You are now back on 'main'
git checkout -
# You are now back on 'feature/new-feature'
Demonstrating the toggling behavior of git checkout -
flowchart TD A[Start on Branch A] --> B{git checkout Branch B} B --> C[Now on Branch B] C --> D{git checkout -} D --> E[Back on Branch A] E --> F{git checkout -} F --> G[Back on Branch B] G --> H[End]
Flowchart illustrating the git checkout -
toggling mechanism
Practical Use Cases and Benefits
The primary benefit of git checkout -
is speed and convenience. Instead of typing out long branch names, you can simply use the hyphen. This is particularly useful in scenarios where you're frequently switching between two branches, such as:
- Context Switching: You're working on a feature branch (
feature/X
), need to quickly check something onmain
(ordevelop
), and then immediately return tofeature/X
. - Reviewing Code: You're reviewing a pull request on a specific branch, but need to jump back to your own working branch to make a quick change or commit.
- Rebasing/Merging: When preparing for a rebase or merge, you might switch to the target branch, pull updates, and then switch back to your feature branch.
git switch
command, introduced in Git 2.23, offers a more modern and explicit way to switch branches. git switch -
provides the same functionality as git checkout -
for switching to the previous branch, aligning with the separation of concerns for git switch
(branch operations) and git restore
(file operations).Beyond Branches: 'git checkout -' for Files
While primarily used for switching branches, the hyphen also has a similar meaning when used with git checkout
for files. If you want to discard changes in a specific file and revert it to its state in the last committed version of the current branch, you can use git checkout -- <file-name>
. The double hyphen (--
) is important here to disambiguate between a branch name and a file path, especially if a file has the same name as a branch. However, git checkout -
(single hyphen) specifically refers to the previous branch context.
# Discard changes in 'my_file.txt' and revert to last committed state
git checkout -- my_file.txt
Reverting a file to its last committed state using git checkout --
git checkout -- <file-name>
as it discards local changes without confirmation. Ensure you understand the implications before executing this command.