- git diff staged files
bash
git diff --cached
- Undo local changes to file
bash
git restore filename.txt
- Get modified filenames
bash
git diff --name-only
# previous commit files
git diff HEAD^ HEAD --name-only
- Short status
bash
git status --porcelain
- Rank commiters for a file
bash
git shortlog -sne README.md
- Rollback to previous commit
bash
git reset --hard <commit-id>
- Search commit log for given text
bash
git log --all --grep='Build 0051'
- View commit and diff
bash
git show <commit-id>
- View all commits affecting a file
bash
git log --follow -- filename/filepath
- Cleanup branch commit history
bash
git reset --soft master
git add .
git commit
git push --force
- Pretty-print branch list
bash
git branch --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) %(color:green)(%(committerdate:relative)) [%(authorname)]' --sort=-committerdate
- Pretty-print git log graph
bash
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
- Pretty-print commit search
bash
git log --pretty="format:%Cgreen%H %Cblue%s" --name-status --grep
- Push current branch to origin
bash
git push --set-upstream origin $(git branch --show-current)
- Recursively get all files and sort based on git modified time
bash
git ls-tree -r --name-only HEAD -z | TZ=UTC xargs -0n1 -I_ git --no-pager log -1 --date=iso-local --format="%ad _" -- _ | sort
- List all remote branches (relative)
bash
git for-each-ref --format=' %(color:red)%(authorname)%(color:reset) %09 %(color:blue)[%(committerdate:relative)] %(color:green)%(refname)'
- Delete merged remote branches
bash
git branch -r --merged | grep -v '\*\|master\|main\|develop' | sed 's/origin\///' | xargs -n 1 git push --delete origin
- Delete 5 oldest remote branches
bash
git branch -r --sort=committerdate | head -n 5 | sed 's/ origin\///' | xargs git push origin --delete