Skip to main content

Git Cheat Sheet

This cheat sheet features the most important and commonly used Git commands for easy reference.

Installation and GUIs

Git for all Platforms - http://git-scm.com/GitHub for Windows/Mac - http://desktop.github.com/ For Linux and Solaris platforms, the latest release is available on the official Git web site.

Setup Configuration

Configuring user information used across all local repositories

`git config --global user.name "[firstname lastname]"`set an author name to be used for all commits by the current user`git config --global user.email "[valid-email]"`set an author email that will be associated with each history marker`git config --global --edit`open the global configuration file in a text editor for manual editing
34

Git Basics and Stage

Working with repositories and the Git staging area

`git init`initialize an existing directory as a Git repository`git clone [url]`retrieve an entire repository from a hosted location via URL`git status`list which files are staged, unstaged, and untracked`git add [file]`stage all changes in working directory for the next commit`git reset [file]`unstage a file while retaining the changes in working directory`git commit -m "[descriptive message]"`commit your staged content as a new commit snapshot`git log`show all commits in the current branch’s history`git diff`diff of what is changed but not staged`git diff --staged`diff of what is staged but not yet committed
56
78

Branch and Merge

Isolating work in branches, changing context, and integrating changes

`git branch`list your branches. A * will appear next to the currently active branch`git branch [branch-name]`create a new branch at the current commit`git checkout -b [branch-name]`create and check out a new branch. Drop the -b flag to checkout an existing branch`git merge [branch-name]`merge the specified branch’s history into the current one
branch 1branch 2
branch 3

Git Log

Examining logs and object information

`git log --oneline`condense each commit to a single line`git log --stat`include which files were altered and the relative number of lines that were added or deleted from each of them`git log branchB..branchA`show the commits on branchA that are not on branchB`git log --author="[name]"`search for commits by a particular author`git log -- [file]`only display commits that have the specified file
log 1 log 2

Rewrite History

Rewriting branches, updating commits and clearing history

`git rebase [branch]`apply any commits of current branch ahead of specified one`git reset`reset staging area to match most recent commit, but leave the working directory unchanged`git reset --hard [commit]`clear staging area, rewrite working tree from specified commit

Share and Update

Retrieving updates from another repository and updating local repos

`git remote add [alias] [url]`create a new connection to a remote repo. After adding a remote, you can use [name] as a shortcut for [url] in other commands`git fetch [alias]`fetch down all the branches from that Git remote`git pull`fetch and merge any commits from the tracking remote branch`git push [alias] [branch]`transmit local branch commits to the remote repository branch`git push [alias] --all`push all of your local branches to the specified remote
share 1share 2

Temporary Commits

Temporarily store modified, tracked files in order to change branches

`git stash`save modified and staged changes`git stash list`list stack-order of stashed file changes`git stash pop`write working from top of stash stack`git stash drop`discard the changes from top of stash stack
stash 1stash 2
stash 3stash 4

Git Tags

References to specific points in Git history

`git tag [tagname]`create a lightweight tag`git tag -a [tagname] -m "[message]"`create an annotated tag`git tag`list stored tags in a repo`git checkout [tagname]`view the state of a repo at a tag`git push [alias] [tagname]`tags have to be explicitly passed to git push`git tag -d [tagname]`delete a tag
tag 1tag 2