DEV Community

Cover image for đŸ”„10 Git Features You Might Not Know About
Anthony Max Subscriber for HMPL.js

Posted on

đŸ”„10 Git Features You Might Not Know About

Git is the backbone of modern software development, offering an unparalleled version control system that can handle everything from small personal projects to massive enterprise codebases. Yet, many of its features remain unexplored, even by experienced developers. Here are 10 lesser-known Git features that can help you work smarter and solve common problems effectively.

1. Pinpoint Bugs with git bisect

Debugging is often one of the most time-consuming tasks in software development. When you’re working in a large repository with hundreds (or thousands) of commits, figuring out exactly when a bug was introduced can feel like finding a needle in a haystack. This is where git bisect comes in.

git bisect uses a binary search algorithm to identify the problematic commit. Instead of manually checking commit by commit, you mark one commit as "good" (bug-free) and another as "bad" (where the bug is present). Git then splits the range of commits in half, asking you to test whether the middle commit is good or bad. This process continues until the exact commit causing the issue is found.

Example usage:

git bisect start
git bisect bad  # Mark the current commit as bad
git bisect good <commit>  # Mark a known good commit
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to test commits. Once you find the bad commit, you can stop the process with:

git bisect reset
Enter fullscreen mode Exit fullscreen mode

This tool is invaluable for debugging and ensures you focus only on relevant commits.


2. Modify Your Last Commit with git commit --amend

We’ve all been there - you make a commit only to realize you forgot to include a file, misspelled something, or need to tweak the commit message. Instead of creating an entirely new commit, you can use the --amend flag to update the last one.

For example, if you forgot to stage a file:

git add <missing_file>
git commit --amend
Enter fullscreen mode Exit fullscreen mode

Or, if you need to adjust the commit message:

git commit --amend -m "Updated commit message"
Enter fullscreen mode Exit fullscreen mode

This command doesn’t create a new commit but rewrites the previous one. Keep in mind that if you’ve already pushed the original commit to a shared branch, you’ll need to force-push (git push --force) after amending, which could affect other collaborators.


3. Optimize Your Repository with git gc

Over time, Git repositories can accumulate unnecessary data-dangling commits, unused objects, and more that bloats the repository size. git gc (garbage collection) is a maintenance command that cleans up your repository and improves its performance.

For a quick cleanup:

git gc
Enter fullscreen mode Exit fullscreen mode

For a more aggressive cleanup that prunes unused objects:

git gc --prune=now --aggressive
Enter fullscreen mode Exit fullscreen mode

This is particularly useful for large repositories or after making major changes, such as removing large files or branches. Note that git gc runs automatically sometimes, but you can manually invoke it when needed.


4. Search for Code Changes with git log -S

When working on a legacy codebase or debugging an issue, you might want to know when a specific line of code or string was added or removed. git log -S is perfect for this. Unlike a simple search through the code, git log -S scans the commit history for changes related to the string.

For example:

git log -S"error_message"
Enter fullscreen mode Exit fullscreen mode

This will display a list of commits where the specified string was introduced or removed. Combine it with --grep to narrow down results or -p to view diffs for each commit.


5. Manage Dependencies with Submodules

If your project relies on other Git repositories (e.g., libraries or shared tools), submodules are a great way to include them without duplicating their contents. Unlike simple file imports, submodules allow you to track a specific commit of the external repository while keeping it separate from your project’s history.

To add a submodule:

git submodule add <URL>
Enter fullscreen mode Exit fullscreen mode

After cloning a project with submodules, initialize and update them:

git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode

Managing submodules can be tricky, but they’re a powerful tool for keeping dependencies organized and version-controlled.


6. Temporarily Save Work with git stash

There are moments when you’re working on a feature but need to quickly switch to another task or branch. Committing unfinished changes isn’t ideal, so Git provides git stash, which temporarily saves your modifications.

Stash your changes:

git stash
Enter fullscreen mode Exit fullscreen mode

Later, apply them back:

git stash apply
Enter fullscreen mode Exit fullscreen mode

You can also list all stashes:

git stash list
Enter fullscreen mode Exit fullscreen mode

And drop (delete) a stash when it’s no longer needed:

git stash drop
Enter fullscreen mode Exit fullscreen mode

This is particularly useful when juggling multiple tasks or fixing urgent bugs.


7. Undo Changes with git restore

Git’s newer restore command simplifies undoing changes. Whether you want to discard modifications in your working directory or unstage a file from the index, git restore can handle it.

To discard changes in a file:

git restore <filename>
Enter fullscreen mode Exit fullscreen mode

To remove a file from the staging area:

git restore --staged <filename>
Enter fullscreen mode Exit fullscreen mode

Compared to older commands like git reset or git checkout, git restore is more intuitive and reduces the risk of accidentally altering other files.


8. Recover Deleted Files with git checkout

Sometimes, you accidentally delete a file before committing. Instead of recreating it manually, you can use git checkout to recover it from the last commit.

For example:

git checkout HEAD -- <filename>
Enter fullscreen mode Exit fullscreen mode

This retrieves the version of the file from your latest commit and restores it to your working directory. It’s a lifesaver when accidental deletions occur.


9. Identify Contributors with git blame

Collaboration in large projects often involves figuring out who wrote a specific piece of code. git blame shows you the author, commit hash, and timestamp for each line in a file:

git blame <filename>
Enter fullscreen mode Exit fullscreen mode

It’s particularly useful when investigating bugs or asking about design decisions. However, be mindful of how you approach team members—it’s better to use this command as a learning tool than for assigning blame!


10. Create Aliases for Common Commands

Typing out long Git commands repeatedly can be tedious. Thankfully, Git allows you to create aliases for frequently used commands. For example:

git config --global alias.co checkout
git config --global alias.st status
git config --global alias.last 'log -1 HEAD'
Enter fullscreen mode Exit fullscreen mode

Now, you can use git co instead of git checkout, git st for git status, and git last to see the latest commit. Aliases save time and make your workflow more efficient.


Conclusion

These 10 Git features go beyond the basics and can significantly enhance your productivity. Whether you’re debugging with git bisect, managing dependencies with submodules, or cleaning up your repository with git gc, these commands empower you to work more effectively.

Explore these features, integrate them into your routine, and take your Git expertise to the next level!


Like

Also, I will be glad if you support the project with your star. Thank you!

Star HMPL ☆

Top comments (0)