Git

Quick Ref

Rebase

Interactive:

git rebase -i HEAD~3

Add by hunk

git add -p

git stash

git stash save save-name

git stash list

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

git stash drop STASH-NAME
git stash clear  # deletes all

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

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

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

Remove a file without deleting

git rm --cached mylogfile.log

Cleaning up

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

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

Remove uncommitted files

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

Configuration

List all your configuration

git config --list

Creating Repositories

Make a Git repository that manages the existing working directory.

git init

Make a “bare” Git repository. It does not manage the directory it resides in. Used for creating remote repositories to push updates to.

git --bare init