Cover image for 10 New Git Commands You Should Know

10 New Git Commands You Should Know

8 min read

Are you looking for the latest Git commands to level up your source control skills? This article compiles 10 modern, powerful Git commands and new features to help you work more efficiently and optimize your software development workflow—perfect for both beginners and experienced developers.

Why Should You Update Your Git Commands?

  • Speed up operations with large repositories
  • Ensure safety when switching branches or undoing changes
  • Save time on maintenance and commit history management
  • Work more efficiently with monorepos or complex projects

Note: If you’re already familiar with basic Git tips, explore the commands below to take your skills to the next level!

1. git switch – A Safer Way to Switch Branches

Before Git 2.23, git checkout was the main command for switching branches, but it did much more. You could use it to restore files, create branches, or check out specific commits. This made it powerful but potentially confusing, especially when you just wanted to switch branches without touching your files.

That’s why Git 2.23 introduced git switch as a more focused alternative for branch operations. With git switch, you can focus solely on branch management:

Bash

# Switch to another branch
git switch feature-branch

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

This clarity reduces the risk of overwriting files or making unwanted changes by accident. If you’ve ever hesitated to use git checkout for fear of doing something wrong, git switch simplifies the process.

2. git restore – Safely Undo Changes

Undoing changes often involves using git checkout to revert files or git reset to move the branch HEAD. However, both commands can change your branch state if used incorrectly: git reset can move your branch HEAD, while git checkout can switch branches or check out another commit, disrupting your current branch.

Git 2.23 introduced git restore to focus solely on undoing changes to files. It provides a safer, more direct way to revert changes in your working directory or staging area, clearly separating file operations from branch management tasks:

Bash

# Discard changes in the working directory
git restore main.js

# Unstage changes from the index
git restore --staged main.js

This is especially useful for beginners or in critical situations where precision is key. You can undo changes without worrying about accidentally switching branches or resetting commits.

3. git maintenance – Automate Repository Health

As your repository grows, performance can degrade. Operations like git fetch, git status, or git log may slow down, and unused data can clutter your repo. Before Git 2.29, you had to manually run commands like git gc (garbage collection) or git repack to keep your repo optimized.

Git 2.29 introduced git maintenance, which automates these tasks for you:

Bash

# Enable automatic maintenance
git maintenance start

# Run maintenance tasks immediately
git maintenance run

What happens behind the scenes?

  • Garbage collection: Removes unreachable objects, such as commits discarded during rebase or branch deletion.
  • Repacking: Merges fragmented pack files for more efficient storage.
  • Commit-graph update: Optimizes commit history browsing, speeding up commands like git log and git blame.

Using git maintenance helps keep your repository healthy without manual effort.

4. git sparse-checkout – Work Efficiently with Large Monorepos

Monorepos are great for managing multiple projects, but cloning the entire repository when you only need a specific folder can be inefficient. Git 2.25 introduced git sparse-checkout to solve this problem.

Bash

# Enable sparse-checkout mode
git sparse-checkout init

# Only include specific folders
# You can specify multiple folders separated by spaces
git sparse-checkout set services/ docs/

With git sparse-checkout, you can include only the folders or files you need in your working directory, ignoring the rest. This is useful for large teams working on separate parts of a monorepo and saves time and disk space.

5. git log —remerge-diff: Better Understand Merges

Merge commits often show which branches were merged, but they don’t always explain the specific changes introduced, especially when conflicts were resolved during the merge.

Starting with Git 2.35, you can use:

Bash

git log --remerge-diff

This option replays the recorded merge strategy and shows the exact changes it introduced. It’s useful for debugging merge conflicts or reviewing complex merge histories.

6. git blame —ignore-rev – Ignore “Noisy” Commits

When your team makes large-scale formatting changes, git blame can lose its usefulness, as every line ends up pointing to the formatting commit instead of the original author.

Introduced in Git 2.23, the --ignore-rev option lets you exclude such commits:

Bash

git blame --ignore-rev commit-hash

To persist this exclusion, you can set up an ignore-revs file:

Bash

# Add commit hash to the ignore-revs file
echo commit-hash >> .git-blame-ignore-revs

# Tell Git to use the file
git config blame.ignoreRevsFile .git-blame-ignore-revs

This helps you focus on meaningful authors and is useful in codebases with frequent style updates.

7. git range-diff – Compare Commit Ranges

Rewriting history—whether through rebase, cherry-picking, or interactive editing—can be tricky. After a rebase, you might wonder how the rewritten commits differ from the originals. git range-diff helps by comparing two commit ranges, showing how one range evolved into another and highlighting changes to each commit:

Bash

git range-diff

This command can be used to understand the evolution of a feature or bugfix across different branches.

8. git worktree – Work on Multiple Branches Simultaneously

Switching branches in a single working directory can disrupt your workflow, especially when you need to work on multiple branches. With git worktree, you can create additional working directories linked to the same repository.

# Add a new worktree for a specific branch
git worktree add ../feature-branch feature-branch

# Remove a worktree when done
git worktree remove ../feature-branch

git worktree lets you work on different branches without switching or stashing. You can also create disposable worktrees with detached HEADs for testing or isolating builds and deployments in separate directories.

9. git rebase —update-refs – Keep References in Sync

When you rebase (rewrite history), old branches and tags can become broken because the history has changed. Git 2.38 adds the --update-refs option to automatically update these branches and tags to match the new history.

git rebase --update-refs

This command helps Git automatically update branches and tags related to rebased commits, ensuring history remains consistent.

You can customize Git to automatically update specific branches and tags by configuring:

Bash

git config rebase.updateRefs true

This is very useful for teams or when managing multiple related branches/tags.

10. git commit —fixup and git rebase —autosquash – Fix Commits Easily

Although available for a long time (since Git 1.7.4), the git commit --fixup command is often overlooked, even though it’s very useful for keeping commit history clean.

While working, you might find you need to fix or improve a previous commit. Manually editing commit history is error-prone.

Git provides git commit --fixup and git rebase --autosquash to automate this process:

Bash

# Create a fixup commit for a specific commit
git commit --fixup=<commit-hash>

# Then, during interactive rebase, automatically squash fixup commits
git rebase -i --autosquash <base-branch> 

The --fixup command creates a special commit that will be automatically squashed into the target commit during interactive rebase. This makes it easy to clean up commit history before merging and ensures related changes are grouped together without manual steps.

Conclusion & Next Steps

These new Git commands and modern features will help you:

  • Manage source code safely and efficiently
  • Save time on daily operations
  • Easily maintain and scale your projects

Try applying one or two of these commands to your current workflow!

Read more: 15 Command Line Git Tips Every Developer Should Know

If you found this article helpful, share it with your colleagues or leave a comment below!

Thanks for reading!

Related Posts

Microservices là gì? Bạn có thật sự cần đến nó?

Tìm hiểu về kiến trúc microservices: thành phần, ưu nhược điểm và khi nào nên áp dụng cho dự án của bạn.

Read more

What are Microservices? Do You Really Need Them?

Learn about microservices architecture: components, pros and cons, and when to apply it to your project.

Read more

Tự động hóa hệ thống với Ansible

Hướng dẫn cài đặt và sử dụng Ansible, công cụ tự động hóa mạnh mẽ cho DevOps.

Read more

10 Lệnh Git Mới Mà Bạn Nên Biết

Khám phá 10 lệnh Git mới giúp tối ưu quy trình làm việc, tăng hiệu suất với ví dụ thực tế và mẹo sử dụng Git hiệu quả.

Read more