Skip to content

Git

Push existing repository to new remote

1
git remote add <name of new remote> <HTTPS or SSH URL>
2
git push <name of new remote> master

Pretty-print branch graph

1
git log --all --decorate --oneline --graph

Move master back X amount of commits

1
git checkout master
2
git reset --hard <old_commit_id>
3
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.

1
git checkout master
2
git pull
3
git checkout different_branch
4
git rebase -Xtheirs master
5
git push --force

Show changed files in specified commit hash

From here.

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

Create patch file from diff

1
git diff file.json > file.patch

Create patch file from commit

1
git show <commit hash> > commit.patch

Apply patch file

1
git apply commit.patch

Bulk create patch files from individual files when running git diff in a repository

From [here](https://raymii.org/s/tutorials/bash frame=“none”_bits_split_a_file_in_blocks_and_do_something_with_each_block.html).

1
OLDIFS=$IFS; IFS=';' \
2
blocks=$(git diff | sed -n '/diff/,/(diff|$)/ {/diff / s/^/\;/; p}'); \
3
for block in ${blocks#;}; do \
4
echo "$block" > $(echo "$block" | head -n 1 | rev | cut -d "/" -f 1 | rev).patch; \
5
done; \
6
IFS=$OLDIFS

Show diff of stashed hunk

1
git stash show -p [stash@{N}]

Bulk create separate stashes of every changed file with a message equaling the filename

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

Pop every entry from the stash back to the working tree

1
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.

1
git checkout -b new_branch
2
git checkout master
3
git reset --hard origin/master

Copy commit to current branch

1
git cherry-pick <commit hash>

Undo pushed commit that nobody has yet pulled

1
git reset HEAD^ --hard
2
git push --force origin

View history of specific function in file

1
git log -L :<function>:<file>

Speed up Git for larger repositories

1
git config feature.manyFiles 1

Search through history for a specific word

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

Delete remote branch

1
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.

1
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

1
git rebase --interactive

Search for ‘something’ in a commit message

1
git log --all -i --grep='something'

Search for ‘something’ through all commits’ contents

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

Various Git aliases

Here

Find out number of changes per author per file

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