20 Essential Git Command-Line Tricks for Developers โ
Git is an indispensable version control system that every developer should master. While graphical user interfaces can simplify certain tasks, the command line provides unmatched control, flexibility, and efficiency. In this blog post, weโll dive into 20 Git command-line tricks that can enhance your workflow, whether youโre working solo or collaborating with a team. From basic configurations to advanced operations, these tricks will help you navigate Git like a pro.
1. Configure Your Git Identity โ
Set up your name and email so your commits are properly attributed:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
๐ก Tip: Use --local
instead of --global
to apply settings to a specific repository only.
2. Undo the Last Commit (Soft Reset) โ
Made a mistake in your last commit? Undo it without losing your changes:
git reset --soft HEAD~1
This keeps your changes staged, allowing you to fix and recommit them.
3. Amend the Last Commit โ
Forgot a file or want to tweak the commit message? Amend your last commit:
git add .
git commit --amend -m "Updated commit message"
This replaces the previous commit with your updated changes.
4. Stash Changes Temporarily โ
Need to switch branches but arenโt ready to commit? Stash your changes:
git stash
Retrieve them later with:
git stash pop
5. Visualize Commit History โ
See your projectโs commit history in a graphical format:
git log --graph --oneline --all
This provides a clear, concise view of branches and merges.
6. Change Commit Author โ
Update the author of your most recent commit:
git commit --amend --author="New Author <newauthor@example.com>"
Note: This only affects the last commit; use other methods for older commits.
7. Diff Staged Changes โ
Check whatโs staged before committing:
git diff --staged
This shows the differences between your staged changes and the last commit.
8. Find Bugs with Bisect โ
Pinpoint the commit that introduced a bug using binary search:
git bisect start
git bisect bad # Current commit is bad
git bisect good <commit-hash> # A known good commit
Git will guide you through the history to find the culprit.
9. Interactive Rebase for Clean History โ
Rewrite your commit history for a tidier log:
git rebase -i HEAD~3
This lets you edit, squash, or reorder the last three commits.
10. Cherry-Pick Commits โ
Bring a specific commit from another branch into your current one:
git cherry-pick <commit-hash>
Perfect for applying targeted fixes or features.
11. List All Branches โ
View all local and remote branches:
git branch -a
This helps you keep track of whatโs available in your repository.
12. Clean Untracked Files โ
Remove untracked files and directories cluttering your working directory:
git clean -fd
๐ก Tip: Use -n
for a dry run to preview what will be deleted.
13. Set Upstream Branch โ
Link your local branch to a remote branch for easier pushing and pulling:
git branch --set-upstream-to=origin/main
14. Squash Commits โ
Combine multiple commits into a single, cleaner commit:
git rebase -i HEAD~n # Replace 'n' with the number of commits
Follow the interactive prompts to squash your commits.
15. View File at Specific Commit โ
Inspect a fileโs contents from a past commit:
git show <commit-hash>:path/to/file
This is great for reviewing historical versions.
16. Update .gitignore โ
Forgot to ignore certain files? Update .gitignore
and untrack them:
echo "node_modules/" >> .gitignore
git rm -r --cached node_modules/
git commit -m "Update .gitignore"
17. Revert a Commit โ
Undo changes from a specific commit without altering history:
git revert <commit-hash>
This creates a new commit that reverses the specified changes.
18. Fetch Metadata Only โ
Check whatโs available on the remote without downloading:
git fetch --dry-run
Useful for planning your next steps.
19. Blame a File โ
See who last modified each line in a file:
git blame path/to/file
A handy tool for tracking down the source of changes.
20. Reset a File โ
Discard local changes to a specific file and revert to the last commit:
git checkout -- path/to/file
Conclusion โ
Mastering these 20 Git command-line tricks can transform your development workflow, giving you greater control and efficiency. Whether youโre configuring your environment, managing branches, or debugging with tools like git bisect
, these commands are invaluable.
To take your Git skills even further, consider the art of writing clear, meaningful commit messages. Tools like penify-cli
can streamline this processโsimply run penify-cli commit
to generate contextual commit messages based on your staged changes, saving time and ensuring consistency.
Happy coding! ๐