Git

Quick Reference

Rebase

Interactive:

git rebase -i HEAD~3

git stash

git stash
git stash push -m 'stash title'

git stash list

git stash apply STASH-NAME  # keeps stash
git stash pop STASH-NAME    # removes stash

git stash drop STASH-NAME   # delete named stash
git stash clear             # delete all stashes

git stash show -p stash@{0} # show diff

Remove a file without deleting

git rm --cached mylogfile.log

Remove uncommitted files

$ git clean -dn
$ git clean -df
$ git clean -df --dry-run

Rename a branch

git branch -m <new-name>             # renames current branch
git branch -m <old-name> <new-name>

Create a branch from a commit

git branch <name> <commit-id>
git branch <name> HEAD@{n}

Cleaning up

git repack -d
git gc --prune=now --aggressive
git fetch -p

Visualization

Command Line Graph:

git log --graph

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

Add by hunk

git add -p

Count Commits

git rev-list --count
git rev-list –count dev

Backup Untracked Files

git ls-files --others --exclude-standard -z |\
xargs -0 tar rvf ~/backup-untracked.zip

git diff

By time

git diff HEAD@{'2 months ago'}
git diff HEAD@{'2010-01-01 12:00:00'})

Commits between tags

git log --pretty=format:"%h; %s" v0.0.2...v0.0.3
git log --pretty=format:"%h; %s" dev...main

git push

Nicer Forced Push

Force push unless remote branch has already been updated

git push --force-with-lease

Get a single file from the repo

git archive --format=tar --remote=ssh://git@git-host.com/user/repo.git master distro.make | tar -xv

Create and apply patches

git format-patch -2
git format-patch HEAD~2..HEAD

git am *.patch
git am file.patch

Configuration

List all your configuration

git config --list