Git Cheatsheet
Ignoring Local Modifications to Tracked Files: git update-index --skip-worktree
When you need to modify a Git-tracked file locally but don't want those changes to show up in git status or in commits (e.g. a local application-local.yml, .env, or IDE configuration), you can use --skip-worktree to make Git ignore local modifications to that file.
Marking and Unmarking
Listing Ignored Files
Marked files are shown with an S flag in git ls-files -v:
Committing Changes to a Marked File
To commit new changes to the file, unmark it first, commit, then mark it again:
--skip-worktree vs --assume-unchanged
Both options stop Git from reporting modifications to tracked files, but they serve different purposes:
So for "ignoring local changes", use --skip-worktree.
Caveats
--skip-worktreeonly affects the working tree — it is not a permanent ignore. Operations that rewrite the file, such asgit pull,git checkout, orgit stash, may overwrite your local changes or cause a conflict. If the file is also likely to change upstream, unmark it before pulling.- It only applies to tracked files; for untracked files, use
.gitignore.
Stashing Unfinished Work: git stash
When you need to switch branches mid-task, use git stash to set your working-tree changes aside:
Amending the Latest Commit: git commit --amend
When you commit and then realise you forgot a file or want to change the message:
Note: if the commit has already been pushed, prefer adding a new commit instead of rewriting shared history.
Undoing Changes: git restore and git reset
To discard all uncommitted changes (including the staging area):
--hard also resets the working tree, so double-check that nothing you want to keep would be lost.
Recovering Lost Commits: git reflog
When a commit seems to have vanished after a bad git reset --hard or git rebase, reflog records every place HEAD has been:
As long as the commit still exists in the object database, reflog can get you back to it.
Bisecting a Regression: git bisect
When you don't know which commit introduced a bug, git bisect uses binary search to locate it:
Git checks out a midpoint commit; you just mark each as good or bad until it narrows down the offending commit:
Friendlier History: git log
Cleaning Up Untracked Files: git clean
-n only shows what would be deleted without deleting it — preview before you execute.
Picking Commits: git cherry-pick
Apply a specific commit from another branch onto your current branch:
Deleting Merged Local Branches
This deletes all local branches merged into the current branch (except the current one). Preview with git branch --merged first.