Skip to content

Instantly share code, notes, and snippets.

@jschementi
Created January 31, 2009 18:44
Show Gist options
  • Save jschementi/55621 to your computer and use it in GitHub Desktop.
Save jschementi/55621 to your computer and use it in GitHub Desktop.
Git cheatsheets

Add colors to your ~/.gitconfig file:

[color]
  branch = auto
  diff = auto
  status = auto
[color "branch"]
  current = yellow reverse
  local = yellow
  remote = green
[color "diff"]
  meta = yellow bold
  frag = magenta bold
  old = red bold
  new = green bold
[color "status"]
  added = yellow
  changed = green
  untracked = cyan

Highlight whitespace in diffs

[color]
  ui = true
[core]
  whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol

Add aliases to your ~/.gitconfig file:

[alias]
  st = status
  ci = commit
  br = branch
  co = checkout
  df = diff
  lg = log -p

Configuration

Sets your email for commit messages.

git config user.email johndoe@example.com

Sets your name for commit messages.

git config user.name 'John Doe'

Tells git-branch and git-checkout to setup new branches so that git-pull(1) will appropriately merge from that remote branch. Recommended. Without this, you will have to add –track to your branch command or manually merge remote tracking branches with “fetch” and then “merge”.

git config branch.autosetupmerge true

You can add “–global” after “git config” to any of these commands to make it apply to all git repos (writes to ~/.gitconfig).

Branching

Create local branch

git branch search

Push local branch

git push origin search

Delete local branch

git branch -d search

Delete remote branch from remote repo (permanently)

git push origin :search

Remove remote branch from local repo

git branch -d -r origin/search

Rename local branch

git branch -m search dev/search

Track a single remote branch as a local branch

git checkout —track -b foo origin/foo

Pushing all local branches to remote, creating new remote branches as needed

git push —all origin

Merging

Merge a branch into the current branch

git merge foo

Merge from another local git repository

git pull ../another-git-repo/ master

Remote

Create a “bare” git repo (just the contents of the .git folder) in the current directory

git --bare init

Add a remote

git remote add origin git@github.com:jschementi/foo.git

Rebase

Move all your commits in a “topic” branch to the top of the log

git rebase master topic

Interactively rebase (reorder, combine, and delete commits)

git rebase --interactive master topic

Move all uncommitted commits on the current branch to the top of the log

git rebase

Commits

Delete the current commit

git reset --hard HEAD~1

Rewrite all commits to move entire repository into a sub-directory

git filter-branch --prune-empty --tree-filter '
  if [ ! -e bank ]; then
    mkdir -p bank;
    git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files bank;
  fi'

Rewrite all commits to change the email address for an author

git filter-branch --commit-filter '
  if [ "$GIT_COMMITTER_NAME" = "Jimmy Schementi" ];
  then
    # GIT_COMMITTER_NAME="<New Name>";
    # GIT_AUTHOR_NAME="<New Name>";
    # GIT_COMMITTER_EMAIL="<New Email>";
    GIT_AUTHOR_EMAIL="[email protected]";
  fi;
  git commit-tree "$@";' HEAD

Tags

Create a tag at the current commit

git tag -a this-is-a-tag

Push tags

git push --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment