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.
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
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>
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
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>
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
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
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
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
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
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.