Git
Various Git aliases from others
Push existing repository to new remote
git remote add <name of new remote> <HTTPS or SSH URL>git push <name of new remote> master
Pretty-print branch graph
git log --all --decorate --oneline --graph
Move ‘master’ back
git checkout mastergit reset --hard <old_commit_id>git push -f origin master
Replay changes from ‘master’
git checkout mastergit pullgit checkout different_branchgit rebase -Xtheirs mastergit push --force
Show changed files in commit hash
From here.
git diff-tree --no-commit-id --name-only <commit hash>
Create patch file from diff
git diff file.json > file.patch
Create patch file from commit
git show <commit hash> > commit.patch
Apply patch file
git apply commit.patch
Bulk create patch files from files
From here.
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
Show diff of stashed hunk
git stash show -p [stash@{N}]
Bulk create separate stashes of files
git status -s | cut -d " " -f 3 | xargs -I {} git stash push {} -m "{}"
Pop every entry from the stash
git stash list | cut -d ":" -f 1 | xargs -I {} git stash pop
Move unpushed commits to new branch
git checkout -b new_branchgit checkout mastergit reset --hard origin/master
Copy commit to current branch
git cherry-pick <commit hash>
Undo pushed commit that wasn’t pulled
git reset HEAD^ --hardgit push --force origin
View history of specific function in file
git log -L :<function>:<file>
Speed up Git for larger repositories
git config feature.manyFiles 1
Search through history for a word
git rev-list --all | ( while read revision; do git grep -F 'word' "$revision"; done; )
Delete remote branch
git push origin --delete branch/name
Bulk reset author of multiple commits
git rebase --onto HEAD~9 --exec "git commit --amend --reset-author --no-edit" HEAD~9
Re-order commits
git rebase --interactive
Search for string in a commit message
git log --all -i --grep='something'
Search for string in commit contents
git grep 'something' $(git rev-list --all)
Clean new untracked files and directories
-d
: recurse into directories as well-f
: go ahead with deletion-n
: dry-run
$ git clean -dnWould remove mdbookWould remove public/snippets/
$ git clean -dfRemoving mdbookRemoving public/snippets/
Find out number of changes for author
git log --pretty=format:'%an' <file> | sort | uniq -c | sort -u | sort -n