Git
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 X amount of commits
git checkout master
git reset --hard <old_commit_id>
git push -f origin master
Replay changes on master
to some other branch
Beware of blindly accepting any incoming changes in favor of your own. From here.
git checkout master
git pull
git checkout different_branch
git rebase -Xtheirs master
git push --force
Show changed files in specified 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 individual files when running git diff
in a repository
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 every changed file with a message equaling the filename
git status -s | cut -d " " -f 3 | xargs -I {} git stash push {} -m "{}"
Pop every entry from the stash back to the working tree
git stash list | cut -d ":" -f 1 | xargs -I {} git stash pop
Move unpushed commits to a new branch
Pull latest changes from 'origin/master' if haven't already. From here.
git checkout -b new_branch
git checkout master
git reset --hard origin/master
Copy commit to current branch
git cherry-pick <commit hash>
Undo pushed commit that nobody has yet pulled
git reset HEAD^ --hard
git 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 specific 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 (unpushed) commits (e.g. 9)
Set correct user name and email prior to this.
git rebase --onto HEAD~9 --exec "git commit --amend --reset-author --no-edit" HEAD~9
Re-order commits
Oldest commit will be at the top. Move commit down with ddp
. Move commit up with ddkP
git rebase --interactive
Search for 'something' in a commit message
git log --all -i --grep='something'
Search for 'something' through all commits' 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 -dn
Would remove mdbook
Would remove public/snippets/
$ git clean -df
Removing mdbook
Removing public/snippets/