Skip to content

Instantly share code, notes, and snippets.

@danielberkompas
Last active February 10, 2021 23:48
Show Gist options
  • Save danielberkompas/425a5798f8b9465199f9 to your computer and use it in GitHub Desktop.
Save danielberkompas/425a5798f8b9465199f9 to your computer and use it in GitHub Desktop.
Git Cheat Sheet
# Use these aliases in your gitconfig for awesomeness
# You can use them like so:
# git lg -> prettier logs
# git s -> "git status"
[alias]
lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
s = status -s
co = checkout
c = commit
a = add
[push]
default = simple
# ***************************
# Configuration
# ***************************
git config --global user.name "<your name>"
git config --global user.email <[email protected]>
git config --global core.editor "<command line program that will open your editor, e.g. 'vim'>"
# These settings should end up in a .gitconfig file in your user folder
# ***************************
# Commands
# ***************************
# Switch to a branch
git checkout <branch>
# Create a new branch
git checkout -b <branch>
# Check which branch you are currently on
git branch
# See what files have been changed since the last commit, and which ones are staged for commit
git status
# Add a file to the staging ground for the next commit
# e.g. `git add app test` (adds all changed files in app/ and test/ to your next commit)
git add <file or directory>
# Add specific changes to staging area, change by change
git add -p
# Create a commit
git commit -m "Message here"
# Or if you want to edit the message in your favorite editor, you can do:
git commit
# Reset a file to its state from the previous commit
git checkout <file>
# See commit history. There are also prettier ways to do this. See recommendations below.
git log
# Merge branches
git checkout <destination-branch-name>
git pull # make sure you have the latest changes
git merge <target-branch-name>
# Conflict resolution:
# 1. Fix conflicts in files that are listed in git
# 2. Add file to staging
git add <fixed-file>
# 3. Make a new commit
# Rebase branch:
# See http://www.git-scm.com/book/en/v2/Git-Branching-Rebasing
git rebase <target-branch>
# Push branch to remote:
git push <remote> <branch> # The main code hosting remote is always called "origin"
# Push everything up, assuming "origin" remote exists
git push --all
# Create a "tag", a snapshot of the codebase. Useful for versions, etc
git checkout <branch-you-want-to-tag>
git tag <name> # e.g. "git tag version2.0"
git push <remote> <tag-name> # git push origin version2.0

Recommendations:

  1. Git courses on Codeschool.
  2. Git docs. Very comprehensive and helpful.

Get the Github Windows App

If you tire of the command line or if less technical people have to use git, you might consider using the Github app. There's no requirement that you actually use it with Github, it works with any repo. Sourcetree should work as well.

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