Skip to content

Instantly share code, notes, and snippets.

@abmmhasan
Last active March 4, 2024 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abmmhasan/885f72b0f323408eec5edc7b7afa8507 to your computer and use it in GitHub Desktop.
Save abmmhasan/885f72b0f323408eec5edc7b7afa8507 to your computer and use it in GitHub Desktop.
Git Cheatsheet

Basic

## Unstage files
git restore --staged .

## Change last commit message (1st one will open editor)
git commit --amend
git commit --amend -m "New commit message"

## Add another repo in existing
git remote add <remote name/alias> <clone_url>

# Push/pull from another repo (after add)
git pull <remote name/alias> <remote_branch>

Advanced

## Stage deleted files only
# https://git-scm.com/docs/git-ls-files
git ls-files --deleted | xargs git add --all

# Stage delete files of an specific directory (lib/foo)
git ls-files --deleted -- lib/foo | xargs git add --all

Information

## Initial commit
git rev-list --max-parents=0 HEAD

## Latest tag
git describe --tags --abbrev=0

## Parsable changelog (version to version)
# here 3.22 & HEAD indicates version/tag
git --no-pager log --no-merges 3.22..HEAD --pretty=">>%h|%ce|%ct|%s|%b|%N" --shortstat

## Get number of changes between 2 commit
git diff --shortstat dfd7386d1a808f5852fb33aa7aa13e78a6c3b561 HEAD

## Get list of changed files between commits/branches
git --no-pager diff HEAD..master --name-only # e.g. getting list of changes between current head & master

Opertional

## Force LF instead of CRLF
git config --global core.autocrlf false
git config --global core.eol lf

## Prune
git remote update origin --prune
git fetch -p && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do git branch -D $branch; done

Check stats with git-fame python library

# need python & pip installed in your OS, then run following
pip install git-fame

# Set global alias (windows)
git config --global alias.fame "!python -m gitfame"

# Set global alias (Linux/Mac; may not need)
# You may have to set python3 instead of python
git config --global alias.fame '!python -m gitfame'

# Restart terminal

# Get Normal Counter on Surviving code (low accuracy)
git fame -e --enum

# Get Deep Counter on Surviving code (intra-file & inter-file modifier detector, file system independent) more calculation time
# You can combine --branch=<branch_name> (default is HEAD) for branch/tag specific info
git fame -ewMC --enum

# Check `git fame -h` command for command details 

You can add CODEOWNERS with this library as well

# bash syntax function for current directory git repository
owners(){
  for f in $(git ls-files); do
    # filename
    echo -n "$f "
    # author emails if loc distribution >= 30%
    git fame -esnwMC --incl "$f" | tr '/' '|' \
      | awk -F '|' '(NR>6 && $6>=30) {print $2}' \
      | xargs echo
  done
}

# print to screen and file
owners | tee .github/CODEOWNERS

# same but with `tqdm` progress for large repos
owners \
  | tqdm --total $(git ls-files | wc -l) \
    --unit file --desc "Generating CODEOWNERS" \
  > .github/CODEOWNERS

Visual Git History with git-story python library

# need python & pip installed in your OS, then run following

# if windows
winget install ffmpeg
python -m pip install manim
python -m pip install gitpython
python -m pip install git-story

# if linux
sudo apt install build-essential python3-dev libcairo2-dev libpango1.0-dev ffmpeg
pip install manim
pip install gitpython
pip3 install git-story

# Restart terminal

# Generate with default config
git-story

# To customize the options check
git-story -h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment