Skip to content

Git

Various Git aliases from others

Push existing repository to new remote

Terminal window
git remote add <name of new remote> <HTTPS or SSH URL>
git push <name of new remote> master

Pretty-print branch graph

Terminal window
git log --all --decorate --oneline --graph

Move ‘master’ back

Terminal window
git checkout master
git reset --hard <old_commit_id>
git push -f origin master

Replay changes from ‘master’

Terminal window
git checkout master
git pull
git checkout different_branch
git rebase -Xtheirs master
git push --force

Show changed files in commit hash

From here.

Terminal window
git diff-tree --no-commit-id --name-only <commit hash>

Create patch file from diff

Terminal window
git diff file.json > file.patch

Create patch file from commit

Terminal window
git show <commit hash> > commit.patch

Apply patch file

Terminal window
git apply commit.patch

Bulk create patch files from files

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

Show diff of stashed hunk

Terminal window
git stash show -p [stash@{N}]

Bulk create separate stashes of files

Terminal window
git status -s | cut -d " " -f 3 | xargs -I {} git stash push {} -m "{}"

Pop every entry from the stash

Terminal window
git stash list | cut -d ":" -f 1 | xargs -I {} git stash pop

Move unpushed commits to new branch

Terminal window
git checkout -b new_branch
git checkout master
git reset --hard origin/master

Copy commit to current branch

Terminal window
git cherry-pick <commit hash>

Undo pushed commit that wasn’t pulled

Terminal window
git reset HEAD^ --hard
git push --force origin

View history of specific function in file

Terminal window
git log -L :<function>:<file>

Speed up Git for larger repositories

Terminal window
git config feature.manyFiles 1

Search through history for a word

Terminal window
git rev-list --all | ( while read revision; do git grep -F 'word' "$revision"; done; )

Delete remote branch

Terminal window
git push origin --delete branch/name

Bulk reset author of multiple commits

Terminal window
git rebase --onto HEAD~9 --exec "git commit --amend --reset-author --no-edit" HEAD~9

Re-order commits

Terminal window
git rebase --interactive

Search for string in a commit message

Terminal window
git log --all -i --grep='something'

Search for string in commit contents

Terminal window
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
Terminal window
$ git clean -dn
Would remove mdbook
Would remove public/snippets/
Terminal window
$ git clean -df
Removing mdbook
Removing public/snippets/

Find out number of changes for author

Terminal window
git log --pretty=format:'%an' <file> | sort | uniq -c | sort -u | sort -n