Modern Git Commands

Updated · 9 min read
mediumGit 2.53gitmodern-gitswitchrestoregit-confignew-features

Modern Git means the commands and options added since Git 2.23 (August 2019) that replace older, confusing or risky ways of doing things. The best known are git switch and git restore, which split the many jobs of git checkout into two clear commands, but there are many more: safer force pushes, background maintenance, sparse checkout, and clearer git config commands. Older commands still work, and most tutorials and answers online still use them, so it helps to know both: write modern commands yourself, and recognize the old ones when you read them. This page was last checked on November 15, 2026, against Git 2.53 (the latest release then was Git 2.56).

A new edition of a dictionary is a good comparison. The old words are still understood, but the new edition adds clearer ones, and a few old entries are marked "dated".

Old and New Side by Side

JobOlder wayModern waySince
Switch branchgit checkout maingit switch main2.23
Create and switchgit checkout -b featuregit switch -c feature2.23
Discard changes to a filegit checkout -- fees.txtgit restore fees.txt2.23
Unstage a filegit reset HEAD fees.txtgit restore --staged fees.txt2.23
Get a file from another commitgit checkout v1.0 -- fees.txtgit restore --source=v1.0 fees.txt2.23
Look at an old commitgit checkout <commit>git switch --detach <commit>2.23
Force pushgit push --forcegit push --force-with-lease --force-if-includes1.8.5, 2.30
Stash with a messagegit stash save "msg"git stash push -m "msg"2.13
Read a settinggit config user.namegit config get user.name2.46
Write a settinggit config user.name "Navika"git config set user.name "Navika"2.46
Housekeepinggit gc by handgit maintenance start2.30
Only some foldersManual .git/info/sparse-checkoutgit sparse-checkout set (cone mode)2.25
Merge strategyrecursiveort (the default)2.34
Older commands and their modern replacements

Example 1: switch and restore Instead of checkout

git checkout does two very different jobs: changing branches and overwriting files. A typo can turn one into the other and lose work. The two modern commands each do one job:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git switch -c feature/late-fee
echo 'Late fee: $250' >> fees.txt
git restore fees.txt
git status --short
git switch main
Output
Switched to a new branch 'feature/late-fee'
Switched to branch 'main'

Explanation: git switch -c created and switched to the branch, git restore fees.txt threw away the unsaved line, and git switch main went back. git switch refuses to take a file name, and git restore never changes branches, so neither can do the other's job by mistake. Git's own documentation says that checkout, switch and restore will all stay, so old scripts keep working.

Example 2: git config get and set

Since Git 2.46, git config has subcommands with clear names, like other commands:

Terminal
git config set --global pull.rebase true
git config get pull.rebase
git config list --global
git config unset --global pull.rebase
git config get pull.rebase || echo "pull.rebase is not set"
Output
true
user.name=Navika Anamaneni
user.email=navika@example.com
init.defaultbranch=main
pull.rebase=true
pull.rebase is not set

Explanation: set, get, list and unset replace the older forms git config <key> <value>, git config <key>, git config --list and git config --unset <key>, which still work. You have already seen the new form in Git's own hints, such as "Disable this message with git config set advice.mergeConflict false".

Example 3: Newer Commands Worth Knowing

Terminal
git repo info references.format object.format
git refs list
git last-modified
Output
references.format=files
object.format=sha1
5c7eaa5e8c85f7c4ee0b62e8aa63fe8f6f812190 commit	refs/heads/feature/late-fee
5c7eaa5e8c85f7c4ee0b62e8aa63fe8f6f812190 commit	refs/heads/main
5c7eaa5e8c85f7c4ee0b62e8aa63fe8f6f812190	fees.txt

Explanation: git repo info prints facts about the repository in a form scripts can read: here, that its references are stored in the files format and its object IDs use SHA-1 (both explained in "Git 3.0: What Is Planned"). git refs list lists references, like git for-each-ref. git last-modified shows, for each file, the last commit that changed it, as hosting platforms do in their file lists. All three were added in recent releases, so older Git versions don't have them, and their options may still change.

Other modern additions covered elsewhere in this course:

