Skip to content

Instantly share code, notes, and snippets.

@DrSensor
Last active March 9, 2022 15:53
Show Gist options
  • Save DrSensor/d7d005265009b38f2130adc27eae59be to your computer and use it in GitHub Desktop.
Save DrSensor/d7d005265009b38f2130adc27eae59be to your computer and use it in GitHub Desktop.
Git Tricks
[alias]
rescue = !git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\\ -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt
ignore = !files={$(echo \"$@\" | tr ' ' ,)}.gitignore && echo '<-' github:$files && curl -sL https://raw.githubusercontent.com/github/gitignore/master/$files >> .gitignore && echo '->' .gitignore || echo FAIL && :
license-osi = !curl -sL https://raw.githubusercontent.com/OpenSourceOrg/licenses/master/texts/plain/$1 > LICENSE
license = !curl -sL https://raw.githubusercontent.com/github/choosealicense.com/gh-pages/_licenses/$1.txt > LICENSE
fork = !git clone $1 $3 --depth 1 && pushd ${3:-$(basename $1 .git)} > /dev/null && git remote add upstream $(git remote get-url origin) && git remote set-url origin $2 && popd > /dev/null && :
modlink = !pushd $1 && repo=$(git remote get-url origin) && popd && git submodule add $repo $2 && rm $2 && ln -s $1 $2 && :
first-commit = !git log $(git rev-list --max-parents=0 HEAD)
untrack = !git rm --cached $@
[core]
pager = delta
[init]
defaultBranch = trunk
# GPG sign all things
git config push.gpgsign if-asked
git config commit.gpgsign true
git config tag.gpgsign true

Motivation

Hide some nasty stuff 💩 in Github

Placing magic notes on dark file

  1. Get the commit hash on specific commit
$ git log --all --grep='search_string' --pretty='format:%h'

5125aee
  1. Then use it to get blob hash of some specific file
$ git ls-tree 5125aee --abbrev filename_or_folder

100644 blob_or_tree 15561e5     filename_or_folder

Be carefull git ls-tree follow the format <unixPermission_mode> SP <blob/tree> SP <hash_object> TAB <file/folder>

  1. Make some notes
$ git notes --ref utopian add 15561e5

hint: Waiting for your editor to close the file...
  1. Push it to remote repository (refs/notes/* if you want to push all notes)
$ git push origin refs/notes/utopian

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.22 KiB | 626.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:DrSensor/git-notes.git
 * [new branch]      refs/notes/utopian -> refs/notes/utopian

After pushed, you can check it in the repository via this url https://github.com/`username`/`project`/tree/refs/notes/utopian

  1. To fetch it, you can use (refs/notes/*:refs/notes/* if you want to fetch all notes):
$ git fetch origin refs/notes/utopian:refs/notes/utopian
  1. Then you can inspect it using
$ git notes --ref=utopian show 15561e5

From github.com:username/project
 * [new ref]         refs/notes/utopian -> refs/notes/utopian

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment