9 Essential Git Tricks Every Developer Should Master
This guide presents nine practical Git techniques—including squashing commits, recovering lost history, cleaning workspaces, amending recent commits, partial file staging, protecting shared branches, undoing merges, purging files from history, and other handy commands—each explained with clear commands, warnings, and visual examples.
Squash multiple commits
Use interactive rebase to combine several commits. Run git rebase -i HEAD~N (replace N with the number of recent commits) and change the command from pick to squash or fixup for the commits you want to merge. After saving the todo list, edit the combined commit message if needed. An alternative shortcut for the last N commits is:
git reset --soft HEAD~N && git commit -m "new message"Recover a lost commit or branch
If a rebase or accidental deletion removes a commit, locate its SHA‑1 hash with git reflog. Then restore it either by resetting the current branch: git reset --hard <sha> or by creating a new branch that points to the commit:
git checkout -b recovered <sha>Obtain a clean workspace
Discard all local modifications and return the working tree to the last committed state:
git reset --hard HEAD
# optionally remove untracked files
git clean -fdUse git checkout . to unstage changes without affecting untracked files.
Amend the most recent commit
To fix a typo, add forgotten files, or change the commit message, run: git commit --amend If you need to modify the content first, reset the HEAD one step back, make the changes, and recommit:
git reset HEAD~
# edit files
git add .
git commit -m "updated commit"Commit only part of a file
Use interactive staging to select specific hunks: git add -p For finer control you can edit the patch manually with git add -e or use the interactive menu git add -i.
Avoid rewriting shared remote branches
Never run history‑rewriting commands such as git reset --hard, git rebase -i, or git push --force on branches that are used by multiple collaborators. Instead create new commits that revert or adjust the undesired changes, e.g. git revert <sha>.
Undo a merge
For a local merge that has not been pushed, reset to the pre‑merge commit: git reset --hard <pre‑merge‑sha> If the merge is already on the remote, create a revert commit that undoes the merge: git revert -m 1 <merge‑sha> The -m 1 option tells Git which parent should be kept as the mainline.
Delete a file from the entire history
When a sensitive file (e.g., passwords, internal IPs) has been committed, purge it from all commits. The classic tool is git filter-branch:
git filter-branch --tree-filter 'rm -f passwords.txt' -- --allA faster, actively maintained alternative is git filter-repo:
git filter-repo --path passwords.txt --invert-pathsBecause this rewrites every commit, notify all developers; they must re‑clone or reset their branches to the new history.
Other useful commands
git bisect– binary search to locate the commit that introduced a bug. git blame – show the last author of each line in a file. git show-branch – visualise relationships among multiple branches. git subtree – split or merge sub‑repositories.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
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.