Command or optionWhat it doesLesson
git push --force-with-lease --force-if-includesForce push only if nobody else pushed meanwhile"Force Push Safely"
git rebase --update-refsMove stacked branches along in a rebase"Git Rebase"
git maintenance startBackground housekeeping"Keeping Repositories Fast"
git sparse-checkout, --filter=blob:noneWork on huge repositories"Git Sparse Checkout", "Shallow and Partial Clones"
git backfillDownload missing file contents of a partial clone in batches"Shallow and Partial Clones"
git merge-tree --write-treeMerge without a working folder"How Git Merges"
git log --remerge-diffSee how a merge's conflicts were resolved"How Git Merges"
SSH commit signing (gpg.format ssh)Sign without GPG"Signing Commits and Tags"
push.autoSetupRemoteFirst git push sets the upstream"Upstream and Remote-Tracking Branches"
scalarSettings for very large repositories"Keeping Repositories Fast"

How to Keep Up

Git releases a new version about every three months, with security and bug-fix releases in between.

  1. Release notes: each version's notes are in Git's source under Documentation/RelNotes/, and announced on the Git mailing list.
  2. Hosting platform blogs: GitHub's blog publishes a "Highlights from Git 2.xx" post for each release, and GitLab writes similar summaries.
  3. Your own Git: git version shows what you have, git help <command> opens the manual for exactly that version, and git help -a lists every command it knows.
  4. Git's planned changes: Documentation/BreakingChanges.adoc lists what Git 3.0 will change (see "Git 3.0: What Is Planned").
Terminal
git version
Output
git version 2.53.0

Common Mistakes

Copying old answers without checking. git checkout -- . and git push --force work, but have safer modern equivalents.

Using a new command in a script that runs on an old Git. CI images and servers can be years behind. Check git version there, or stick to long-established commands in shared scripts.

Thinking checkout is deprecated. It isn't, and Git plans to keep it. The modern commands are simply clearer.

Never updating Git. Old versions miss security fixes as well as features.

Under the Hood

Git keeps old commands working for a long time, because millions of scripts depend on them. New behavior usually arrives in three steps: first as an option (--force-with-lease), then as a setting that changes the default (pull.rebase, push.autoSetupRemote), and only in a major version, such as the planned Git 3.0, as a new default. switch and restore are new front ends over the same internal code that checkout uses, which is why they behave identically for the jobs they share. Commands marked "EXPERIMENTAL" in their help, such as git replay, may still change their options between releases.

Interview Corner

What is the difference between git checkout, git switch and git restore?

checkout both switches branches and restores files. Since Git 2.23, switch only changes branches and restore only restores files, which avoids mistakes. All three are supported.

How do you unstage a file in modern Git?

git restore --staged <file>. The older form is git reset HEAD <file>.

Name some features added to Git in recent years.

switch and restore, --force-with-lease with --force-if-includes, git maintenance, sparse checkout in cone mode, the ort merge strategy, SSH signing, git config get/set, merge-tree --write-tree and --remerge-diff.

Practice

Challenge: modernize these commands. Rewrite git checkout -b feature/exam-fee, git checkout -- fees.txt and git reset HEAD fees.txt with modern commands, and run them on a change to fees.txt.

What does Git print? Before you open the solution, decide the short status after each step.

Show solution
Terminal
git switch --quiet -c feature/exam-fee
echo 'Exam fee: $800' >> fees.txt
git add fees.txt
git restore --staged fees.txt
git status --short
git restore fees.txt
git status --short
Output
 M fees.txt

After git restore --staged, the change is no longer staged but still in the file ( M). After git restore, the file is back to its committed version, and the status is empty.

Checkpoint. Read the release notes of the newest Git version, pick three changes, and try each in a practice repository.

Quick check: modern Git

4 questions
1Which modern command replaces git checkout -b feature?
2Which command unstages fees.txt but keeps the change?
3Is git checkout planned for removal in Git 3.0?
4Which command prints a setting in modern Git?

Key Takeaways

Quick reference

OldModern
git checkout <branch>git switch <branch>
git checkout -b <branch>git switch -c <branch>
git checkout -- <file>git restore <file>
git reset HEAD <file>git restore --staged <file>
git push --forcegit push --force-with-lease
git config <key>git config get <key>

Was this tutorial helpful?

Lesson 105 of 106