Dzone RC Getting Started With Git PDF
Dzone RC Getting Started With Git PDF
094
Why Get Git?
Getting Started
Cloning Existing Projects
The Typical Local Workflow
preferred credentials.
%USERPROFILE% on Windows).
GETTING
STARTED WITH
GIT 7
JAVA ENTERPRISE
EDITION
help.github.com/articles/set-up-git/#platform-windows
help.github.com/articles/set-up-git/#platform-mac
help.github.com/articles/set-up-git/#platform-linux
Now that Git is installed and the user information established, you
that you want to put under version control. Then initialize the
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.
supported platform.
D Z O NE, INC.
DZ O NE.C O M
TREEISH
DEFINITION
HEAD
HEAD^
HEAD^^
HEAD~1
HEAD~3
:/Reformatting all
RELEASE-1.0
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
difference in a DVCS is that the entire history, not just the latest
version, is retrieved and saved to the local users disk.
T H E T Y P I C A L LO C A L W O R K F LO W
EDITING
write-enabled protocol.
that its merge capabilities are a match for any conflicted changes
If you need to move a file, Git can often detect your manual relocation
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.
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
folders, and the files comprising the changeset. This allows commits
to be made independent of any central coordinating server.
git rm fileyouwishtodelete.txt
VIE WING
Daily work calls for strong support of viewing current and historical
64de179becc3ed324daab72f7238df1404723672
STATUS
Additionally, any unique sub-portion of the hash can be used. Git will
let you know when the characters supplied are not enough to be
D Z O NE, INC .
DZ O NE .C O M
git status
BLAME
DIFF
If trying to discover why and when a certain line was added, cut to
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
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
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
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,
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
D Z O NE, INC .
DZ O NE .C O M
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 show
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
ABORTING
version control systems, but the difference lies in the fact that Git
to) the rest of the team. The concept of inexpensive local branches
CHOOSING A BRANCH
To target just one blob, use the checkout command to restore the file
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)
When the developer is ready to put files into the next commit, they
must first be staged with the add command. Users can navigate to
add
add
add
add
git add -i
remote branch with a local branch of exactly the same name when
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:
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
PUSH
MERGING
Like other popular VCSes, Git allows you to merge one or more
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
branches, simply fetch the blobs. This invisibly stores all retrieved
REBASE
structure, but waits for further explicit instructions for a source and
intent of moving the branch start point forward, then replaying the
rewound commits. This allows developers to test their branch changes
PULL
top of the mainline code, including any recent mainline bug fixes.
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
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
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
repository with fetch. Many Git users add a file extension of .bundle
as a matter of convention.
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
Tk. The first, Git-Gui offers a panel by which to select files to add
git remote v
git gui gitk
D Z O NE, INC .
DZ O NE .C O M
There are a number of GUIs out there for Git that arent officially
more widely used ones include Tower, SourceTree, GitEye, and GitHub
Git commits can be pushed, transactionally, one for one to the cloned
Subversion repository. When the Git commits are at a good point for
use of GitHub as an online repository, its likely that youll run into
git svn dcommit
IDES
Java IDEs including IntelliJ, Eclipse (eGit), and NetBeans (NBGit) all
offer native or simple plugin support for Git through their traditional
freshen the Git repo with those changes, rebase to the latest state of
A DVA N C E D CO M M A N DS
Git offers commands for both the new user and the expert alike.
C VS , S U BV E R S I O N
On the interoperability front, the most amazing thing about Git is its
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,
prune any orphaned blobs from the tree with gc, and search through
This quick overview demonstrates what a rich and deep DVCS Git
truly is, while still being approachable for the newcomer to this bold
RESOURCES
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
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
[email protected]
SPONSORSHIP OPPORTUNITIES
DZ [email protected]
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