Clue Mediator

Mastering Git: Hidden Commands Every Developer Should Know

📅December 24, 2024
🗁Git

Git is an essential tool for every developer, and while most developers know the basic Git commands, there are some lesser-known commands that can save time and make version control smoother. In this blog post, we’ll dive into some of these hidden gems that can help you master Git.

Hidden Git Commands to Boost Your Workflow

1. git reflog: Track All Your Movements

The git reflog command allows you to view the history of your Git HEAD, which tracks all changes to the current branch. This is super helpful if you’ve lost commits or if you’re trying to undo something. It shows a detailed log of where your HEAD and branch pointers have been.

Example:

git reflog

This will list all the previous positions of your HEAD, and you can use this history to recover lost commits or reset your branch to a previous state. For example, if you want to go back to a specific commit:

git checkout <commit_hash>

2. git stash: Save Your Work Without Committing

The git stash command temporarily saves changes that you don’t want to commit yet but still need to switch branches. It’s like setting your work aside for a bit and picking it back up later. It’s great when you’re in the middle of a task and need to quickly jump into something else.

Example:

git stash

When you’re ready to get back to your work, simply use:

git stash pop

This will bring back your stashed changes, so you can continue where you left off. You can also see a list of your stashes with:

git stash list

And apply a specific stash:

git stash apply stash@{2}

3. git bisect: Find the Commit That Broke Your Code

Ever wonder when a bug was introduced into your codebase? Git’s bisect command can help you find the exact commit that caused the issue. It performs a binary search through your commit history, which can save you hours of debugging.

Example:

git bisect start
git bisect good <last_known_good_commit>
git bisect bad <commit_with_bug>

Git will then guide you through the process of narrowing down the commit that caused the problem. After identifying the problematic commit, you can end the search with:

git bisect reset

This will bring you back to your current branch.

4. git clean: Remove Untracked Files

Sometimes, your workspace gets cluttered with untracked files that you don’t need. Instead of manually deleting them, git clean can do the job for you. It’s a quick way to clean up your repository.

Example:

git clean -f

This will remove all untracked files from your working directory. You can also add the -d flag to remove untracked directories.

If you want to see what files will be removed without actually deleting them, use the -n (dry-run) flag:

git clean -n -d

This will list the files that would be deleted, allowing you to review them before taking action.

5. git log --oneline: View a Compact Log of Commits

Sometimes, you don’t need all the detailed information in a Git log. The git log --oneline command gives you a compact, easy-to-read summary of all commits.

Example:

git log --oneline

This will show you a simple list of commits with their hash and message. If you’re working with a large project, this helps you quickly navigate through the history without too much clutter.

You can also filter the log to show commits by a specific author:

git log --oneline --author="Author Name"

6. git cherry-pick: Apply a Commit from Another Branch

If you want to apply a specific commit from one branch to another, git cherry-pick allows you to do just that. This can be useful when you want to bring over a feature or bug fix without merging the entire branch.

Example:

git cherry-pick <commit_hash>

This will apply the changes from the specified commit to your current branch.

7. git diff --stat: Get a Summary of Changes

When you're curious about how much has changed between two commits or between your working directory and the repository, git diff --stat gives you a high-level summary.

Example:

git diff --stat

This will show you how many files were changed, added, or deleted, along with a summary of the lines added and removed.

Conclusion

These hidden Git commands can make your life as a developer much easier by streamlining your workflow and saving you time. The next time you're working in Git, try these out and see how they improve your productivity.

"The more you learn, the more you realize how much you don't know. Keep coding!"