Git

$ git config --global init.defaultBranch main
$ git config --global merge.conflictstyle diff3

Ignore a file locally:

Sign a past commit

  • git rebase --interactive
  • When you’re on the commit, git commit --amend --no-edit -S

HELPFUL FLAGS

  • -w - ignore whitespace in commands that show diffs
  • --color-words - show words removed and added within lines

CONFIG

git config --global user.name "Whatev"
git config --global user.email "email@example.com"
git config user.name "Whatev"
git config user.email "email@example.com"

SETUP

git init
git clone [REPO URL]
git clone --depth 1 [REPO URL]

HISTORY

git log — show commit history
	-p — include diffs
        -p --full-history --first-parent - show diffs for merges too
git status — show state of working copy
git show [COMMIT HASH] - diffs
git diff [COMMIT HASH 1] [COMMIT HASH 2]

WORKING COPY

git diff — shows diffs in working copy
git checkout -- [FILE NAME] — undo local changes to file
git reset --hard HEAD — undo all local changes

STASH

git stash — stashes current changes
git stash save "stash name" — stashes under a name
git stash list — shows stashes
git stash pop — apply latest stash
git stash apply stash@{#}
git stash drop stash@{#}

COMMITTING

git add filenames — add files to the staging area
git reset filenames — remove files from the staging area
git rm filenames — record the removal of a file in the staging area
git add -A — add all new files
git add -p — pick add
git diff --cached — show diffs in staging area
git commit -m "my commit message"
git commit --amend -m "new message" — change the commit message OR the files
git commit --amend --no-edit —change the commit files, no message prompt
git commit --allow-empty -m "new message" — commit with only a message, no file changes

BRANCHES

git branch -v — list branches
git branch -va — list branches including remote
git branch [NEW BRANCH NAME] — creates a branch, doesn't switch to it
git branch -d [BRANCH NAME] — deletes
git branch -dr [REMOTE BRANCH NAME] — deletes a remote branch
git checkout [BRANCH NAME]
git checkout -b [BRANCH NAME] — create and check out a new branch
git checkout --track [REMOTE BRANCH NAME]
git merge [BRANCH NAME] — merges named branch into HEAD
git merge --no-ff [BRANCH NAME] — merges named branch always including a merge commit

REMOTES

git remote add [REMOTENAME] [REMOTEURL]
git remote -v —list remotes
git remote set-url [REMOTEURL] [URL]
git fetch [REMOTENAME] — download info on the state of that remote's branches
git pull — pull remote tracked branch to HEAD
git push — push HEAD to remote tracked branch
git push [REMOTENAME] [BRANCHNAME]
git push -u [REMOTENAME] [BRANCHNAME] — sets up tracking
git push --force [REMOTENAME] [BRANCHNAME]

HISTORY CHANGES

git revert [COMMIT HASH] — reverse the effects
git reset --hard [COMMIT HASH] — resets HEAD to there
git reset --soft [COMMIT HASH] — resets HEAD and moves changes back to staged
git rebase --interactive [COMMIT HASH] — lets you re-step through history to change files or comments
git rebase --continue — move past current step
git rebase --abort — cancel interactive rebase
git checkout [COMMIT HASH] file — overwrite a file with an older version of it

MISC

git cherry-pick [COMMIT HASH]
git tag -d [TAG NAME] -- delete a tag

Git command line setup in Zsh

Use oh-my-zsh with the git-extras plugin.

Git command line setup in Bash

  1. Show branch name in command line - https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt
    • I use the following PS1 instead - export PS1=’[\033[37m]\w[\033[00;35m]$(parse_git_branch)[\033[00m] # ‘
  2. Autocomplete branch names - https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion
    • Note that you don’t need to install this person’s project “git-flow-completion”, you just need to follow the wiki’s instructions to install bash-completion (it seems like it supports git branches out of the box)
  3. Set up command shortcuts A. For shortcuts that need autocomplete for branch names, set them up in .gitconfig and use them with git at the front so that autocomplete will work - see https://github.com/CodingItWrong/dotfiles/blob/master/.gitconfig - example: git b for git branch B. For shortcuts that don’t need autocomplete, you can set them up in .bash_profile and then you won’t need to type git in front - see https://github.com/CodingItWrong/dotfiles/blob/master/.bash_profile - example: gri for git rebase --interactive