# Cleanup .git http://gcc.gnu.org/ml/gcc/2007-12/msg00165.htmlgit repack -a -d --depth=250 --window=250# Reset to previous commitgit reset HEAD~# Reset to commitgit reset <commit hash> --hard# Checkout previous commitgit checkout HEAD~# Create new branchgit checkout -b# Revert changes to modified files (working changes)git reset --hard# New branch without git history & filesgit checkout --orphan# Show where git configs get definedgit config --show-origin -l# Undo last commit but don't throw away changesgit reset --soft HEAD^# List all git submodulesgit submodule--helper list# Pull from PRgit pull origin pull/<issue ID>/head# List remote branchesgit branch -a# Delete branchgit branch -d# Delete remote branchgit push origin --delete# Force overwrite git repo. https://stackoverflow.com/questions/10510462/force-git-push-to-overwrite-remote-filesgit push -f <remote> <branch># Reset to specific commitgit reset --hard <commit># Remove dir from gitgit rm --cached -r <dir># Rename previous commitgit commit --amend# Force push overwritegit push --force origin master# Hard reset a branchgit reset --hard <branch-name># Change remote. i.e. when making a fork of a clone to change upstream destination.git remote rename origin upstream; git remote rename nikitavoloboev origin# Change upstream to my name so it pushes theregit branch --set-upstream-to nikitavoloboev/master master# Show changes between commits. Where f5352 and be73 are unique commit hashes.git diff f5352 be73# Update submodulegit submodule update# Set PGP key for Git globally. <key> = fingerprint w/o spacesgit config --global user.signingkey <key>
# Delete commit from remote.# 1. Delete commit from local repogit reset --hard HEAD~1# 2. Delete commit from remote repo (can get commit using git log)git push -f origin last_known_good_commit:branch_name
## It is bad practice to rewrite history. Works best if no one has pushed commits on top of remote branch you want to rewrite history of.# 1. Rebase to commit you want to change (~1 means the first ancestor of the specified commit)git rebase -i <hash>~1# Can also do thisgit rebase -i HEAD~4 # where HEAD~4 = last 3 commits# 2. Rename pick to reword (in opened editor) & save/quit# 3. Change commit message in editor and save# 4. Overwrite and remove duplicate commitsgit push --force-with-lease
git config --global --add alias.pr '!f() { git fetch -fu ${2:-upstream} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f'git config --global --add alias.pr-clean '!git checkout master ; git for-each-ref refs/heads/pr/* --format="%(refname)" | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done'