10 Essential Git Tricks to Supercharge Your Workflow
This guide walks you through ten powerful Git techniques—including auto‑completion, .gitignore usage, blame, log options, reflog, partial staging, interactive rebasing, stash, fsck, and cherry‑pick—to help you manage code efficiently, recover lost commits, and streamline your development process.
1. Git Auto‑Completion
If you use the command line for Git, typing commands manually can be tedious; enable auto‑completion by running the following script on Unix:
cd ~
curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bashThen add these lines to your ~/.bash_profile:
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi2. Ignoring Files in Git
Create a .gitignore file and list patterns for files or directories you don’t want Git to track, using ! to negate patterns when needed.
*.pyc
*.exe
my_db_config/
!main.pyc3. Who Broke My Code?
When something goes wrong, use git blame [file_name] to see the author, commit hash, and timestamp for each line.
4. Viewing Repository History
Beyond git log, three useful options are: --oneline – shows a compact hash and message per commit. --graph – draws a text‑based graph of the history (ineffective for a single branch). --all – displays history of all branches.
Combining them yields concise visual output.
5. Never Lose Track of Commits
If you accidentally reset and lose commits, git reflog lists all HEAD positions on your local machine, allowing you to recover lost work that isn’t part of the remote repository.
6. Staging Partial Changes
Use git add -p [file_name] to interactively select hunks to stage, choosing y to stage, n to skip, e to edit, d to skip to next file, or s to split a hunk.
7. Squashing Multiple Commits
Run git rebase -i HEAD~[number_of_commits] (e.g., git rebase -i HEAD~2) to open an interactive list where you can mark older commits for squashing into a single one.
8. Stashing Uncommitted Changes
When you need to switch context, run git stash to save your work. List stashes with git stash list and restore with git stash apply (optionally specifying a stash identifier).
9. Finding Lost Commits
Besides git reflog, use git fsck --lost-found to locate dangling commits in large repositories, then inspect or merge them with git show [commit_hash] or git merge [commit_hash].
10. Cherry‑Pick
Use git cherry-pick [commit_hash] to apply a single commit from another branch onto your current branch, useful for fixing bugs across multiple branches without merging entire histories.
These ten tips can elevate your Git proficiency, helping you work faster, recover lost work, and maintain a clean project history.
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.
