Git Tips
Contents
Restore a deleted file or directory
- Find the commit that deleted the file:
git rev-list -n 1 HEAD -- <filename>
- Restore the file:
git checkout <commit ID>^ -- <filename>
Search a file's history
git log -S<string> -- <file path>
Diff a specific file
git diff <old commit> <new commit> -- <file path>
Find a file in history
git log --all --full-history -- **/<filename>.*
Delete a remote branch
git push origin --delete <branch name>
Set the upstream for a branch
git push -u origin <branch>
- If you don't want to push, you can do:
git branch --set-upstream-to <remote branch>
View changes in a stash
git stash show [-p stash ID]
- If used without the
stash ID
arg, it shows the changes in the stash at the top of the stack
Add a single file to a stash
Create a branch from a stash
git stash branch <branchname> <stashid>
Delete all branches except for the ones you want to keep
git branch | grep -v <branch to keep> | grep -v <branch to keep> | ... | xargs git branch -D
To keep the remote's changes in a merge (i.e. you want to wipe out local changes):
git merge --strategy-option theirs
Clean up stale remote references
Skip commit hooks
Clean up already merged branches
git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d
Show summary statistics of a diff
git diff --stat <rev1> <rev2>
Stash untracked files
Configure Editor
git config --global core.editor=<editor>