Based on notes from Advanced Git.
git config [--global] core.editor "code --wait"
: Set text editorgit config [--global] rerere.enabled true
: Replay previously seen conflict resolution (helpful for long rebases)git config [--global] branch.<branch>.rebase true
: always rebase branch instead of merginggit config [--global] help.autoCorrect <#>
: auto-correct typos after #/10 secondsgit config [--global] alias.<aliasname> "git <args>"
: Alias a git commandgit config --global alias.lg "log --graph --pretty=format:'%C(auto)%h -%d %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
: pretty log (rungit lg
)
<commit>~[n]
: n-th ancestor of commit (n=1 if omitted, n=2 is parent of parent)<commit>^[n]
: n-th parent of commit (n=1 if omitted, only useful for merges
git add -p
: interactively stage commits by hunk
git stash --include-untracked
: stashes tracked and untracked changesgit stash save "DESCRIPTION"
: sets stash description (instead of last commit msg)git stash list
: list changesgit stash show stash@{#}
: show contents of a stashgit stash apply
: apply the latest stash without removing itgit stash apply stash@{#}
: apply a stashgit stash drop <optional stash@{#}>
: Remove a stashgit stash clear
: Remove all stashesgit stash branch <branchname> [optional stash@{#}]
: Start new branch from stashgit checkout <stash name> -- <filename>
: Grab a single file from a stash
git tag <tagname>
: Create a tag and use message from last commitgit tag -a <tagname>
: Create an "annotated" tag with your own message, author, date.git tag
: list tagnamesgit tag --points-at <commit>
: list tagnames that point to a commitgit show <tagname>
: Show message & diff for taggit push <tagname>
: push to remote
git branch
: list local branchesgit branch -r
: list remote branchesgit branch <new-branchname> <commit>
: Create a branch with at HEAD (useful for dangling commits)git branch -vv
: list branches with upstream and commit msg
git merge <feature-branch>
: merges into the current branchgit merge --no-ff <feature-branch>
Disables fast forward (makes merge commit even if no merge conflicts, useful if want to group related changes)
git log --graph --decorate --all --oneline
: draw graph of how branches relategit log --since="yesterday"
git log --since="2 weeks ago"
git log --name-status --follow -- <file>
: Shows log history for a file with that works across renames/moves"git log -grep=<regexp>
: log history matchinggit log --author=<author>
: log history forgit log --stat
: Summarize changes to each file in log messagegit log diff-filter=<F> --stat
: Log diffs with filter (A=Add, D=Delete, M=Modified)git log -n #
: Show the last # of commits
git show [commit]
: log message and diff of [commit], or HEAD if commit omittedgit show --stat
: adds per-file change summary for each filegit show <commit>:<file>
: show diff of single file in commit
git diff
: show unsaved changes diffgit diff --staged
: show staged changes diffgit diff A B
: diff from A to Bgit diff A..B
: diff from A&B's common ancestor to Bgit diff --merged <branch>
: show branches that have been merged into (useful for master)git diff --no-merged <branch>
: show branches not merged into
git checkout -- <filename>
: overwrites file changes in working area with staging area versiongit checkout <commit> -- <filename>
: copies file at commit to staging area & working areagit checkout -tb <branch>
: Create off HEAD and use HEAD as upstreamgit checkout -t origin/<branch>
: Checkout branch from origin and track origin's version
git push -u origin <branch>
: push HEAD to on origin
git cherry -v
: Show which commits haven't been pushed yet (are local only)
git fetch
: get changes without applying any
git pull
: git fetch && git mergegit pull --rebase
: git fetch && git rebase
git clean -i
: remove untracked file changes, interactivelygit clean -i -d
: also remove untracked folder changesgit clean --dry-run
: see what would be removed without removinggit clean --dry-run -f
: remove changes after checking with dry-run
git reset --soft <commit>
: moves HEAD to and stages changes to go backgit reset [--mixed] <commit>
: moves HEAD to and moves changes to go back to working areagit reset --hard <commit
: moves HEAD to and changes to go back disappear (use ORIG_HEAD to get it back)git reset [commit] -- <file>
: stages change to revert file to state at [commit] or HEAD if not suppliedORIG_HEAD
pointer to change after doing a reset (handy if need to revert a bad reset)git reset --merge ORIG_HEAD
: Reset a merge commit (--merge) preserves uncommitted changesgit reflog && git reset --hard HEAD@{#}
: jump back to previous state
git revert <commit>
: creates a commit reverting
git rebase <branch>
: Rebase current branch on top ofgit rebase -i <branch>
: interactive rebasepick
: keep this commitreword
: keep commit but reword messageedit
: keep but modify commitsquash
: combine commit with previous, edit messagefixup
: combine commit with previous and keep previous messageexec
: run command with previous commitdrop
: remove this commit
git commit --fixup <SHA> && git rebase -i --autosquash <SHA>^
: makes new commit a "fixup!" of and squashes fixup intogit rebase -i --exec "<cmd>" <commit>
: Runs after each commit (useful for unit testing)git rebase --abort
: Back out a rebasegit branch my_branch_backup
->git reset --hard my_branch_backup
: Create a copy of branch before rebasing, restore it later
git remote -v
: Show remotes
origin git@github.com:username/repo.git (fetch)
origin git@github.com:username/repo.git (push)
git remote add upstream https://github.com/ORIG_OWNER/REPO.git
: Add upstream (from fork)git remote rename origin upstream
: Rename origin remote to upstreamgit pull --rebase <remote> <branch>
: Pull in changes from 's
git clone git@github.com:username/repo.git
git grep -e <regexp>
: Searches tracked files including working areagit grep --cached -e <regexp>
: Only searches staged and commited changesgit grep -e <regexp> -- <file or path>
: limits regex to file/pathgit grep --line-number --heading --break -e <regexp>
: Better formatting
git checkout <branch> && git cherry-pick <SHA>
: Creates commit on with
git blame <filename>
: show who last touched a file line by linegit blame -w -M -C <filename>
: better format-w
: ignore whitespace-M
: follow moved/copied lines within file-C
: follow moved/copied lines outside file
git log -diff-filter=D -- <deleted_file>
: See what commit(s) deleted a filegit blame <commit>^ -- <deleted_file>
: Blame on file before it was deleted
git blame -L#,# -- <file>
: Limit blame for lines #-#git blame -L'/<regexp>/' <file>
: Limit blame to lines matching regexp
git bisect start <BAD_SHA> <GOOD_SHA>
: Find which commit broke something- If good:
git bisect good
- If bad:
git bisect bad
- If good:
git bisect run <test-cmd> <arguments>
: Automate bisect by providing to determine pass/fail (pass=retval0)git bisect run "grep -c <BAD_TEXT> <FILENAME>" <BAD_SHA> <GOOD_SHA>
: finds which commit added <BAD_TEXT> to
git init --template=<.git-tempalte path>
: initializes .git with .git settings from <.git-template path> (hooks, set up .gitignore)
- executable located in .git/hooks
- common: .git/hooks/pre-commit, .git/hooks/post-merge, .git/hooks/post-checkout
- git hook examples:
- https://github.com/pre-commit/pre-commit
- https://github.com/typicode/husky (javascript focused)
- premade .gitignores: https://github.com/github/gitignore
git lint
: https://github.com/sk-/git-lint
t
: activate file finderl
: jump to linew
: switch branch/tagy
: expand URL to canonical form (useful for permalink)i
: show/hide all inline notes
Setup:
- Install:
brew install hub
- Add to .bash_profile:
alias git=hub
Usage:
- Open a pull request:
git pull-request -h <branch>
- Open browser with issues:
git browse -- issues
ISSUE_TEMPLATE.md
: default contents when creating an issuePULL_REQUEST.md
: default contents when sending a PRCONTRIBUTING.md
: link to this doc in sidebare when creating a PR