10 Powerful Git Tricks Every Developer Should Master
This tutorial explores advanced Git techniques—including auto‑completion, .gitignore usage, blame, log options, reflog, partial staging, interactive rebase, stash, fsck, and cherry‑pick—providing clear commands, examples, and screenshots to help developers manage repositories more efficiently.
1. Git Auto-Completion
If you find typing Git commands tedious, enable auto‑completion by downloading the script and sourcing it in your ~/.bash_profile:
cd ~
curl /git-completion.bash -o ~/.git-completion.bash if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fiUsing the command line unlocks the full power of Git.
2. Ignoring Files in Git
Create a .gitignore file and list patterns for files or directories you do not want Git to track, using ! to specify exceptions.
*.pyc *.exe my_db_config/ !main.pyc3. Who Modified My Code?
Use git blame to see the author, commit hash, and timestamp for each line in a file.
git blame [file_name]
4. Reviewing Repository History
Enhance git log with options: --oneline – compact one‑line output. --graph – text‑based graph of history. --all – show all branches.
5. Never Lose a Commit
After a hard reset, use git reflog to view all HEAD movements and recover lost commits.
6. Staging Partial Changes
Use git add -p to interactively select hunks for staging, choosing y, n, e, d, or s for each hunk.
git add -p [file_name]
7. Squashing Multiple Commits
Run an interactive rebase to combine several commits into one. git rebase -i HEAD~2 Mark the earlier commit with pick and the later with squash, then edit the commit message.
8. Stashing Uncommitted Changes
Save unfinished work with git stash, list stashes with git stash list, and restore with git stash apply (optionally specifying a stash identifier).
git stash git stash list git stash apply git stash apply stash@{2}9. Checking Lost Commits
When git reflog is insufficient, use git fsck --lost-found to locate dangling commits and recover them with git show or git merge.
git fsck --lost-found10. Cherry‑Pick Command
git cherry-pickcopies a specific commit from one branch to another, useful for applying bug fixes across multiple branches.
git cherry-pick [commit_hash]
Conclusion
These Git tips can significantly boost your version‑control workflow; the tool is versatile enough to handle any task you can imagine.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
