You are on page 1of 2

Quick Reference

TM

COMMAND OVERVIEW

Concepts and Definitions Initial Setup


repository a project tracked by Git, git config --global user.name "Foo Bar"
consisting of commits & branches, git config --global user.email
usually stored with project files and "foo.bar@example.com"
directories in a working directory ssh-keygen -t rsa
working directory cat ~/.ssh/id_rsa.pub
aka. working tree or workspace, Then copy and paste the output to
the directory containing a working your SSH keys on the remote server.
copy of project files and directories
index aka. cache or stage, Creating a New Repository
staging area for building a commit mkdir myrepo
of changes in the working directory
cd myrepo
commit history
git init
a database storing past commits
# create or add files
commit a snapshot or record of
changes to files in the working echo "hello" > foo.txt
directory at some point in time git add .
branch a reference to a commit at git commit -m "initial commit"
the end of a chain of commits
HEAD a reference to the commit Push Existing Repo to Remote
that is currently checked out git remote add origin remote-repo
merge a commit joining divergent git push --all –u origin
development paths or branches
merge conflict Downloading a Repository
a condition that arises from a failed git clone remote-repo
automatic merge; requires manual where remote-repo is a path of the
editing to resolve the conflict form user@server:/path/to/repo

Ref Notation Viewing Changes


HEAD Reference to the commit git status View list of changed files
currently checked out git diff View changes to files in the
ref placeholder for branch, tag, working directory
or commit SHA-1 hash git diff --cached View changes in
ref^n the nth parent of ref, index from HEAD commit
where n=1 when omitted
(only merge commits Committing Changes
have multiple parents) git add file Add changes in file to index
ref~n the nth ancestor of ref, git commit Commit staged changes in
where n=1 when omitted the index to the local repo
ref@{n} the nth reflog entry of ref
git commit file
Examples: Same as above two commands,
HEAD^ denotes parent of HEAD except file must already be tracked
master~3 great grandparent of the To commit all changes to tracked files and
latest commit on master new or removed files:
HEAD~5^2 HEAD’s great-great-great git add --all
grandparent’s 2nd parent git commit -m "commit message"
HEAD@{1} previous value of HEAD
0c708f Refers to a commit by its Commit all changes (to tracked files only):
SHA-1 hash (unique ID) git commit -a -m "commit message"

Git logo by Jason Long licensed under Creative Commons


Attribution 3.0 Unported (bit.ly/cc-by). All other trademarks
are property of their respective owners. #1071
Quick Reference
TM

COMMAND OVERVIEW

Branches Pushing and Pulling


git branch branch git push Upload commits to default
Create new branch named branch upstream remote repository
at the HEAD (current commit) (To set default upstream:
git checkout branch git push -u remote branch)
Check out (i.e. switch to) branch git push remote branch
git checkout -b branch Push new commits on
Same as above two commands, i.e. branch to remote, e.g.
create new branch named branch git push origin master
at current commit and check it out git pull Pull latest changes from
git branch -d branch origin (does fetch & merge)
Delete branch named branch git pull remote branch
Pull latest commits on
Merging branch from remote
To merge branch2 into branch1:
git checkout branch1 branch2
branch1
Restoring Files
git merge branch2 git checkout commit -- file
Restore file from the given commit
git checkout HEAD -- file
Undoing Commits Discard uncommitted changes to file
git reset commit git reset --hard HEAD
Rewind current branch to commit, Discard all uncommitted changes
e.g. HEAD^ (never do this on
published commits!) Staging Files
git add file Add changes in file to index
git revert commit
Does not do what you would think git reset file Unstage file, i.e. remove file
it does – creates a new commit to from index, e.g. to keep it
undo changes of a previous commit from being committed
when you do git commit
Viewing History git checkout file git reset file

git log List commit history of


the current branch
Working
git log --oneline Show one per line Directory
Index History

git log --follow file Show history of file


git show ref View changes in commit git add file git commit

git blame file See who changed what


(and when) in given file Resolving Merge Conflicts
git diff A… B Compare two branches git status List the files with conflicts
vim file Edit files to fix conflicts…
Rebase
problematic areas are marked as follows:
Doing a rebase sequentially regenerates <<<<<<< HEAD
a series of commits onto another branch. text changed in current branch
=======
git checkout B text changed in other-branch
git rebase A Rebase branch B onto A >>>>>>> refs/heads/other-branch

Before A
B
After A B
…or use a dedicated merge tool:
git mergetool
Then, git add file to mark each file
git rebase --onto A C [B] resolved and finally git commit to
Rebase branch B starting at commit conclude the merge. Alternatively, run
C onto branch A. If B isn’t specified, git merge --abort to cancel the merge.
rebase up to and including HEAD.
git.cs.hofstra.edu

You might also like