DEV Community

Shakil Alam
Shakil Alam

Posted on

Git Checkout vs Git Switch: Why You Need to Break Up With Your Old Habits

Ah, git checkout. The jack-of-all-trades, the Swiss Army knife, the “one command to rule them all” in the Git world. For years, it’s been our go-to for switching branches, restoring files, creating branches, and even detaching HEADs (yikes). But let’s face it: multitasking isn’t always a good thing.

Git Switch vs Git Checkout

If you’ve ever found yourself wondering, “Did I just restore a file or switch branches?”, then congratulations—you’ve been a victim of git checkout’s overly ambitious nature.

That’s why, starting with Git 2.23, the Git wizards blessed us with two new commands: git switch and git restore. They’re clearer, safer, and designed to simplify your life. In this post, I’ll show you why these commands are the heroes your Git workflow needs—and how to start using them today.


Why Did Git Break Up With git checkout?

Let’s be honest: git checkout was doing too much. Think of it like a coworker who insists on doing all the tasks but ends up making a mess because they’re stretched too thin.

Here’s what git checkout used to juggle:

  1. Switching branches.
  2. Creating new branches.
  3. Restoring files.
  4. Checking out specific commits.

The result? Ambiguity and a whole lot of human error. Have you ever tried to restore a file but accidentally switched branches? Or detached your HEAD without knowing what just happened? Yep, we’ve all been there.

Git’s solution was simple: split git checkout into two specialized commands:

  • git switch for switching and creating branches.
  • git restore for restoring files or working tree states.

Now, instead of one multitasking command, we have tools that are purpose-built—and far less likely to make you pull your hair out.


git checkout vs git switch: Let’s Settle This

Here’s where the magic happens. Let’s compare how these commands stack up in common scenarios.

1. Switching Branches

  • The Old Way (git checkout):
  git checkout feature-branch
Enter fullscreen mode Exit fullscreen mode

Does the job, but it’s easy to accidentally restore files if you’re not careful.

  • The New Way (git switch):
  git switch feature-branch
Enter fullscreen mode Exit fullscreen mode

Straightforward and unambiguous. You’re just switching branches—nothing more, nothing less.


2. Creating a New Branch

  • The Old Way (git checkout):
  git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode
  • The New Way (git switch):
  git switch -c new-branch
Enter fullscreen mode Exit fullscreen mode

Why git switch is Better: The -c flag explicitly says, “Hey, I’m creating a branch here!” It’s clear, concise, and reduces confusion.


3. Restoring Files

This is where git restore truly shines.

  • The Old Way (git checkout):
  git checkout HEAD~1 myfile.txt
Enter fullscreen mode Exit fullscreen mode

Did you just restore a file? Switch branches? Detach your HEAD? Who knows?

  • The New Way (git restore):
  git restore myfile.txt
Enter fullscreen mode Exit fullscreen mode

Why git restore Wins: It’s laser-focused. If you’re restoring a file, use git restore. No side effects, no guessing games.


Why This Matters for Your Workflow

Still not convinced? Let’s talk about why switching to git switch and git restore is more than just a nice-to-have.

1. Fewer Mistakes

With git switch and git restore, you can’t accidentally restore files when you meant to switch branches—or vice versa. Each command has one job, so there’s less room for error.

2. Cleaner Commands

Your intent is crystal clear. When you type git switch, everyone knows you’re switching branches. When you type git restore, it’s obvious you’re fixing files. It makes your workflow more readable for both you and your team.

3. Future-Proofing

Git’s not-so-subtle nudges (“Did you mean git switch?”) are a sign of things to come. git switch and git restore are the future, so it’s better to get comfortable with them now.


How to Start Using git switch and git restore

Ready to level up your Git game? Here’s how to make the switch (pun intended):

1. Update Your Git Version

If you’re not seeing these commands, you’re probably running an ancient version of Git. Time to update:

sudo apt update && sudo apt install git  # Linux  
brew install git                        # macOS  
Enter fullscreen mode Exit fullscreen mode

2. Replace git checkout in Your Workflow

Next time you need to switch branches or restore files, use the new commands:

git switch main  
git switch -c new-branch  
git restore myfile.txt  
Enter fullscreen mode Exit fullscreen mode

3. Create Aliases for Convenience

Save some keystrokes by setting up aliases:

git config --global alias.sw switch  
git config --global alias.re restore  
Enter fullscreen mode Exit fullscreen mode

Now you can type git sw and git re like a true Git ninja.


Final Thoughts

Look, I get it. Change is hard. But using git switch and git restore isn’t just about jumping on a trend—it’s about clarity, efficiency, and future-proofing your workflow.

So the next time you catch yourself typing git checkout, stop. Ask yourself: Am I switching branches or restoring files? Then use the right tool for the job.

Your future self—and your team—will thank you. Now go ahead and give these commands a shot. Who knows, you might even start enjoying Git (well, almost).

You may also like 7 Secrets to Writing Perfect GitHub Issues Developers Love

Top comments (0)