Skip to content

Instantly share code, notes, and snippets.

@RedFlames
Last active October 9, 2022 02:00
Show Gist options
  • Save RedFlames/ce4eab93bc98df21e6d8cff7e7dd5c23 to your computer and use it in GitHub Desktop.
Save RedFlames/ce4eab93bc98df21e6d8cff7e7dd5c23 to your computer and use it in GitHub Desktop.
Useful git one-liners (...and multi-liners)

Enough cheat sheets out there, https://jdsalaro.com/blog/git-cheat-sheet.html looks interesting

This is just other voodoo I've used before

# Number of commits ahead / behind
git rev-list --left-right --count main...mybranch
# list branches with dates, latest committed at the bottom
git for-each-ref --sort=committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'

git for-each-ref --sort=committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)' --merged
git for-each-ref --sort=committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)' --no-merged

# both of the above two lines mashed together
( OPTS="--sort=committerdate refs/heads/ --format %(committerdate:iso)_%(refname:short)"; git for-each-ref $OPTS --merged=main | sed 's/:[0-9]\{2\} +0[0-9]00_/ -m- /g'; git for-each-ref $OPTS --no-merged=main | sed 's/:[0-9]\{2\} +0[0-9]00_/ - - /g' ) | sort -n
# aka
( 
    OPTS="--sort=committerdate refs/heads/ --format %(committerdate:iso)_%(refname:short)"
    git for-each-ref $OPTS --merged=main | sed 's/:[0-9]\{2\} +0[0-9]00_/ -m- /g'
    git for-each-ref $OPTS --no-merged=main | sed 's/:[0-9]\{2\} +0[0-9]00_/ - - /g'
) | sort -n
# absurd: grep in all commits
git rev-list --all | \
(
    while read revision; do
        git grep -F 'Something' $revision
    done
)
# list branches that touch a file or idk
git log --all --source -- CelesteNet.Client/Components/CelesteNetMainComponent.cs | grep -o "refs/heads/.*" | sort -u | xargs -I '{}' git log -1 --format=%aI%x20%S '{}' -- CelesteNet.Client/Components/CelesteNetMainComponent.cs | sort
# yoink specific PRs commits
for pull in 22 23 24 25 27; do 
     curl -s -H "Accept: application/vnd.github+json" -H "Authorization: token $PAT" "https://api.github.com/repos/0x0ade/CelesteNet/pulls/$pull/commits" > pr_$pull.txt
done

# dump out just the commit hashes from the jsons
for pull in 22 23 24 25 27; do
     commits="$(jq -r '.[]|.sha' < pr_$pull.txt)"
     echo "$commits"
done | tr -d '\r' > pr_commits.txt

# list commits since date X that weren't in the PRs
git log --no-merges --since=2022-01-19 --oneline --no-abbrev-commit | grep -v -f pr_commits.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment