Start using these modern Git commands instead "git checkout"

If you're still using `git checkout` for everything, you're missing out on some improved Git workflows.

Don't get me wrong - `git checkout` isn't going anywhere, and it still works perfectly fine. But Git introduced two new commands that make your intentions way clearer and reduce the chances of accidentally messing something up.

Let me show you what I mean.

 

The old way vs. the new way

Here's how most of us learned to work with Git:

# Switching branches the old way
git checkout main
git checkout feature-branch

# Creating new branches the old way  
git checkout -b new-feature

But since Git 2.23 (released in 2019), we have much cleaner alternatives:

# Switching branches the new way
git switch main
git switch feature-branch

# Creating new branches the new way
git switch -c new-feature

See the difference? `git switch` makes it crystal clear that you're switching between branches, not restoring files or doing some other checkout operation.

 

Why this matters more than you think

The problem with `git checkout` is that it does too many things. You can use it to switch branches, create branches, restore files, detach HEAD... it's basically the Swiss Army knife of Git commands.

This becomes a problem when you're working fast and accidentally run something like:

# Oops, meant to switch to the 'main' branch
git checkout main.py

Instead of switching to the main branch, you've now restored the `main.py` file and potentially lost your changes. Not fun.

With the new commands, this confusion disappears:

# Want to switch branches? Use switch
git switch main

# Want to restore a file? Use restore  
git restore main.py

Your intentions are clear, and Git knows exactly what you want to do.

 

The commands you should be using instead

Here's your new toolkit:

For switching and creating branches:

# Switch to existing branch
git switch feature-branch

# Create and switch to new branch
git switch -c new-feature

# Switch to previous branch (like cd -)
git switch -

For restoring files:

# Restore file from index (unstage changes)
git restore --staged main.py

# Restore file from HEAD (discard working changes)
git restore main.py

# Restore file from specific commit
git restore --source=HEAD~2 main.py

 

OperationOld CommandNew Command
Switch to existing branchgit checkout branch-namegit switch branch-name
Create and switch to new branchgit checkout -b new-branchgit switch -c new-branch
Restore file from indexgit checkout -- file.txtgit restore file.txt
Unstage filegit checkout HEAD -- file.txtgit restore --staged file.txt
Switch to previous branchgit checkout -git switch -

 

What about detached HEAD?

You can still detach HEAD when needed, but now it's more explicit:

# Old way
git checkout abc1234

# New way - Git will warn you about detached HEAD
git switch --detach abc1234

The `--detach` flag makes it clear that you're intentionally detaching HEAD, not accidentally ending up there.

 

Making the switch

I get it - muscle memory is hard to break. I still catch myself typing `git checkout` sometimes. But here's what helped me make the transition:

Start with `git switch` for branch operations only. Once that feels natural, add `git restore` for file operations. Within a week or two, you'll find yourself naturally reaching for the right command.

Your future self (and your teammates) will thank you for the clearer intentions in your Git workflow.

********************************** ************************* ************************ **************** ****************** *********** ************** ************* ************ *************