DEV Community

Cover image for Exploring Lesser-Known but Essential Git Commands
JohnKagunda
JohnKagunda

Posted on

Exploring Lesser-Known but Essential Git Commands

While many are familiar with basic commands like git add, git commit, and git push, there are several lesser-known commands that can be equally important. Understanding these commands can help you become more efficient and effective in your version control practices.

1. git stash

Imagine you're working on a feature, and suddenly you need to switch branches to fix a critical bug. You don't want to commit incomplete work, but you also don't want to lose your changes. This is where git stash comes in handy.

Command:

git stash
Enter fullscreen mode Exit fullscreen mode

This command temporarily shelves (or stashes) your changes, allowing you to switch branches without committing. Later, you can reapply your stashed changes with:

git stash apply
Enter fullscreen mode Exit fullscreen mode

2. git cherry-pick

Have you ever committed a bug fix on one branch and then realized that the same fix is needed on another branch? Instead of copying and pasting the changes, you can use git cherry-pick.

Command:

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

This command applies the changes from a specific commit to your current branch. It’s like plucking a cherry from one tree and placing it on another!

3. git bisect

When trying to find the commit that introduced a bug, git bisect can be a lifesaver. It uses a binary search algorithm to efficiently pinpoint the problematic commit.

Command:

git bisect start
git bisect bad
git bisect good <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Git will then check out different commits, asking you to mark them as good or bad, until it identifies the offending commit.

4. git reflog

Ever made a mistake and wished you could turn back time? git reflog is your time machine. It logs every change made to the tip of branches and allows you to recover lost commits.

Command:

git reflog
Enter fullscreen mode Exit fullscreen mode

You can then use git checkout <commit-hash> or git reset --hard <commit-hash> to revert to a previous state.

5. git blame

Want to know who made a specific change in a file? git blame can tell you. It shows the commit hash, author, and timestamp for each line in a file.

Command:

git blame <file>
Enter fullscreen mode Exit fullscreen mode

This can be useful for tracking down when a change was introduced and who made it.

6. git clean

When your working directory gets cluttered with untracked files, git clean can help you tidy up.

Command:

git clean -f
Enter fullscreen mode Exit fullscreen mode

The -f flag stands for force, as Git wants to ensure you really want to remove those files. For added caution, use the -n flag to perform a dry run and see what would be removed:

git clean -n
Enter fullscreen mode Exit fullscreen mode

7. git log --graph --oneline

If you want a visual representation of your commit history, git log --graph --oneline is perfect. It provides a concise, graphical view of your branch history.

Command:

git log --graph --oneline
Enter fullscreen mode Exit fullscreen mode

This is particularly useful for understanding complex branching and merging histories.

8. git shortlog

For a summary of contributions by author, git shortlog is very helpful. It groups commit messages by author and can give you an overview of who has contributed what.

Command:

git shortlog
Enter fullscreen mode Exit fullscreen mode

Add the -s flag to see a summary:

git shortlog -s
Enter fullscreen mode Exit fullscreen mode

9. git diff

While git diff is commonly known, many don't use it to its full potential. It shows changes between commits, commit and working tree, etc.

Command:

git diff
Enter fullscreen mode Exit fullscreen mode

To compare changes between two branches:

git diff branch1..branch2
Enter fullscreen mode Exit fullscreen mode

10. git show

git show is a versatile command that shows various types of objects, including commits, trees, and tags.

Command:

git show <object>
Enter fullscreen mode Exit fullscreen mode

For instance, to see the details of a specific commit:

git show <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Conclusion

By incorporating these lesser-known but powerful Git commands into your workflow, you can enhance your productivity and make your version control more effective.


Feel free to explore these commands in your projects, and don't hesitate to refer back to this guide whenever you need a quick refresher. Git is an indispensable tool, and mastering it can significantly improve your development experience.

Top comments (0)