Fundamentals 10 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
10 Powerful Git Tricks Every Developer Should Master

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
fi

Using 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.pyc

3. 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]
git blame example
git blame example

4. Reviewing Repository History

Enhance git log with options: --oneline – compact one‑line output. --graph – text‑based graph of history. --all – show all branches.

git log options
git log options

5. Never Lose a Commit

After a hard reset, use git reflog to view all HEAD movements and recover lost commits.

git reflog example
git reflog example

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]
git add -p interface
git add -p interface

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.

interactive rebase
interactive rebase

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-found

10. Cherry‑Pick Command

git cherry-pick

copies a specific commit from one branch to another, useful for applying bug fixes across multiple branches.

git cherry-pick [commit_hash]
cherry-pick example
cherry-pick example

Conclusion

These Git tips can significantly boost your version‑control workflow; the tool is versatile enough to handle any task you can imagine.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

software developmentTutorial
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.