Show Menu
Cheatography

Git Basics Cheat Sheet (DRAFT) by

Basic commands for using Git

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Getting a Git Repository - p. 26

Initia­lizing Repository in Existing Directory
Go to directory
$ cd C:/Use­rs/­use­r/m­y_p­roject
Initialize .git
subdirectory
$ git init
Begin file tracking
$ git add *.c
$ git add LICENSE
Initial Commit
$ git commit -m <me­ssa­ge>
Cloning Existing Repository
Clone directory
$ git clone <ur­l> <di­rectory name>

Tracking File Status - p. 28

Tracked files
Files that were in last snapshot + any newly staged files
can be unmodi­fied, modified, or staged
Untracked Files
any files in working directory that were not in your last snapshot and are not in your staging area.
won't be included in your commit snapshots until explicitly told
$ git status
check status
$ git add <fi­le/­dir­ectory name>
track new file, stage file
If you modify a file after you run git add, you have to run git add again to stage the latest version of the file
$ git status -s
check status (short)
??
new untracked files
A
newly staged files
M
modified files
lefthand column indicates status of staging area and the right-hand column indicates status of working tree
Ignoring Files
repository might have a single .gitignore file in its root directory, which applies to the entire repository
additional .gitignore files in subdir­ect­ories possible
specifies which files are ignored by version control

View and Commit Changes - p. 33

git diff
view unstaged changes
git diff --staged
compare staged changes to last commit
If all changes are staged, git diff will give no output.
git difftool
view changes in graphical difftool
git commit
commit changes, launches editor for commit message
git commit -m
add message in command line
git commit -a
commit all changed files, skip staging
git rm
remove file from tracked files
git rm -f
force removal of already staged file
git rm --cached
remove file from Git track, keep it on hard drive
If you simply remove the file from working directory, it shows up under the “Changes not staged for commit­"
git mv file_from file_to
Move/R­ename File
Git doesn’t explicitly track file movement
 

Viewing the Commit History - p. 40

git log
list commits made in chrono­logical order
git log -p
show difference introduced in each commit
git log -2
show last two log entries
git log --stat
show abbrev­iated stats for each commit
git log --pretty
change log output format
pretty­=fo­rmat: "%h - %an, %ar : %s"
format specifier for log output
%h
abbrev­iated commit hash
%an
author name
%ar
author date, relative
%s
Subject
git log --graph
display ASCII graph for merging history
git log --since=x
limit log output to time x
git log --author=x
limit log output to author x
git log --all-­match
limit output to those that match all limiting patterns
git log -S
Only show commits adding or removing code matching string
-- path/file
limit log output to changes introduced in specified files
Is always the last option and is generally preceded by double dashes (--) to separate paths from the options
--no-m­erges
prevent display of merge commits