Git is an essential tool for developers, enabling version control and collaboration on codebases. Mastering Git can significantly enhance your productivity and efficiency. Here are 15 Git command-line tricks that every developer should know, presented in a way that's easy to understand and enjoyable to read. Let's dive in!
1. Stashing Changes
Ever been in the middle of something and suddenly need to switch branches? git stash
is your friend! It temporarily saves your changes so you can work on something else.
git stash
To apply the stashed changes later:
git stash apply
2. Interactive Rebase
Want to clean up your commit history before merging? Interactive rebase lets you squash, reorder, or edit commits.
git rebase -i HEAD~n
Replace n
with the number of commits you want to go back.
3. Cherry-Picking Commits
Need to apply a specific commit from one branch to another? Cherry-pick to the rescue!
git cherry-pick <commit-hash>
4. Reverting Commits
Made a mistake? No problem! Revert a commit to undo its changes.
git revert <commit-hash>
5. Amending Commits
Forgot to add something to your last commit? Amend it!
git commit --amend
6. Viewing Commit History
Want a pretty view of your commit history? Use:
git log --oneline --graph --decorate --all
7. Finding Large Files
Identify large files in your repository with:
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
8. Alias Shortcuts
Tired of typing long commands? Create aliases!
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
9. Autocomplete
Enable Git autocomplete for your shell to save time.
For Bash:
source ~/.git-completion.bash
For Zsh:
source ~/.git-completion.zsh
10. Ignoring Files
Use .gitignore
to exclude files from version control. Generate a template with:
curl https://www.toptal.com/developers/gitignore/api/<OPERATING-SYSTEM>,<IDE>,<PROGRAMMING-LANGUAGE> > .gitignore
11. Viewing Changes
See what's changed in your working directory with:
git diff
Or compare branches:
git diff branch1..branch2
12. Stashing Specific Files
Stash only specific files:
git stash push -m "your message" path/to/file
13. Bisecting Bugs
Use git bisect
to find the commit that introduced a bug.
git bisect start
git bisect bad
git bisect good <commit-hash>
14. Reflog
Accidentally lost a commit? Use the reflog to recover it.
git reflog
15. Submodules
Manage dependencies with Git submodules.
git submodule add <repository-url> path/to/submodule
Conclusion
Mastering these Git tricks can make your development workflow smoother and more efficient. Whether you're stashing changes, rebasing commits, or using aliases, each trick adds a layer of control and convenience. Happy coding, and may your Git adventures be bug-free and productive! 🎉💻
This blog aims to make learning Git fun and engaging. If you found these tips helpful, share them with your fellow developers and let's all become Git masters together!
Follow me on GitHub for more tips and tricks: https://github.com/SOVANNARO
If you enjoy my content and find it helpful, you can support my work by buying me a coffee! Your support helps me keep sharing valuable knowledge. ☕❤️ buymeacoffee.com/sovannaro
Top comments (14)
Most of these are easier to do on IDEs or dedicated software. The best git related investment I've done was buying a Fork license. I started using it back when it was free and after it became paid, I chose it instead of GitKraken (which is also amazing) because GitKraken is a subscription.
Meanwhile, the best IDE related investment for me was IntelliJ, which I was so impressed by it that I and a few other devs decided to ask to add it to the department budget. All Devs in the company now have a license paid by the company
I'm curious, would you mind elaborating at all on why IntelliJ beats, say, VSCode?
I’ve been using IntelliJ for quite some time now, as it is also a part of the department budget. I’ve used VS code and atom as well.
I like IntelliJ’s color scheming, integrated DataGrip and automatic code formatting.
I like VS codes merge conflict manager, the search functionality and how lightweight it is.
To me, working in IntelliJ for 3 years, I have to say it is the superior IDE. Though, I have had some consistent issues with IntelliJ crashing and freezing during development of which was so consistent at some point that I had to switch to VS code just to get work done and stay focused.
I only had one catastrophic crash with IntelliJ. An update borked all my saved settings which was painful because I manage over 100 repos, but just deleting the new version settings files was enough to fix it. It was able to auto-reimport everything from the older version on the second try
VS Code is a glorified text editor; it is NOT an IDE. IntelliJ is an IDE and hands-down the best available. It might have some issues here and there, but VSC requires that you go plugin-diving to get any meaningful functionality. It also lacks some "basic" editing commands that IntelliJ has out-of-the-box, like incremental selection.
(I'm not here often, so I probably won't see any replies.)
Yeah, they went the opposite route of "batteries included" - the core functionality is there, then whatever language support or optional features you need are up to you. Microsoft has official extensions for the major stuff.
Code completion and finding code in IntelliJ is superior than VSCode. I've always struggled finding where is, for example, the CSS class defined when I'm in HTML while on VSCode. In IntelliJ ctrl+click works with more things than on VSCode. Refactoring code, renaming stuff, everything works better on IntelliJ. I like the colour coding of code better on IntelliJ too. Database integration is excellent and just simply works. You can run JPQL and native queries directly from the code. It will give you squiggly lines for columns that are not present in tables (as in a typo), etc. It's also so much easier to set up. I can open many projects built in many different tech stacks and it just knows what it should do. Since our projects are very eclectic, for lack of better words, IntelliJ is great for us. It's also possible to debug a web application directly in the IDE just like you would with the backend.
There are a few things I like VSCode more, though. I like how lightweight it is, so if I just need to quickly check out some code I still use VSCode. I like how clean the interface is too, but recently IntelliJ has this kind of interface too.
Makes sense, VSCode is very much not ready to use out of the gate. Switching for each tech stack should be doable with workspaces but I definitely understand not wanting to mess with that. Thanks for answering!
Using bisect is simply THE superpower
Any meaningful IDE would provide these with a click.
Maybe, but I'm going to chuckle if you aren't familiar with a good chunk of these, and I don't think I'm the only one (but I could be).
git add -p
Nope the two superpowers are interactive rebase and worktrees, worktrees help so much in projects where multiple people are working and you need to switch branches frequently
ok