Hidden Secrets of Git : That You Never Knew

Anand
4 min readJun 7, 2024

--

Git, the powerful version control system, is a cornerstone of modern software development. While most devops engineer are familiar with its basic commands like git commit and git push, Git has many hidden features that can significantly enhance your workflow. Let's dive into some of the lesser-known secrets of Git that you might not be aware of.

1. The Power of Git Aliases

Creating Git aliases can save you a lot of time by shortening commonly used commands. For example, you can set up an alias for a complex log command.

git config --global alias.lg "log --color --graph --pretty=format:'%C(yellow)%h%Creset - %s %Cgreen(%cr)%Creset %C(bold blue)<%an>%Creset'"

Now, you can simply use git lg to see a beautifully formatted log.

2. Interactive Rebase for Cleaner Commits

Interactive rebase is a powerful tool for rewriting commit history in a more readable and organized manner. It allows you to edit, reorder, squash, and remove commits.

git rebase -i HEAD~n

Replace n with the number of commits you want to include in the rebase. This opens an editor where you can choose actions for each commit.

3. Stash Specific Files

Did you know you can stash specific files instead of stashing all changes? This is particularly useful when you want to stash only parts of your work.

git stash push -m "Stash message" path/to/file

Retrieve it later with:

git stash pop

4. Diff with Word Highlighting

When you need to review changes in detail, git diff with word highlighting can be invaluable. This shows changes within the lines, making it easier to spot exact differences.

git diff --color-words

5. Auto-Correcting Mistyped Commands

If you often mistype Git commands, you can enable Git’s autocorrect feature. This will automatically correct minor typos in your commands after a short delay.

git config --global help.autocorrect 1

Now, if you type git comit, Git will automatically assume you meant git commit.

6. Check the Git History of a Single Line

To trace the history of a specific line in a file, use the git blame command. This can be useful for identifying when a particular change was made.

git blame -L <start>,<end> <file>

Replace <start> and <end> with the line numbers you’re interested in, and <file> with the file name.

7. Work with Submodules

Submodules allow you to include and manage repositories inside other repositories, which is great for large projects with dependencies.

git submodule add <repository_url>

Update submodules with:

git submodule update --remote

8. Using Git Bisect to Find Bugs

Git Bisect helps you find the commit that introduced a bug by performing a binary search through your commit history.

git bisect start
git bisect bad # Mark the current commit as bad
git bisect good <commit> # Mark an earlier commit as good

Git will check out different commits between good and bad until it finds the problematic one.

9. Reflog: The Ultimate Undo

The reflog keeps track of all changes to the tip of branches and other references, making it possible to recover from almost any mistake.

git reflog

You can reset your branch to any previous state recorded in the reflog:

git reset --hard HEAD@{n}

Replace n with the appropriate entry number from the reflog.

10. Git Hooks for Automation

Git hooks are scripts that run automatically at certain points in the Git workflow. They can be used for automating tasks like code quality checks, testing, and more.

  • pre-commit: Runs before a commit is made.
  • post-commit: Runs after a commit is made.
  • pre-push: Runs before changes are pushed.

Place your scripts in the .git/hooks/ directory.

#!/bin/sh
# Example pre-commit hook script
echo "Running pre-commit checks..."
# Add your commands here

Conclusion

These hidden secrets of Git can greatly improve your efficiency and effectiveness as a developer. By incorporating these tips and tricks into your workflow, you can harness the full power of Git, making your version control practices more robust and streamlined.

Do you have any other Git secrets up your sleeve? Share them in the comments below!

Happy Learning!

About Author: An experienced IT professional worked in major IT companies for more than 20+ years as Solution Architect with core expertise on DevOps and Cloud. To get trained in DevOps from experts like Anand visit https://www.svsitsolutions.in or contact in WhatsApp

Other Interesting Blogs from the Author:

# Avoiding Git Pitfalls: Common Mistakes and How to Fix Them

# 10 Surprising Facts About Git That You Probably Didn’t Know

# Cinematic Experience with GIT : Lights, Camera, Action for Your Code!

# Learning Teaser — Git in a Few Hours: A Comprehensive Training Guide

# Unboxing GIT fundamentals: Part 1 of 10

--

--