Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Last active January 29, 2020 00:09
Show Gist options
  • Save trevnorris/b962236c0054a804d7285dcb5c047057 to your computer and use it in GitHub Desktop.
Save trevnorris/b962236c0054a804d7285dcb5c047057 to your computer and use it in GitHub Desktop.
cheat sheet of git commands I use

note: A lot of this assumes a repo has already been forked.

Basics

First make sure all your commits have your name/email:

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"

I like to set rerere to automatically track conflict resolution:

git config --global rerere.enabled true

Checking out a new repo:

git clone <REPO_LOCATION> -- DIRECTORY

Adding another remote to track changes from another project:

git remote add <REMOTE_NAME> <REMOTE_URL>

The default <REMOTE_NAME> after cloning a repo is origin. If the repo needs to track changes of a central repo that contains the combined code of all developers it's common to call this the upstream remote. Ex.

git clone [email protected]:trevnorris/node.git -- node
cd node
git remote add upstream https://github.com/nodejs/node.git

All added remotes can be printed with

git remote -v

Print branches from all remotes:

git branch -a

Check out and track a branch from the remote:

git checkout -b <LOCAL_BRANCH_NAME> <REMOTE_NAME>/<REMOTE_BRANCH>

If the remote branch is on origin then <REMOTE_NAME> can be excluded. Otherwise it should be done like this:

git checkout -b v10.x upstream/v10.x

Grab latest changes from the remote project without applying the changes:

git fetch <REMOTE_NAME>

NOTE: I generally don't use git pull since it tries to do things that can cause me headaches. Ex. automatically attempt a merge when there are conflicts. Because of this I only use git fetch and git merge manually.

Merge remote changes into currently checked out branch, but if there is a conflict then abort the merge:

git merge --ff-only <REMOTE_NAME>/<REMOTE_BRANCH>

Push changes to your forked repo:

git push origin <BRANCH_NAME>

It's important not to just use git push since that pushes all branches. Always include <REMOTE_NAME> and <REMOTE_BRANCH>.

Push changes from your local branch to a branch on <REMOTE_NAME>.

git push upstream <BRANCH_NAME>

Stage files to be committed:

git add <PATH>

Stage only part of the changes in a file to be committed:

git add -p <PATH>

View changes of files that have been staged:

git diff --cached

When resolving conflicts it's helpful to see a file at a specific version:

git show <COMMIT>:<PATH>

Not as Basic

I use these two alias to print out the commit list as a tree:

git config --global alias.glog 'log --decorate=short --oneline --graph'
git config --global lg 'log --format='%Cgreen%h%Creset %C(cyan)%an%Creset - %s %Cgreen(%cr)%Creset' --graph'

The two aliases output the following (terminal output is colorized):

$ git glog --all -5 v10.18.1
* 343ddffc1c (admin/master, admin/HEAD) doc: document process.std*.fd
* 4f11fb6410 test: add wasi test for symlink() and readlink()
* 7d5a86cc05 fs: do not emit 'close' twice if emitClose enabled
* 57bd715d52 doc: fix several child_process doc typos
* 85592e2818 src: reduce large pages code duplication
$ git lg --all -5 v10.18.1
* 343ddffc1c Harshitha KP - doc: document process.std*.fd (6 hours ago)
* 4f11fb6410 cjihrig - test: add wasi test for symlink() and readlink() (6 hours ago)
* 7d5a86cc05 Robert Nagy - fs: do not emit 'close' twice if emitClose enabled (22 hours ago)
* 57bd715d52 cjihrig - doc: fix several child_process doc typos (28 hours ago)
* 85592e2818 Gabriel Schulhof - src: reduce large pages code duplication (30 hours ago)

Here's a bash function to print out all branches for a given remote by date:

function git-branch-detail() {
  local ref="refs"
  [[ $1 ]] && ref="$ref/remotes/$1" || ref="$ref/heads"
  while read line; do
    if [[ -t 1 ]]; then
      printf "\033[1;34m${line}\033[0m\n";
    else
      printf "${line}\n";
    fi
    echo "  " $(git log --date=short -1 --format="%ad %h  %s" ${line})
  done <<< $(git for-each-ref --format='%(refname:short)' --sort=-committerdate $ref)
}

Example output (terminal output is colorized):

$ git-branch-detail upstream | head -10
admin/master
   2020-01-17 343ddffc1c doc: document process.std*.fd
admin/HEAD
   2020-01-17 343ddffc1c doc: document process.std*.fd
admin/v13.7.0-proposal
   2020-01-16 d1e8e6fc71 2020-01-21, Version 13.7.0 (Current)
admin/v12.15.0-proposal
   2020-01-17 115221595f wip notable changes
admin/canary-base
   2020-01-17 dd86db2364 tools: update v8 gypfiles

Or leave off the remote to print off only local branches:

$ git-branch-detail | head -10
master
   2019-08-21 f39ad8a91f http: fix event listener leak
v12.x
   2019-02-02 b4e670dc26 benchmark: remove unreachable return
v10.9.0-proposal
   2018-08-13 604bac1f2f 2018-08-15, Version 10.9.0 (Current)
trevnorris-emeritus
   2018-04-12 68d3143b21 doc: move trevnorris to TSC Emeritus
v8.10.0-proposal
   2018-01-24 d532080c49 2018-02-13 Version 8.10.0 'Carbon' (LTS)

This bash function returns you to the base directory of the repo:

function cdr() {
  cd "./$(git rev-parse --show-cdup)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment