Type something to search...
10+ Secret Git Commands That Will Save Hours Every Week

10+ Secret Git Commands That Will Save Hours Every Week

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 powerful commands that can save you hours of manual work every week. In this guide, we’ll explore 10+ lesser-known but incredibly useful Git commands that will streamline your workflow, improve automation, and boost productivity.

Each command is grouped based on its use case, providing real-world scenarios so you can apply them immediately. Let’s dive in!


Undoing Changes Like a Pro

1. git reflog

Description: Recover lost commits.

What it does: Tracks all actions performed in your repository, including resets and rebases.

When to use it: When you accidentally delete a branch or reset commits and need to recover them.

Example: 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
git reflog

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

Terminal window
git reset --hard <commit-hash>

Now your work is back!

2. git reset --soft HEAD~1

Description: Undo last commit but keep changes.

What it does: Removes the last commit but keeps the changes staged.

When to use it: When you commit something too soon but don’t want to lose your changes.

Example: 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
git reset --soft HEAD~1

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

3. git restore

Description: Revert modified files.

What it does: Restores modified files to their last committed state.

When to use it: When you accidentally modify or delete files and need to revert them.

Example: 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
git restore <filename>

Your file is now back to its last committed version.


Managing Branches Efficiently

4. git switch

Description: Switch branches quickly.

What it does: Quickly switches between branches.

When to use it: When working on multiple features and need to jump between branches frequently.

Example: 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
git switch feature-branch

Faster and cleaner than git checkout!

5. git worktree

Description: Work on multiple branches simultaneously.

What it does: Allows you to check out multiple branches in separate directories.

When to use it: When working on multiple branches without switching context.

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

Terminal window
git worktree add ../review-pr feature-branch

Now you have both branches available in different directories!

6. git rebase -i HEAD~<number-of-commits>

Description: Clean up commit history.

What it does: Interactively rewrites commit history.

When to use it: When cleaning up commit history before merging a branch.

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

Terminal window
git rebase -i HEAD~10

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


Managing Commits Like a Boss

7. git cherry-pick

Description: Apply specific commits to another branch.

What it does: Applies a specific commit to another branch.

When to use it: When you need to apply a hotfix from one branch to another.

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

Terminal window
git cherry-pick <commit-hash>

8. git commit --amend

Description: Modify the last commit.

What it does: Modifies the last commit message or adds more changes.

When to use it: When you make a small mistake in a commit.

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

Terminal window
git commit --amend -m "Corrected commit message"

No need for an extra commit!

9. git range-diff

Description: Compare commit ranges.

What it does: Compares two commit ranges.

When to use it: When reviewing a rebase before pushing changes.

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

Terminal window
git range-diff origin/main HEAD

This ensures nothing important is lost.


Managing the Working Directory

10. git stash

Description: Save uncommitted changes temporarily.

What it does: Temporarily saves uncommitted changes.

When to use it: When switching branches but keeping your work.

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

Terminal window
git stash

Switch branches, then restore it with:

Terminal window
git stash pop

11. git sparse-checkout

Description: Checkout specific parts of a repository.

What it does: Allows checking out only specific parts of a repository.

When to use it: When dealing with large monorepos.

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

Terminal window
git sparse-checkout init --cone

Then include specific folders:

Terminal window
git sparse-checkout set backend/

Debugging and Auditing Code

12. git bisect

Description: Find the commit that introduced a bug.

What it does: Helps find which commit introduced a bug.

When to use it: When debugging regressions.

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

Terminal window
git bisect start

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

13. git blame

Description: Identify who modified each line.

What it does: Shows who last modified each line in a file.

When to use it: When investigating why a piece of code changed.

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

Terminal window
git blame config.yaml

Now you know whom to ask!


Conclusion

By mastering these Git commands, you can save hours every week, work more efficiently in Git, and streamline your workflow. Whether you are a Software Engineer, DevOps Engineer, or GitHub user, these commands will help you undo changes, manage branches, optimize commits, and debug code like a pro.

Related Posts

Check out some of our other posts

Dotfiles: A Git-Based Strategy for Configuration Management

Dotfiles: A Git-Based Strategy for Configuration Management

Keeping your dotfiles — those hidden configuration files that dot your home directory — both safe and easily accessible across various machines can seem daunting. However, with the strategy outlined b

read more
Git SSH Keys for GitHub, GitLab, and Bitbucket on Windows

Git SSH Keys for GitHub, GitLab, and Bitbucket on Windows

Introduction Git connects to remotes by default via HTTPS, which requires you to enter your login and password every time you run a command like Git pull or git push, using the SSH protocol. You m

read more
Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux

Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux

Introduction Git connects to remotes by default via HTTPS, which requires you to enter your login and password every time you run a command like Git pull or git push, using the SSH protocol. You m

read more
What is a CI/CD?

What is a CI/CD?

Introduction Continuous Integration and Continuous Delivery are two of the most important concepts in DevOps. In this article, we will discuss what is a CI/CD and how it can help you to improve yo

read more
How Version Number Software Works

How Version Number Software Works

Introduction Introducing new package versions in systems with a lot of dependencies may rapidly turn into a headache. You run the risk of experiencing version lock if the dependency specifications

read more
What is DevOps?

What is DevOps?

What is DevOps, and why is it important? The name "DevOps" is a combination of the terms "development" and "operations," although it refers to a far broader range of principles and procedures than

read more