---
title: "10+ Secret Git Commands That Will Save Hours Every Week"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/post/10-secret-git-commands-to-save-time
---

![Blog post image for 10+ Secret Git Commands That Will Save Hours Every Week - Discover 10+ secret Git commands that will save hours every week! Learn advanced Git techniques for undoing mistakes, managing commits, automating workflows, and optimizing repositories. Perfect for DevOps and GitHub users.](/_astro/hero.BrIIpyXb_Z1j7y9S.webp)

[Home](/)›[Blog](/blog)›[All Categories](/blog/categories)›[Git](/blog/categories/git)

Blog

[Next in GitDotfiles: A Git-Based Strategy for Configuration Management](/blog/post/dotfiles)

[Git](/blog/categories/git)[Version Control](/blog/categories/version-control)[DevOps](/blog/categories/devops)[Software Development](/blog/categories/software-development)[Productivity](/blog/categories/productivity)

# 10+ Secret Git Commands That Will Save Hours Every Week

[Mohammad Abu Mattar](/authors/mohammad-abu-mattar)Published: 22 Feb 202504 Mins read08 Mins listen

[Markdown for AI(opens in a new tab)](/post/10-secret-git-commands-to-save-time/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

TL;DR

Discover 10+ secret Git commands that will save hours every week! Learn advanced Git techniques for undoing mistakes, managing commits, automating workflows, and optimizing repositories. Perfect for DevOps and GitHub users.

Series

[Developer Environment & Tooling](/series/developer-environment--tooling)4/4

[PreviousCustomization Windows Terminal With Starship](/blog/post/customization-windows-terminal-with-starship)

All posts in this series (4)

Blog4

1.  [Dotfiles: A Git-Based Strategy for Configuration Management](/blog/post/dotfiles)
2.  [VIM Cheat Sheet](/blog/post/vim-cheat-sheet)
3.  [Customization Windows Terminal With Starship](/blog/post/customization-windows-terminal-with-starship)
4.  [10+ Secret Git Commands That Will Save Hours Every WeekYou are here](/blog/post/10-secret-git-commands-to-save-time)

### 10+ Secret Git Commands That Will Save Hours Every Week

Contents

[Introduction](#introduction)[Undoing changes like a pro](#undoing-changes-like-a-pro)[1\. `git reflog`](#1-git-reflog)[2\. `git reset --soft HEAD~1`](#2-git-reset---soft-head1)[3\. `git restore`](#3-git-restore)[Managing branches efficiently](#managing-branches-efficiently)[4\. `git switch`](#4-git-switch)[5\. `git worktree`](#5-git-worktree)[6\. `git rebase -i HEAD~<number-of-commits>`](#6-git-rebase--i-headnumber-of-commits)[Managing commits like a boss](#managing-commits-like-a-boss)[7\. `git cherry-pick`](#7-git-cherry-pick)[8\. `git commit --amend`](#8-git-commit---amend)[9\. `git range-diff`](#9-git-range-diff)[Managing the working directory](#managing-the-working-directory)[10\. `git stash`](#10-git-stash)[11\. `git sparse-checkout`](#11-git-sparse-checkout)[Debugging and auditing code](#debugging-and-auditing-code)[12\. `git bisect`](#12-git-bisect)[13\. `git blame`](#13-git-blame)[Conclusion](#conclusion)[References](#references)

## [Introduction](#introduction)

As a software engineer, DevOps engineer, or GitHub user, you probably use Git daily. But are you making the most of it? Git is packed with commands that can save you hours of manual work every week. In this guide, we’ll look at 10+ lesser-known but very useful Git commands that simplify your workflow and cut out repetitive steps.

Each command is grouped by use case, with a real-world scenario for each one so you can apply it immediately.

* * *

## [Undoing changes like a pro](#undoing-changes-like-a-pro)

### [1\. `git reflog`](#1-git-reflog)

Recover lost commits.

It tracks all actions performed in your repository, including resets and rebases.

Use it when you accidentally delete a branch or reset commits and need to recover them.

Imagine you are working on a critical feature and accidentally run `git reset --hard`, losing your latest commit. Instead of panicking, you can recover your work by running:

Terminal window

```
1git reflog
```

Find the commit hash of the lost state and recover it with:

Terminal window

```
1git reset --hard <commit-hash>
```

Now your work is back!

### [2\. `git reset --soft HEAD~1`](#2-git-reset---soft-head1)

Undo the last commit but keep the changes.

It removes the last commit but keeps the changes staged.

Use it when you commit something too soon but don’t want to lose your changes.

You just committed a file but realized you forgot to add an important change. Instead of creating another commit, you can undo the last commit while keeping your changes staged:

Terminal window

```
1git reset --soft HEAD~1
```

Now your changes are still staged, and you can amend them!

### [3\. `git restore`](#3-git-restore)

Revert modified files.

It restores modified files to their last committed state.

Use it when you accidentally modify or delete files and need to revert them.

You mistakenly changed a config file and need to revert it to its last committed state. Instead of re-cloning the repo, you can simply run:

Terminal window

```
1git restore <filename>
```

Your file is now back to its last committed version.

* * *

## [Managing branches efficiently](#managing-branches-efficiently)

### [4\. `git switch`](#4-git-switch)

Switch branches quickly.

It moves you between branches without the rest of what `git checkout` does.

Use it when you are working on multiple features and need to jump between branches frequently.

You are working on multiple features and need to switch between branches frequently. Instead of using `git checkout` every time, you can use:

Terminal window

```
1git switch feature-branch
```

Faster and cleaner than `git checkout`!

### [5\. `git worktree`](#5-git-worktree)

Work on multiple branches at the same time.

It lets you check out multiple branches in separate directories.

Use it when you need to work on multiple branches without switching context.

You need to review a PR while working on a feature. Instead of stashing your changes, you can run:

Terminal window

```
1git worktree add ../review-pr feature-branch
```

Now you have both branches available in different directories!

### [6\. `git rebase -i HEAD~<number-of-commits>`](#6-git-rebase--i-headnumber-of-commits)

Clean up commit history.

It rewrites commit history interactively.

Use it when cleaning up commit history before merging a branch.

Your feature branch has 10 messy commits. Instead of merging them all, you can run:

Terminal window

```
1git rebase -i HEAD~10
```

Pick, squash, or edit commits for a cleaner history!

* * *

## [Managing commits like a boss](#managing-commits-like-a-boss)

### [7\. `git cherry-pick`](#7-git-cherry-pick)

Apply specific commits to another branch.

It copies one commit onto the branch you have checked out.

Use it when you need to apply a hotfix from one branch to another.

A bugfix commit exists in `dev` but is needed in `main`. Instead of merging everything, you can apply the specific commit:

Terminal window

```
1git cherry-pick <commit-hash>
```

### [8\. `git commit --amend`](#8-git-commit---amend)

Modify the last commit.

It changes the last commit message, or folds more changes into it.

Use it when you make a small mistake in a commit.

You committed with the wrong message. Instead of creating a new commit, you can amend the last commit message:

Terminal window

```
1git commit --amend -m "Corrected commit message"
```

No need for an extra commit!

### [9\. `git range-diff`](#9-git-range-diff)

Compare commit ranges.

It diffs two ranges of commits against each other.

Use it when reviewing a rebase before pushing changes.

Before force-pushing a rebased branch, you want to verify the changes. You can compare the commit ranges:

Terminal window

```
1git range-diff origin/main HEAD
```

That way nothing important gets lost.

* * *

## [Managing the working directory](#managing-the-working-directory)

### [10\. `git stash`](#10-git-stash)

Save uncommitted changes temporarily.

It parks your uncommitted changes and leaves you a clean working tree.

Use it when switching branches but keeping your work.

You need to check out `main`, but have unfinished work. Instead of committing incomplete changes, you can stash them:

Terminal window

```
1git stash
```

Switch branches, then restore it with:

Terminal window

```
1git stash pop
```

### [11\. `git sparse-checkout`](#11-git-sparse-checkout)

Check out specific parts of a repository.

It limits your working tree to the paths you ask for.

Use it when dealing with large monorepos.

You are working with a large monorepo and only need specific directories. Instead of cloning everything, you can initialize sparse-checkout:

Terminal window

```
1git sparse-checkout init --cone
```

Then include specific folders:

Terminal window

```
1git sparse-checkout set backend/
```

* * *

## [Debugging and auditing code](#debugging-and-auditing-code)

### [12\. `git bisect`](#12-git-bisect)

Find the commit that introduced a bug.

It binary-searches your history for the commit that broke things.

Use it when debugging regressions.

A feature broke, but you don’t know when. You can automate the search for the bad commit:

Terminal window

```
1git bisect start
```

Mark bad and good commits, and Git will find the culprit!

### [13\. `git blame`](#13-git-blame)

Identify who modified each line.

It shows who last touched each line in a file, and in which commit.

Use it when investigating why a piece of code changed.

You found a bug in a file and want to know who last modified it. You can run:

Terminal window

```
1git blame config.yaml
```

Now you know whom to ask!

* * *

## [Conclusion](#conclusion)

Learn these Git commands and you will save hours every week, mostly by not redoing work you already did once. Whether you are a software engineer, DevOps engineer, or GitHub user, they cover undoing changes, managing branches, tidying up commits, and debugging.

* * *

## [References](#references)

1.  Pro Git book - Scott Chacon and Ben Straub. [https://git-scm.com/book/en/v2](https://git-scm.com/book/en/v2)
2.  Git Documentation - Official Git SCM. [https://git-scm.com/doc](https://git-scm.com/doc)
3.  Atlassian Git Tutorial. [https://www.atlassian.com/git/tutorials](https://www.atlassian.com/git/tutorials)
4.  `git reflog` - Git SCM. [https://git-scm.com/docs/git-reflog](https://git-scm.com/docs/git-reflog)
5.  `git reset` - Git SCM. [https://git-scm.com/docs/git-reset](https://git-scm.com/docs/git-reset)
6.  `git restore` - Git SCM. [https://git-scm.com/docs/git-restore](https://git-scm.com/docs/git-restore)
7.  `git switch` - Git SCM. [https://git-scm.com/docs/git-switch](https://git-scm.com/docs/git-switch)
8.  `git worktree` - Git SCM. [https://git-scm.com/docs/git-worktree](https://git-scm.com/docs/git-worktree)
9.  `git rebase` - Git SCM. [https://git-scm.com/docs/git-rebase](https://git-scm.com/docs/git-rebase)
10.  `git cherry-pick` - Git SCM. [https://git-scm.com/docs/git-cherry-pick](https://git-scm.com/docs/git-cherry-pick)
11.  `git stash` - Git SCM. [https://git-scm.com/docs/git-stash](https://git-scm.com/docs/git-stash)
12.  `git sparse-checkout` - Git SCM. [https://git-scm.com/docs/git-sparse-checkout](https://git-scm.com/docs/git-sparse-checkout)
13.  `git bisect` - Git SCM. [https://git-scm.com/docs/git-bisect](https://git-scm.com/docs/git-bisect)
14.  `git blame` - Git SCM. [https://git-scm.com/docs/git-blame](https://git-scm.com/docs/git-blame)
15.  GitHub Guides. [https://guides.github.com/](https://guides.github.com/)

Was this useful?

## Tags

[#Git Commands](/blog/tags/git-commands)[#Git Tips](/blog/tags/git-tips)[#Version Control](/blog/tags/version-control)[#DevOps](/blog/tags/devops)[#GitHub](/blog/tags/github)[#Productivity Hacks](/blog/tags/productivity-hacks)[#CLI Tools](/blog/tags/cli-tools)[#Software Engineering](/blog/tags/software-engineering)[#Code Management](/blog/tags/code-management)[#Git Workflow](/blog/tags/git-workflow)

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=10%2B%20Secret%20Git%20Commands%20That%20Will%20Save%20Hours%20Every%20Week&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time&title=10%2B%20Secret%20Git%20Commands%20That%20Will%20Save%20Hours%20Every%20Week&summary=Discover%2010%2B%20secret%20Git%20commands%20that%20will%20save%20hours%20every%20week!%20Learn%20advanced%20Git%20techniques%20for%20undoing%20mistakes%2C%20managing%20commits%2C%20automating%20workflows%2C%20and%20optimizing%20repositories.%20Perfect%20for%20DevOps%20and%20GitHub%20users.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=10%2B%20Secret%20Git%20Commands%20That%20Will%20Save%20Hours%20Every%20Week%20https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time&text=10%2B%20Secret%20Git%20Commands%20That%20Will%20Save%20Hours%20Every%20Week "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time&title=10%2B%20Secret%20Git%20Commands%20That%20Will%20Save%20Hours%20Every%20Week "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time&t=10%2B%20Secret%20Git%20Commands%20That%20Will%20Save%20Hours%20Every%20Week "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time&media=&description=Discover%2010%2B%20secret%20Git%20commands%20that%20will%20save%20hours%20every%20week!%20Learn%20advanced%20Git%20techniques%20for%20undoing%20mistakes%2C%20managing%20commits%2C%20automating%20workflows%2C%20and%20optimizing%20repositories.%20Perfect%20for%20DevOps%20and%20GitHub%20users. "Share on Pinterest")[Email](<mailto:?subject=10%2B%20Secret%20Git%20Commands%20That%20Will%20Save%20Hours%20Every%20Week&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fblog%2Fpost%2F10-secret-git-commands-to-save-time>)

## Comments

## You might also enjoy

More posts on similar topics

[![Dotfiles: A Git-Based Strategy for Configuration Management](/_astro/hero.wfzXy7kc_dMWKx.webp)](/blog/post/dotfiles)

## [Dotfiles: A Git-Based Strategy for Configuration Management](/blog/post/dotfiles)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Linux](/blog/categories/linux)
-   [Git](/blog/categories/git)
-   [Configuration Management](/blog/categories/configuration-management)
-   [Developer Tools](/blog/categories/developer-tools)
-   [Productivity](/blog/categories/productivity)

Introduction Your dotfiles, those hidden .-prefixed configuration files scattered across your home directory, are the muscle memory of your environment. They hold your shell aliases, your editor

[#Dotfiles Management](/blog/tags/dotfiles-management)[#Git Bare Repository](/blog/tags/git-bare-repository)[#Shell Configuration](/blog/tags/shell-configuration)+7 tags

[read more](/blog/post/dotfiles)

[![Customization Windows Terminal With Starship](/_astro/hero.C_M2lGhc_1GJQQi.webp)](/blog/post/customization-windows-terminal-with-starship)

## [Customization Windows Terminal With Starship](/blog/post/customization-windows-terminal-with-starship)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Windows](/blog/categories/windows)
-   [Terminal](/blog/categories/terminal)
-   [PowerShell](/blog/categories/powershell)
-   [Starship](/blog/categories/starship)
-   [Customization](/blog/categories/customization)
-   [Developer Tools](/blog/categories/developer-tools)

Introduction In this article, we will install PowerShell and Starship, configure Windows Terminal, and then customize it with Starship. What is Windows Terminal? Windows Terminal is a modern

[#Windows Terminal](/blog/tags/windows-terminal)[#Starship Prompt](/blog/tags/starship-prompt)[#PowerShell Customization](/blog/tags/powershell-customization)+7 tags

[read more](/blog/post/customization-windows-terminal-with-starship)

[![VIM Cheat Sheet](/_astro/hero.Buk-UDGW_2r0YqI.webp)](/blog/post/vim-cheat-sheet)

## [VIM Cheat Sheet](/blog/post/vim-cheat-sheet)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Linux](/blog/categories/linux)
-   [VIM](/blog/categories/vim)
-   [Text Editors](/blog/categories/text-editors)
-   [Developer Tools](/blog/categories/developer-tools)
-   [Command Line](/blog/categories/command-line)

What is VIM? VIM (Vi Improved) is a versatile text editor pre-installed on most Linux systems, known for its efficiency in command-line file editing. Its modal nature, switching between modes like

[#VIM Commands](/blog/tags/vim-commands)[#VIM Cheat Sheet](/blog/tags/vim-cheat-sheet)[#Text Editing](/blog/tags/text-editing)+5 tags

[read more](/blog/post/vim-cheat-sheet)

[![GitHub Actions vs. GitLab CI for Monorepos: Which One Wins?](/_astro/hero.7c9S_WKE_ZmGOGN.webp)](/blog/post/github-actions-vs-gitlab-ci-for-monorepos)

## [GitHub Actions vs. GitLab CI for Monorepos: Which One Wins?](/blog/post/github-actions-vs-gitlab-ci-for-monorepos)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [DevOps](/blog/categories/devops)
-   [CI/CD](/blog/categories/cicd)
-   [Monorepos](/blog/categories/monorepos)
-   [Software Development](/blog/categories/software-development)

The world of building software is always changing, and how teams organize their code can really affect how well they work. One way that's become pretty popular is using a monorepo. That's just keeping

[#GitHub Actions](/blog/tags/github-actions)[#GitLab CI](/blog/tags/gitlab-ci)[#Monorepo](/blog/tags/monorepo)+7 tags

[read more](/blog/post/github-actions-vs-gitlab-ci-for-monorepos)

[![Streamlining GitHub Organization Management with Terraform](/_astro/hero.DJ7g8CQA_Z1UlNiP.webp)](/blog/post/streamlining-github-organization-management-with-terraform)

## [Streamlining GitHub Organization Management with Terraform](/blog/post/streamlining-github-organization-management-with-terraform)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [DevOps](/blog/categories/devops)
-   [Infrastructure as Code](/blog/categories/infrastructure-as-code)
-   [GitHub](/blog/categories/github)
-   [Automation](/blog/categories/automation)
-   [Terraform](/blog/categories/terraform)

Managing a GitHub organization manually can become increasingly complex as teams grow and projects multiply. For DevOps and DevSecOps engineers, automation is how you keep things consistent and cut do

[#Terraform](/blog/tags/terraform)[#GitHub](/blog/tags/github)[#IaC](/blog/tags/iac)+7 tags

[read more](/blog/post/streamlining-github-organization-management-with-terraform)

[![Understanding Software Versioning](/_astro/hero.DFRD27Ad_19S6oB.webp)](/blog/post/how-version-number-software-works)

## [Understanding Software Versioning](/blog/post/how-version-number-software-works)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Software Development](/blog/categories/software-development)
-   [Versioning](/blog/categories/versioning)
-   [DevOps](/blog/categories/devops)
-   [Best Practices](/blog/categories/best-practices)

Introduction Software versioning is an important practice in software development that tracks changes and updates to a codebase. It provides a structured way to identify different iterations of a

[#Semantic Versioning](/blog/tags/semantic-versioning)[#Software Versioning](/blog/tags/software-versioning)[#Release Management](/blog/tags/release-management)+6 tags

[read more](/blog/post/how-version-number-software-works)

6 related posts
