# git

# Config

git config -l
git config --global -l
git config -e
git config --global user.name "Bob"

# Branch

git branch # list all branches
git checkout remote-brach-name  
git checkout -b new-branch-name # create and switch to new branch

# Undo local changes

git reset --hard HEAD~1   # permanently delete staged changes

Git reset options (opens new window)

# Housekeeping

git branch -d <branch>  # deletes <branch> locally
git push origin :<branch> # deletes <branch> remotely

git branch --merged # list merged branches
git branch --merged| egrep -v "(^\*|master|main)"   # exlude master/main
git branch --no-merged  # not merged branches

git branch -r   # list referenced remote branches
git remote prune origin # clean up outdated references

git branch --merged | egrep -v "(^\*|master|main)" | xargs git branch -d    # delete all local branches that are already merged into the currently checked out branch

git branch | grep -v "master" | xargs git branch -D # delete all branches but master

# Merging

Note

Merge master to a feature branch

git checkout feature-branch  
git merge master --no-ff

# Rebase

git checkout feature-branch
git rebase master   # fix any conflicts
git add .
git push -f

# Tag

git tag # list all tags
git tag <tag-name>
git push origin tag-name

git tag -d tag-name # delete tag

git clone --branch <tag> <repo> # git clone a specific tag

# Diff

# changes between current branch and master
git diff --name-status master | cat

# changes between current branch and remote master
git diff --name-status origin/master | cat

# commits that branch-X has but master doesn't
git log master..branch-X

# local changes
git diff index.js
git diff --shortstat commit1 commit2
git diff --stat commit1 commit2

# Searching

git branch --contains <commit>
git log --all --grep="Build 0051"  
git log -3 filename

# who has contributed to project?
git log --format='%aN' | sort -u    

# Patch

Apply a patch file:

git apply --stat fix.patch
git apply --check fix.patch
git apply fix.patch

# Github

ssh -T git@github.com # Test ssh connection

# Misc

git remote show origin
git log --pretty=format:"%ad - %an: %s" --after="2018-03-29" --until="2018-04-05"

Undo anything (opens new window)

Git CLI (opens new window)

Reset v Revert (opens new window)