CONTENTS
Get More Refcardz! Visit DZone.com/Refcardz
094
Why Get Git?
Getting Started
Cloning Existing Projects
The Typical Local Workflow
Getting Started With Git
BY MATTHEW MCCULLOUGH, UPDATED AND REVISED BY KURT COLLINS
The Remote Workflow...and much more!
WHY GET GIT?
The canonical reference for Git installers can be found on a
subpage of the official Git site at: git-scm.com/download.
There are a number of installers available there for those
that dont want to go through the hassle of doing the
install manually. In addition, also available at the official Git
download site are links to older distributions of the Git binary.
Git is a postmodern version control system that offers the
familiar capabilities of CVS or Subversion, but doesnt stop at
just matching existing tools. Git stretches the very notion of
version control systems (VCS) by its ability to offer almost all of
its features for use offline and without a central server. It is the
ESTABLISHING USER CREDENTIALS
brainchild of Linus Torvalds, with the first prototype written in a
Once you have selected a suitable distribution of Git for your
platform, youll need to identify yourself with a username and
email address to Git.
vitriolic two-week response to the BitKeeper debacle of 2005.
Today, developers everywhere are migrating in droves to this
exciting platform. Users reference its blistering performance,
In a separation of concerns most satisfying to the purist,
Git does not directly support repository authentication or
authorization. It delegates this in a very functional way to the
protocol (commonly SSH) or operating system (file system
permissions) that is hosting or serving up the repository. Thus,
the user information provided during your first Git setup on a
given machine is purely for credit of your code contributions.
usage flexibility, offline capabilities, and collaboration features
as their motivation for switching. Lets get started with Git.
Youll be using it like a master in no time at all.
DISTRIBUTED VERSION CONTROL
If you are familiar with one or more traditional or centralized
version control systems like Subversion, there will be several
With the binaries in your $PATH, issue the following three
commands just once per new machine on which youll be
using Git. Replace the username and email address with your
mental adjustments to make in your migration to Git. The
first is that there is no central server. The second is that there
is no central server. The full history of the repository lives
preferred credentials.
on every users machine that has cloned (checked out) a copy
of the repository. This is the essence of a Distributed Version
git config --global user.name yogi.bear
git config --global user.email yogi@jellystonepark.org
git config --global color.ui auto
Control System (DVCS).
Once over those hurdles, it is quite liberating to be able to work
entirely independently, versioning any new project that you
These commands store your preferences in a file named
start, even if in the incubation phase. The ease of setting up
.gitconfig inside your home directory (~ on UNIX and Mac, and
a new Git repository (or repo in common parlance) leads to
%USERPROFILE% on Windows).
GETTING
STARTED WITH
GIT 7
JAVA ENTERPRISE
EDITION
setting up repos everywhere. It feels frictionless.
If you are intrigued by all the potential nuances of a Git setup,
From there youll progress to the second epiphany of being able
GitHub, a web-based code hosting site, offers several in-depth
to share a repository and a changeset directly with a colleague
tutorials on setting up Git for Linux, Windows, and Mac. Here
without any complicated setup, without a commit/check-in to a
are several in-depth Git installation guides:
central server, direct network connectivity, or having to worry
help.github.com/articles/set-up-git/#platform-windows
about firewalls getting in the way. Git has done technologically
help.github.com/articles/set-up-git/#platform-mac
for version control what BitTorrent did for file sharing. It
help.github.com/articles/set-up-git/#platform-linux
permanently replaced the spoke and hub structure with a
peer-to-peer model, and theres no turning back. It supports
CRE ATING A REPOSITORY
transmitting binary sets of changes via USB stick, email, or in
Now that Git is installed and the user information established, you
the traditional style, over a network, but amazingly, via HTTP/S,
can begin creating new repositories. From a command prompt,
FTP/S, SCP, Samba, SSH, RSYNC or WebDAV. While some of these
change directories to either a blank folder or an existing project
have been deprecated (specifically FTP and RSYNC) due to their
that you want to put under version control. Then initialize the
inefficiencies, the number of synchronization options available
directory as a Git repository by typing the following command:
natively for Git repos are still astounding.
git init
touch README.md
git add .
git commit m The first commit
G E T T I N G S TA R T E D
INSTALLING GIT
Git has a very light footprint for its command line installation.
The first command in the sequence, init, builds a .git directory
For most platforms, you can simple copy the binaries to a
that contains all the metadata and repository history. Unlike
folder that is on the executable search $PATH. Git is primarily
many other version control systems, Git uniquely stores
written in C, which means there is a unique distribution for each
everything in just a single directory at the top of the project. No
supported platform.
pollution in every directory.
D Z O NE, INC.
DZ O NE.C O M
GETTING STARTED WITH GIT
Following the directory initialization, the next command creates
TREEISH
an empty file in the directory named README.md. You can skip
this part if you decided to create a repository from a directory with
DEFINITION
HEAD
The current committed version
HEAD^
One commit ago
HEAD^^
Two commits ago
HEAD~1
One commit ago
HEAD~3
Three commits ago
:/Reformatting all
Nearest commit whose comment begins
with Reformatting all
RELEASE-1.0
User-defined tag applied to the code when it
was certified for release.
files in it.
Next, the add command with the dot wildcard tells Git to start
tracking changes for the current directory, its files, and for all folders
beneath, if any exist.
Lastly, the commit function takes all previous additions and makes
them permanent in the repositorys history in a transactional action.
Rather than letting Git prompt the user via the default text editor,
the -m option preemptively supplies the commit message to be
saved alongside the committed files.
It is amazing and exciting to be able to truthfully say that you can use
the basics of Git for locally versioning files with just these commands.
C LO N I N G E X I S T I N G P RO J E C TS
An equally common use case for Git is starting from someone elses
repository history. This is similar to the checkout concept in
The complete set of revision specifications can be viewed
Subversion or other centralized version control systems. The
by typing: git help rev-parse.
difference in a DVCS is that the entire history, not just the latest
version, is retrieved and saved to the local users disk.
Treeish can be used in combination with all Git commands that
The syntax to pull down a local copy of an existing repo is:
accept a specific commit or range of commits. Examples include:
git log HEAD~3..HEAD
git checkout HEAD^^
git merge RELEASE-1.0
git clone git://github.com/matthewmccullough/hellogitworld.git
git clone http://github.com/matthewmccullough/hellogitworld.git
git clone git@github.com:matthewmccullough/hellogitworld.git
The protocol difference often signifies whether you have read-
T H E T Y P I C A L LO C A L W O R K F LO W
only or writeable access to the origin repository. The final syntax,
which accesses an SSH exposed repository, is the most common
EDITING
write-enabled protocol.
Once youve cloned or initialized a new Git project, just start
The clone command performs several subtasks under the hood. It
changing files as needed for your current assignment. There is no
sets up a remote (a Git repository address bookmark) named origin
pessimistic locking of files by teammates. In fact, theres no file
that points to the location git://github.com/matthewmccullough/
locking at all. Git operates in a very optimistic manner, confident
hellogitworld.git. Next, clone asks this location for the contents of
that its merge capabilities are a match for any conflicted changes
its entire repository. Git copies those objects in a zlib-compressed
that you and your colleagues can craft.
manner over the network to the requestors local disk. Lastly,
If you need to move a file, Git can often detect your manual relocation
clone switches to a branch named master, which is equivalent to
of the file and will show it as a pending move. However, it is often
Subversions trunk, as the current working copy. The local copy of
more prudent to just directly tell Git to relocate a file and track its
this repo is now ready to have edits made, branches created, and
new destination.
commits issued all while online or offline.
git mv originalfile.txt newsubdir/newfilename.txt
TREEISH & HASHES
If you wish to expunge a file from the current state of the branch,
Rather than a sequential revision ID, Git marks each commit with a
simply tell Git to remove it. It will be put in a pending deletion state
SHA-1 hash that is unique to the person committing the changes, the
and can be confirmed and completed by the next commit.
folders, and the files comprising the changeset. This allows commits
to be made independent of any central coordinating server.
git rm fileyouwishtodelete.txt
A full SHA-1 hash is 40 hex characters:
VIE WING
Daily work calls for strong support of viewing current and historical
64de179becc3ed324daab72f7238df1404723672
facts about your repository, often from different, perhaps even
To efficiently navigate the history of hashes, several symbolic
orthogonal points of view. Git satisfies those demands in spades.
shorthand notations can be used as listed in the table below.
STATUS
Additionally, any unique sub-portion of the hash can be used. Git will
To check the current status of a projects local directories and files
let you know when the characters supplied are not enough to be
(modified, new, deleted, or untracked) invoke the status command:
unique. In most cases, 4-5 characters are sufficient.
D Z O NE, INC .
DZ O NE .C O M
GETTING STARTED WITH GIT
git status
FIGURE 3: GIT LOG
FIGURE 1: GIT STATUS
BLAME
DIFF
If trying to discover why and when a certain line was added, cut to
A patch-style view of the difference between the currently edited
the chase and have Git annotate each line of a source file with the
and committed files, or any two points in the past can easily be
name and date it came into existence:
summoned. The .. operator signifies a range is being provided. An
omitted second element in the range implies a destination of the
git blame <filename>
current committed state, also known as HEAD:
git diff
git diff 32d4..
git diff --summary 32d4..
Git allows for diffing between the local files, the stage files, and the
committed files with a great deal of precision.
COM M AND
DEFINITION
git diff
everything unstaged diffed to the last commit
git diff --cached
everything staged diffed to the last commit
git diff HEAD
everything unstaged and staged diffed to the
last commit
FIGURE 4: GIT BL AME
STASHING
Git offers a useful feature for those times when your changes are in
an incomplete state, you arent ready to commit them, and you need
to temporarily return to the last committed (e.g. a fresh checkout).
This feature is named stash and pushes all your uncommitted
changes onto a stack.
git stash
You can also save your stash with a name. This is particularly useful
when you want to save more than one stash to the stack.
git stash save picnic_basket
To write the most recently stashed changes back into the working
FIGURE 2: GIT DIFF
copies of the files, simply pop them back off the stack. Keep in mind,
the following command also removes the changes from the stack.
LOG
The full list of changes since the beginning of time, or optionally,
git stash pop
since a certain date is right at your fingertips, even when
disconnected from all networks:
However, if you have multiple stashes on the stack, you can also list
all of the stashes by using:
git log
git log --since=yesterday
git log --since=2weeks
git stash list
D Z O NE, INC .
DZ O NE .C O M
GETTING STARTED WITH GIT
To supply the commit message directly at the command prompt:
Once youve found the stash you want to load, you can do it one of
two ways: either use the apply command or the pop command as in
git commit m <your commit message>
the following example.
To view the statistics and facts about the last commit:
git stash pop stash{n}
git stash apply stash{n}
git show
Take note of the n in between the brackets. The n corresponds to
the number of the stash you want to use. You can get the number by
use the git stash list command explained above. Again, if you use the
git stash pop version of the command, it will remove the code you
want to use from the stack. However, if you use the git stash apply
version of the command, it will use the code you want and leave it on
If a mistake was made in the last commits message, edit the text
while leaving the changed files as-is with:
git amend
BR ANCHING
the stack to be used again at another time.
Branching superficially appears much the same as it does in other
ABORTING
branches can be targeted to exist only locally, or be shared with (pushed
version control systems, but the difference lies in the fact that Git
to) the rest of the team. The concept of inexpensive local branches
If you want to abort your current uncommitted changes and
restore the working copy to the last committed state, there are two
increases the frequency in which developers use branching, opening
it up to use for quick private experiments that may be discarded if
commands that will help you accomplish this.
unsuccessful, or merged onto a well-known branch if successful.
git reset --hard
git branch <new branch name> <from branch>
git branch <new branch name>
Resetting with the hard option recursively discards all of your
currently uncommitted (unstaged or staged) changes.
CHOOSING A BRANCH
To target just one blob, use the checkout command to restore the file
Checking out (switching to) a branch is as simple as providing its name:
to its previous committed state.
git checkout <branch name>
git checkout -- Person.java
Local and remote git branches are checked out using the same
command, but in somewhat of a radical change of operation for users
ADDING (STAGING)
coming from other systems like Subversion, remote branches are
When the developer is ready to put files into the next commit, they
read-only until tracked and copied to a local branch. Local
must first be staged with the add command. Users can navigate to
branches are where new work is performed and code is committed.
any directory, adding files item by item, or by wildcard.
git
git
git
git
add
add
add
add
<file name, folder name, or wildcard>
submodule1/PrimaryClass.java
.
*.java
git branch <new branch name> <from branch>
git checkout <new branch name>
Or alternatively, in a combined command:
git checkout -b <new branch name> <from branch>
Specifying a folder name as the target of a git add
There is another way, as well. The following commands:
recursively stages files in any subdirectories.
git checkout <from branch>
git checkout -b <new branch name>
The -i option activates interactive add mode, in which Git prompts
for the files to be added or excluded from the next commit.
This method is often used if youre already working within a branch
and you want to quickly start a new branch based off of the current
git add -i
branch youre working with.
The -p option is a shortcut for activation of the patch sub-mode of
Starting with Git 1.6.6, a shorthand notation can be used to track a
the interactive prompt, allowing for precise pieces within a file to be
remote branch with a local branch of exactly the same name when
selected for staging.
no local branch of that name already exists and only one remote
location is configured.
git add -p
<remote and local branch name> git checkout performanceexperiment
COMMIT TING
Once all desired blobs are staged, a commit command transactionally
LISTING BRANCHES
saves the pending additions to the local repository. The default text
To list the complete set of current local and remote branches known to Git:
$EDITOR will be opened for entry of the commit message.
git branch -a
git commit
D Z O NE, INC .
DZ O NE .C O M
5
The local branches typically have simple names like master and
GETTING STARTED WITH GIT
git remote add <remote name> <remote address>
git remote add <remote name> git@github.com:matthewmccullough/
ts.git
experiment. Local branches are shown in white by Gits default
syntax highlighting. Remote branches are prefixed by remotes
and are shown in red.
PUSH
MERGING
Pushing with Git is the same thing as sending local changes
Like other popular VCSes, Git allows you to merge one or more
to a colleague or community repository with sufficiently open
branches into the current branch.
permissions as to allow you to write to it. If the colleague has the
pushed-to branch currently checked out, they will have to re-
git merge <branch one>
git merge <branch one> <branch two>
checkout the branch to allow the merge engine to potentially weave
your pushed changes into their pending changes.
If any conflicts are encountered, which happens less with Git that with
FETCH
many other VCSes, a notification message is displayed and the files are
To retrieve remote changes without merging them into your local
internally marked with >>>>>>>>> and <<<<<<<< surrounding the
branches, simply fetch the blobs. This invisibly stores all retrieved
conflicting portion of the file contents. Once manually resolved,
git-add the resolved file, then commit in the usual manner.
objects locally in your .git directory at the top of your project
REBASE
destination of the merge.
structure, but waits for further explicit instructions for a source and
Rebasing is the rewinding of existing commits on a branch with the
git fetch <remote name> git merge <remote name/remote branch>
intent of moving the branch start point forward, then replaying the
rewound commits. This allows developers to test their branch changes
PULL
safely in isolation on their private branch just as if they were made on
Pulling is the combination of a fetch and a merge as per the previous
top of the mainline code, including any recent mainline bug fixes.
section all in one seamless action.
git rebase <source branch name>
git rebase <source branch name> <destination branch name>
git pull
git pull <remote name>
git pull <remote name> <branch name>
TAGGING
In Git, tagging operates in a simple manner that approximates other
BUNDLE
VCSes, but unlike Subversion, tags are immutable from a commit
Bundle prepares binary diffs for transport on a USB stick or via
standpoint. To mark a point in your code timeline with a tag:
email. These binary diffs can be used to catch up a repository that
is behind otherwise too stringent of firewalls to successfully be
git tag <tag name>
git tag <tag name> <treeish>
reached directly over the network by push or pull.
git bundle create catchupsusan.bundle HEAD~8..HEAD
git bundle create catchupsusan.bundle --since=10.days master
T H E R E M O T E W O R K F LO W
These diffs can be treated just like any other remote, even though
Working with remote repositories is one of the primary features of
they are a local file on disk. The contents of the bundle can be
Git. You can push or pull, depending on your desired workflow with
inspected with Is-remote and the contents pulled into the local
colleagues and based on the repository operating system file and
repository with fetch. Many Git users add a file extension of .bundle
protocol permissions. Git repositories are most typically shared via
as a matter of convention.
SSH, though a lightweight daemon is also provided.
git ls-remote catchupsusan.bundle
git fetch catchupsusan.bundle
Git repository sharing via the simple daemon is
introduced at: www.kernel.org/pub/software/scm/git/
docs/git-daemon.html. Sharing over SSH and Gitosis
is documented in the Git Community Book at: book.gitscm.com/4_setting_up_a_private_repository.html.
GUIS
Many graphical user interfaces have gained Git support in the last
two years. The most popular Ruby, Perl, and Java/JVM IDEs have
between a good and great level of Git integration today.
REMOTES
GITK & GIT-GUI
While full paths to other repositories can be specified as a source or
destination with the majority of Git commands, this quickly becomes
Standard Git distributions provide two user interfaces written in Tcl/
unwieldy and a shorthand solution is called for. In Git-speak, these
Tk. The first, Git-Gui offers a panel by which to select files to add
bookmarks of other repository locations are called remotes.
and commit, as well as type a commit message. The latter offers a
diagram visualization of the projects code history and branching.
A remote called origin is automatically created if you cloned a remote
They both assume the current working directory as the repository
repository. The full address of that remote can be viewed with:
you wish to inspect.
git remote v
git gui gitk
To add a new remote name:
D Z O NE, INC .
DZ O NE .C O M
GETTING STARTED WITH GIT
TOWER, SOURCETREE & OTHERS
Please be patient, and note the progress messages. Clones of large
There are a number of GUIs out there for Git that arent officially
Subversion repositories can take hours to complete.
released along with Git. Some of them include significant advanced
functionality and are available on multiple platforms. Some of the
PUSHING GIT COMMITS TO SUBVERSION
more widely used ones include Tower, SourceTree, GitEye, and GitHub
Git commits can be pushed, transactionally, one for one to the cloned
Desktop. Unfortunately, GitHub Desktop currently only works with
Subversion repository. When the Git commits are at a good point for
GitHub & GitHub Enterprise repositories. However, given the wide
sharing with the Subversion colleagues, type:
use of GitHub as an online repository, its likely that youll run into
git svn dcommit
the GitHub Desktop client at one point.
IDES
RETRIE VING SUBVERSION CHANGES
Java IDEs including IntelliJ, Eclipse (eGit), and NetBeans (NBGit) all
When changes are inevitably made in Subversion and you want to
offer native or simple plugin support for Git through their traditional
freshen the Git repo with those changes, rebase to the latest state of
source code control integration points. However, there are a number
the Subversion repo.
of other applications that also offer direct Git integration, as well.
git svn rebase
This includes applications such as Sublime Text and Atom.
Numerous other platform-native GUIs offer graphically rich history
A DVA N C E D CO M M A N DS
browsing, branch visualization, merging, staging and commit features.
Git offers commands for both the new user and the expert alike.
C VS , S U BV E R S I O N
Some of the Git features requiring in-depth explanations can be
On the interoperability front, the most amazing thing about Git is its
discovered through the resources links below. These advanced
ability to read and write to a remote Subversion or CVS repository
features include the embedded (manpage-like) help and ASCII art
while aiming to provide the majority of the benefits of Git on the
visualization of branch merge statuses with show-branch. Git is also
local copy of a repository.
able to undo the last commit with the revert command, binary search
for (bisect) the commit over a range of history that caused the unit
CLONING
tests to begin failing, check the integrity of the repository with fsck,
To convert a Subversion repository that uses the traditional trunk,
prune any orphaned blobs from the tree with gc, and search through
tags, branches structure into a Git repository, use a syntax very
history with grep. And that is literally just the beginning.
similar to that used with a traditional Git repository.
This quick overview demonstrates what a rich and deep DVCS Git
truly is, while still being approachable for the newcomer to this bold
git svn clone --stdlayout <svn repo url>
new collaborative approach to source code and version control.
RESOURCES
ABOUT THE AUTHOR
OFFICIAL RELEASE NOTES AND
MAN PAGES
git-scm.com
KURT COLLINS (@timesync) is Director of Technology
Evangelism and Partnerships at Built.io. Kurt began his engineering
career at Silicon Graphics (SGI) where he worked on debugging
its UNIX-flavored operating system. Kurt co-founded The Hidden
Genius Project along with eight other individuals in response to the
urgent need to increase the number of black men in the technology
industry. Mentoring young people interested in the technology
industry is a personal passion of his. Today, he continues this work
with tech startups and non-profits alike.
kernel.org/pub/software/scm/git/docs
MANUALS, TUTORIALS
cworth.org/hgbook-git/tour
www-cs-students.stanford.edu/~blynn/
gitmagic
BOOKS
Pro Git by Scott Chacon (free HTML)
Version Control with Git by Jon Loeliger
Pragmatic Version Control Using Git by
Travis Swicegood
BOOKMARKS
delicious.com/matthew.mccullough/git
peepcode.com/products/git
BROWSE OUR COLLECTION OF FREE RESOURCES, INCLUDING:
RESEARCH GUIDES: Unbiased insight from leading tech experts
REFCARDZ: Library of 200+ reference cards covering the latest tech topics
COMMUNITIES: Share links, author articles, and engage with other tech experts
JOIN NOW
DZONE, INC.
150 PRESTON EXECUTIVE DR.
CARY, NC 27513
DZone communities deliver over 6 million pages each month to more than 3.3 million software
developers, architects and decision makers. DZone offers something for everyone, including news,
tutorials, cheat sheets, research guides, feature articles, source code and more.
888.678.0399
919.678.0300
REFCARDZ FEEDBACK WELCOME
refcardz@dzone.com
"DZone is a developer's dream," says PC Magazine.
SPONSORSHIP OPPORTUNITIES
DZ Osales@dzone.com
NE .C O M
Copyright 2016 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or
D Zpermission
O NE, INC
transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
of the. publisher.
VERSION 1.0