Get started with 33% off your first certification using code: 33OFFNEW

Lesser known, but interesting git commands

3 min read
Published on 26th April 2024
Lesser known, but interesting git commands

Git, the version control system, is a treasure trove of commands designed to streamline every aspect of project management. While most developers are familiar with basic commands like git commit, git push, and git pull, Git offers a plethora of lesser-known commands that can significantly enhance your workflow. Let's dive into some of these hidden gems, providing a brief overview and official documentation links for each.

1. git bisect – Binary Search Through Commits

Finding the commit that introduced a bug can be like finding a needle in a haystack. git bisect simplifies this by using a binary search algorithm to quickly identify the offending commit.

How it works:

  • Start with git bisect start.
  • Mark the current version as bad with git bisect bad.
  • Mark a known good state (e.g., a commit where the bug was not present) with git bisect good [good_commit].
  • Git will then checkout a commit halfway between the good and bad commits. Test this version, then mark it as good or bad. Repeat until Git identifies the commit that introduced the bug.

Git bisect documentation

2. git reflog – A Safety Net for Lost Commits

Ever made a mistake and thought you lost commits? git reflog is your safety net, recording updates to the tips of branches and other Git refs. You can use it to find lost commits, undo rebase, or deleted branches.

Git reflog documentation

3. git cherry-pick – Apply Specific Commits Elsewhere

Need to apply a commit from one branch to another without merging the entire branch? git cherry-pick allows you to select specific commits and apply them to your current branch, which is incredibly useful for pulling in bug fixes or small feature additions.

Git cherry-pick documentation

4. git stash – Temporarily Shelve Changes

Working on a new feature but need to switch contexts quickly? git stash temporarily shelves changes so you can work on a different branch with a clean working directory. Retrieve your changes later with git stash pop.

Git stash documentation

5. git blame – Who Modified Each Line?

When trying to understand why a line of code exists or who last modified it, git blame comes in handy. It shows line-by-line revision history for a file, helping you understand changes and track down when a particular piece of code was altered.

Git blame documentation

6. git shortlog – Summarized Git Log

For a summarized view of the commit history, git shortlog groups commits by author, making it easier to see how many commits each contributor has made. It's particularly useful for generating release notes or understanding team contributions.

Git shortlog documentation

7. git fsck – File System Consistency Check

Ensure the integrity of your Git database with git fsck (file system consistency check). This command verifies the connectivity and validity of objects in the database, identifying any corruption or dangling objects.

Git fsck documentation

8. git rev-parse – Parse Git References

Need to find out the commit SHA for HEAD or resolve branch names to their commit SHAs? git rev-parse is a powerful utility that provides this information, aiding in scripting and Git internals exploration.

Git rev-parse documentation

9. git archive – Create an Archive of Files

Creating a zip or tar archive of your repository or a subset of files for distribution is straightforward with git archive. Specify the desired format and prefix, and you can generate archives directly from your repository without including the entire .git directory.

Git archive documentation

While the basics of Git get you through daily tasks, exploring its lesser-known commands can uncover more efficient, safer, and more precise ways to manage your repositories. From recovering lost commits to fine-tuning your commit history, these commands offer a deeper level of control and insight into your version control system. Leverage these tools to enhance your Git proficiency and streamline your development workflow.