Fundamentals 9 min read

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.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
9 Essential Git Tricks Every Developer Should Master

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

Use 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' -- --all

A faster, actively maintained alternative is git filter-repo:

git filter-repo --path passwords.txt --invert-paths

Because 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.

Git rebase illustration
Git rebase illustration
Git reset illustration
Git reset illustration
Git revert illustration
Git revert illustration
Gitbest practicesrebaseVersion Controlresetrevertfilter-branchinteractive add
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

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.