Git Cheatsheet

A watered-down description of what a bunch of git commands do.

git config

Change default options for Git, can set your username, email, etc.

Syntax

git config [options]

Examples

# Set the username for a repo.
git config user.name = "Jaime"

# Set the email for all repos.
git config --global user.email = "Jaime"

git clone

Download a repo.

Syntax

git clone [options] [URL] [folder]

Examples

# Download the "noti" repo to the current folder.
git clone git@github.com:variadico/noti.git

git remote

Add a remote server to send code to.

Syntax

git remote add [name] [url]

Examples

# Send code to a server called "origin" at the URL
# "git@github.com:variadico/noti.git".
git remote add origin git@github.com:variadico/noti.git

git init

Create a local repository.

Syntax

git init [options] [folder]

Examples

# Create a repo in the current folder.
git init

# Create a repo for receiving pushes from people.
git init --bare

git status

View information about the current repo, like added or modified files.

Syntax

git status [options]

Examples

git status
git status --short

git log

Show a list of all the commits for a repo.

Syntax

git log [options] [range]

Examples

git log
git log --oneline

# Show the last 5 commits.
git log HEAD~5..HEAD

git add

Mark the files you intend to save with git commit.

Syntax

git add [options]

Examples

git add hw6.cpp

# Marks all files in repo.
git add --all

git commit

Create save points.

Syntax

git commit [options]

Examples

# Create a save point with a short message.
git commit -m "Fix the crash"

# Create a save point with a long message. This will open vim, write the change
# in the file, then save and quit like normal.
git commit

git push

Upload code to some server.

Syntax

git push [options] [remote server] [branch]

Examples

git push origin master
git push origin fix-bug

git pull

Download code updates from server, if there is any.

Syntax

git pull [options] [remote server] [branch]

Examples

push pull origin master

git branch

Create parallel branches of your code.

Syntax

git branch [options]

Examples

git branch --list

# Create branch called "fix-bug".
git branch fix-bug

# Delete branch.
git branch -D fix-bug

git checkout

Restore changes from branches.

Syntax

git checkout [options]

Examples

# Throw away all your changes since the last commit.
git checkout main.cpp

# Switch to branch "fix-bug".
git checkout fix-bug

git diff

Show the differences between commits.

Syntax

git diff [options] [range]

Examples

# Show differences between the current code and the previous commit.
git diff HEAD~1

# Show differences between 2 commits ago and the previous commit.
git diff HEAD~2..HEAD~1

git merge

Merge code from another branch to current branch.

Syntax

git merge [options]

Examples

git merge fix-bug
git merge make-faster --ff-only

# Cancel merging a branch. Useful when you get a merge conflict.
git merge --abort

git stash

Temporarily save changes, but don't create save point.

Syntax

git stash
git stash list
git stash pop

Examples

# Temporarily save changes and reset files back to previous commit.
git stash

# Restore temporarily saved changes and modify current files.
git stash pop

git rebase

Catch up to changes in another branch.

Syntax

git rebase [options]

Examples

# Grab all the commits from the branch "master", add them to the current
# branch, and put the current branch's commits on top.
git rebase -i master

git fetch

Download changes from a server, but don't apply them to the current repo.

Syntax

git fetch [options]

Examples

# Download all branches from the remote server.
git fetch --all

git reset

Undo changes.

Syntax

git reset [options] [server/branch]

Examples

# Unmark files that were previously marked with "git add".
git reset

# Undo all local changes and commits; restore the copy from the branch "master"
# on the server "origin".
git reset --hard origin/master

# Restore the save point called "f83ff1af77b13de918adb880fbcdce657ee7cae7".
# Undoes all local changes and commits.
git reset --hard f83ff1af77b13de918adb880fbcdce657ee7cae7