Skip to content

Git

Terminal window
git remote add <name of new remote> <HTTPS or SSH URL>
git push <name of new remote> master
Terminal window
git log --all --decorate --oneline --graph
Terminal window
git checkout master
git reset --hard <old_commit_id>
git push -f origin master
Terminal window
git checkout master
git pull
git checkout different_branch
git rebase -Xtheirs master
git push --force

From here.

Terminal window
git diff-tree --no-commit-id --name-only <commit hash>
Terminal window
git diff file.json > file.patch
Terminal window
git show <commit hash> > commit.patch
Terminal window
git apply commit.patch

From here.

Terminal window
OLDIFS=$IFS; IFS=';' \
blocks=$(git diff | sed -n '/diff/,/(diff|$)/ {/diff / s/^/\;/; p}'); \
for block in ${blocks#;}; do \
echo "$block" > $(echo "$block" | head -n 1 | rev | cut -d "/" -f 1 | rev).patch; \
done; \
IFS=$OLDIFS
Terminal window
git stash show -p [stash@{N}]
Terminal window
git status -s | cut -d " " -f 3 | xargs -I {} git stash push {} -m "{}"
Terminal window
git stash list | cut -d ":" -f 1 | xargs -I {} git stash pop
Terminal window
git checkout -b new_branch
git checkout master
git reset --hard origin/master
Terminal window
git cherry-pick <commit hash>
Terminal window
git reset HEAD^ --hard
git push --force origin
Terminal window
git log -L :<function>:<file>
Terminal window
git config feature.manyFiles 1
Terminal window
git rev-list --all | ( while read revision; do git grep -F 'word' "$revision"; done; )
Terminal window
git push origin --delete branch/name
Terminal window
git rebase --onto HEAD~9 --exec "git commit --amend --reset-author --no-edit" HEAD~9
Terminal window
git rebase --interactive
Terminal window
git log --all -i --grep='something'
Terminal window
git grep 'something' $(git rev-list --all)
  • -d: recurse into directories as well
  • -f: go ahead with deletion
  • -n: dry-run
Terminal window
$ git clean -dn
Would remove mdbook
Would remove public/snippets/
Terminal window
$ git clean -df
Removing mdbook
Removing public/snippets/
Terminal window
git log --pretty=format:'%an' <file> | sort | uniq -c | sort -u | sort -n