Ignore changes to a tracked file:
git update-index --assume-unchanged <file>
Start tracking changes to an ignored file (above):
git update-index --no-assume-unchanged <file>
Undo the last commit:
git reset HEAD~1 --soft
Undo the first (unpushed) commit of a repository:
git update-ref -d HEAD
Verify objects in database and optimize:
git fsck && git gc --aggressive --prune=now
Change branch:
git checkout <branch name>
Create a new local branch based off your current one:
git checkout -b <branch name>
Water current branch with changes from other branch:
git merge <other branch name>
Squash feature branch changes into current one:
git merge --no-commit --squash <feature branch name>
Delete an unmerged local branch, i.e. force delete:
git branch -D <branch name>
Delete a remote branch:
git push <remote name> :<branch name>
Get branch names you may own:
git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n | grep <git username>
Create patch from current changes:
git diff > <patch name>.patch
Note: To ensure new files are added to the patch run git add .
and then git diff --cached > <patch name>.patch
. Add the --binary
flag when adding non-text files.
Apply patch:
git apply <patch name>.patch
Find the commit that deleted a file:
git log --full-history -- path/to/file.js
Search for commits that added/deleted a string:
git log -S 'string here'