Unlocking the Power of Git: 10 Lesser-Known Commands You Should Know

Unlocking the Power of Git: 10 Lesser-Known Commands You Should Know

Previously, we explored the commonly known Git commands that we use in our daily workflows. If you haven't read that yet, I recommend checking out this blog for a comprehensive understanding.

Introduction to Git: A Beginner's Guide

Today, we will delve into 10 lesser-known but highly powerful Git commands. Beyond the frequently used commands like git commit and git push, there are several hidden gems that can significantly enhance your workflow. Here are ten Git commands that are not commonly used but are extremely useful.

  1. git stash

Have you ever been working on something and needed to switch branches? git stash lets you save your changes temporarily and clean up your workspace. You can bring back those changes later when you're ready to continue.

git stash
git stash apply

Example

Scenario: You're working on a new feature but need to switch to another branch to fix a bug.

git stash
git checkout bugfix-branch
# Fix the bug and commit the changes
git checkout feature-branch
git stash apply
  1. git reflog

git reflog is super helpful when you need to find changes that aren't in the commit history. It shows a log of all changes made to the repository, even those that have been changed or deleted.

git reflog

Example

Scenario: You accidentally deleted a branch and need to recover it.

git reflog
# Find the commit hash before the branch was deleted
git checkout -b recovered-branch <commit-hash>
  1. git bisect

Finding the commit that caused a bug can be really hard. git bisect helps you quickly find the bad commit by doing a step-by-step search.

git bisect start
git bisect bad
git bisect good <commit>

Example

Scenario: You need to find the commit that introduced a bug.

git bisect start
git bisect bad
git bisect good <last-known-good-commit>
# Follow the prompts to test and mark commits as good or bad
  1. git cherry-pick

Want to use a specific commit from one branch in another branch? git cherry-pick lets you do that easily, without needing to merge the whole branch.

git cherry-pick <commit>

Example

Scenario: You want to apply a specific bug fix from one branch to another.

git checkout feature-branch
git cherry-pick <commit-hash>
  1. git clean

Over time, your working directory can get cluttered with untracked files. git clean helps you remove these files, keeping your workspace tidy.

git clean -f

-f stands for force clean

Example

Scenario: Your working directory is cluttered with untracked files.

git clean -f
  1. git blame

Wondering who made changes to a specific line in a file? git blame shows you the author and the commit for each line, making it easier to see who did what and understand the history of your code.

git blame <file>

Example

Scenario: You want to know who last modified a specific line in a file.

git blame main.py
  1. git shortlog

git shortlog provides a summary of the git log output in a more readable format, grouping commits by author and summarizing the commit messages.

git shortlog

Example

Scenario: You need a summary of commits grouped by author.

git shortlog
  1. git archive

Need to create a tar or zip file from your repository? git archive allows you to package your code in a variety of formats, making it easy to share or deploy.

git archive --format=tar HEAD > archive.tar

Example

Scenario: You need to create a zip file of your project for distribution.

git archive --format=zip HEAD > project.zip
  1. git describe

git describe gives a human-readable name to a commit, which can be especially useful for tagging releases or identifying specific builds.

git describe

Example

Scenario: You want to give a human-readable name to a commit.

git describe
  1. git fsck

Making sure your Git file system is in good shape is important. git fsck checks your repository for any problems and tells you what needs to be fixed.

git fsck

Example

Scenario: You want to check the integrity of your repository.

git fsck

Summary

In this blog we tried to understand 10 hidden git commands along with their actual use cases. By incorporating these commands into your workflow, you can unlock the full potential of Git and streamline your development process.

Happy coding! 💻✨