Skip to content
Austin Kong edited this page Jan 8, 2018 · 5 revisions

git Cheatsheet

Useful links

Basics

Show status - uncommitted/added changes

git status

Compact status

git status -sb

Add all files

git add .

Commit changes

git commit

Commit short form

git commit -m "useful description"

Unstage changes

git reset HEAD -- <file>

View commit history

git log

Upload changes - local branch (master) -> remote (origin)

git push origin master

Download and merge changes - remote (origin) -> local branch (master)

git pull origin master

View commits

Show changes from last 2 commits

git log -p -2

Fancy

git log --oneline --decorate --graph --all

Pretty and fancy

git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Show commit statistics

git log --stat
git commit -a -m "commit all"
git commit -m "message"

Fetching

Downloads changes only, don't merge

git fetch origin

Merge changes after fetching

git merge origin/master

Fetch can download references to a new branch. Set up as

git checkout -b serverfix origin/serverfix

or merge locally

git merge origin/serverfix

can't push due to conflict? use with caution

git pull --rebase origin master

Branching

List branches

git branch
git branch --all

List remote tracking branches

git branch -all
git branch -vv

Activate/checkout branch

git checkout <branch>

Create branch

git branch <branch>

Create new branch and checkout

git checkout -b <new_branch>

Rename current branch

git branch -m <renamed_branch>

Delete branch

git branch -d <name>

Merging

merge onto current checked out branch

git merge <branch>

rebase (let branch catch up to master)

git checkout <branch>
git rebase <master>

git checkout <branch>

use meld or alike to fix merge conflicts

git mergetool

Creating and cloning repositories

Make a local copy of a repo

git clone git@example.com:my_project.git
cd my_project

Create remote repo

remote$
mkdir my_project.git
cd my_project.git
git init --bare

Add upstream original repo

git remote add upstream user@host/path/to/repo

Create local repo then add remote

local$
cd my_project
git init
git add *
git commit -m "My initial commit message"
git remote add origin git@example.com:my_project.git
git push -u origin master

Stashing

Hide uncommitted changes

git stash

View stashes

git stash list

Restore most recent

git stash apply

Restore a specific stash

git stash apply stash@{2}

Delete stash

git stash drop stash@{0}

Stash unapply

git stash show -p stash@{0} | git apply -R

Create branch from stash

git stash branch <new_branch>

Upstream/origin stuff

SO checkout remote git branch

Setup branch to remotely track (and shorthand)

git checkout -b [branch] [remotename]/[branch]
git checkout --track origin/serverfix

Set upstream branch to an existing branch

git branch -u origin/serverfix

Set to branch track upstream

git branch --set-upstream dimension origin/dimension 

list remote branches

git ls-remote origin

push CURRENTLY ACTIVE branch to repo can create a new branch at repo origin

git push origin <branch>

pull down a remote branch to CURRENTLY ACTIVE local branch MUST CREATE A LOCAL BRANCH FIRST OR IT WILL OVERWRITE THINGS

git checkout -b <branch>
git pull origin <branch>

Delete remote branch

git push origin --delete serverfix
git push origin --delete <name>

Show origin URL

git remote -v

Look at origin details

git remote show origin

Update origin URL

git remote set-url origin <URL>

More alias from z1024

alias.commend=commit --amend --no-edit
alias.glog=log --graph --abbrev-commit --decorate --all --format=format:"%C(auto)%h - %C(bold cyan)%aD%Creset - %an %C(green)(%ar) %C(auto)%d%n%s"
alias.graph=log --all --oneline --decorate --graph
alias.new=!git init && git commit -m Root
alias.please=push --force-with-lease
alias.stat=status --short --branch