Git Refrence PDF
Git Refrence PDF
94
CONTENTS
WHY GET GIT? the executable search $PATH. Git is primarily written in C, which
means there is a unique distribution for each supported platform.
Git is a postmodern version control system that offers the
familiar capabilities of CVS or Subversion, but doesn’t stop at The canonical reference for Git installers can be found on a
just matching existing tools. Git stretches the very notion of subpage of the official Git site at: git-scm.com/download. There
version control systems (VCS) by its ability to offer almost all of are a number of installers available there for those that don’t want
its features for use offline and without a central server. It is the to go through the hassle of doing the install manually. In addition,
brainchild of Linus Torvalds, with the first prototype written in a also available at the official Git download site are links to older
vitriolic two-week response to the “BitKeeper debacle” of 2005. distributions of the Git binary.
Today, developers everywhere are migrating in droves to this ESTABLISHING USER CREDENTIALS
exciting platform. Users reference its blistering performance, Once you have selected a suitable distribution of Git for your
usage flexibility, offline capabilities, and collaboration features as platform, you’ll need to identify yourself with a username and
their motivation for switching. Let’s get started with Git. You’ll be email address to Git.
using it like a master in no time at all.
In a separation of concerns most satisfying to the purist, Git does
DISTRIBUTED VERSION CONTROL not directly support repository authentication or authorization. It
If you are familiar with one or more traditional or centralized delegates this in a very functional way to the protocol (commonly
version control systems like Subversion, there will be several SSH) or operating system (file system permissions) hosting or
mental adjustments to make in your migration to Git. The first serving up the repository. Thus, the user information provided
is that there is no central server. The second is that there is no during your first Git setup on a given machine is purely for “credit”
central server. The full history of the repository lives on every of your code contributions.
user’s machine that has cloned (checked out) a copy of the
repository. This is the essence of a Distributed Version Control With the binaries on your $PATH, issue the following three
System (DVCS). commands just once per new machine on which you’ll be
using Git. Replace the username and email address with your
Once over those hurdles, it is quite liberating to be able to work preferred credentials.
entirely independently, versioning any new project that you start,
even if in the incubation phase. The ease of setting up a new Git git config --global user.name “matthew.mccullough”
git config --global user.email “matthewm@ambientideas.com”
repository (or ‘repo’ in common parlance) leads to setting up git config --global color.ui “auto”
repos everywhere. It feels frictionless.
INSTALLING GIT
Git has a very light footprint for its installation. For most
platforms, you can simple copy the binaries to a folder that is on
Bitbucket.org/forthecode
3 GETTING STARTED WITH GIT
These commands store your preferences in a file named The protocol difference often signifies whether you have read-
.gitconfig inside your home directory (~ on UNIX and Mac, and only or writeable access to the origin repository. The final syntax,
%USERPROFILE% on Windows). which accesses an SSH exposed repository, is the most common
write-enabled protocol.
If you are intrigued by all the potential nuances of a Git setup,
Bitbucket, a web-based code hosting site, offers several in-depth The clone command performs several subtasks under the hood.
tutorials on setting up Git for Linux, Windows, and Mac. Here are It sets up a remote (a Git repository address bookmark) named
several in-depth Git installation guides: origin that points to the location. Next, clone asks this location for
the contents of its entire repository. Git copies those objects in a
• atlassian.com/git/tutorials/install-git#windows
zlib-compressed manner over the network to the requestor’s local
• atlassian.com/git/tutorials/install-git#mac-os-x disk. Lastly, clone switches to a branch named master, which is
equivalent to Subversion’s trunk, as the current working copy. The
• atlassian.com/git/tutorials/install-git#linux local copy of this repo is now ready to have edits made, branches
created, and commits issued – all while online or offline.
CREATING A REPOSITORY
Now that Git is installed and the user information established,
TREEISH AND HASHES
you can begin establishing new repositories. From a command
prompt, change directories to either a blank folder or an Rather than a sequential revision ID, Git marks each commit
existing project that you want to put under version control. with a SHA-1 hash that is unique to the person committing the
Then initialize the directory as a Git repository by typing the changes, the folders, and the files comprising the changeset.
following commands: This allows commits to be made independent of any central
coordinating server.
git init
git add . A full SHA-1 hash is 40 hex characters
git commit –m ’The first commit‘
64de179becc3ed324daab72f7238df1404723672
The first command in the sequence, init, builds a .git directory
that contains all the metadata and repository history. Unlike many To efficiently navigate the history of hashes, several symbolic
other version control systems, Git uniquely stores everything in shorthand notations can be used as listed in the table below.
just a single directory at the top of the project. No pollution in Additionally, any unique sub-portion of the hash can be used. Git
every directory. will let you know when the characters supplied are not enough to
be unique. In most cases, 4-5 characters are sufficient.
Next, the add command with the dot wildcard tells Git to start
tracking changes for the current directory, its files, and for all
TREEISH DEFINITION
folders beneath, if any exist.
HEAD HEAD The current committed version
Lastly, the commit function takes all previous additions
and makes them permanent in the repository’s history in a HEAD^ One commit ago
transactional action. Rather than letting Git prompt the user via
the default text editor, the -m option preemptively supplies the HEAD^^ Two commits ago
commit message to be saved alongside the committed files.
HEAD-1 One commit ago
It is amazing and exciting to be able to truthfully say that you can
HEAD-3 Three commits ago
use the basics of Git for locally versioning files with just these
three commands.
:/”Reformatting all” Hybrid method of types 1, 2, and 3.
If you need to move a file, Git can often detect your manual git log
git log --since=yesterday
relocation of the file and will show it as a pending “move.”
git log --since=2weeks
However, it is often more prudent to just directly tell Git to
relocate a file and track its new destination.
BLAME
If trying to discover why and when a certain line was added, cut
git mv originalfile.txt newsubdir/newfilename.txt
to the chase and have Git annotate each line of a source file with
the name and date it came into existence:
If you wish to expunge a file from the current state of the branch,
simply tell Git to remove it. It will be put in a pending deletion git blame <filename>
state and can be confirmed and completed by the next commit.
STATUS
When you are ready to write the stashed changes back into the
To check the current status of a project’s local directories and
working copies of the files, simply pop them back of the stack.
files (modified, new, deleted, or untracked) invoke the status
command:
git stash pop
git status
ABORTING
If you want to abort your current uncommitted changes and
DIFF restore the working copy to the last committed state, there are
A patch-style view of the difference between the currently edited two commands that will help you accomplish this.
and committed files, or any two points in the past can easily be
summoned. The .. operator signifies a range is being provided. An git reset --hard
git add <file name, folder name, or wildcard> Local and remote git branches are checked out using the same
git add submodule1/PrimaryClass.java
git add .
command, but in somewhat of a radical change of operation
git add *.java for users coming from other systems like Subversion, remote
branches are read-only until “tracked” and copied to a local
Specifying a folder name as the target of a git add recursively branch. Local branches are where new work is performed and
stages files in any subdirectories. code is committed.
The -i option activates interactive add mode, in which Git prompts git branch<new branch name> <from branch>
git checkout <new branch name>
for the files to be added or excluded from the next commit.
git branch -a
git commit
The local branches typically have simple names like master and
To supply the commit message directly at the command prompt:
experiment. Local branches are shown in white by Git’s default
git commit –m”<your commit message>” syntax highlighting. Remote branches are prefixed by “remotes”
and are shown in red.
To view the statistics and facts about the last commit:
MERGING
git show Like other popular VCSes, Git allows you to merge one or more
branches into the current branch.
If a mistake was made in the last commit’s message, edit the text
git merge <branch one>
while leaving the changed files as-is with: git merge <branch one> <branch two>
git amend If any conflicts are encountered, which is rare with Git, a
notification message is displayed and the files are internally
BRANCHING marked with >>>>>>>>> and <<<<<<<< around the conflicting
Branching superficially appears much the same as it does in other portion of the file contents. Once manually resolved, git-add the
resolved file, then commit in the usual manner.
version control systems, but the difference lies in the fact that
Git branches can be targeted to exist only locally, or be shared REBASE
with (pushed to) the rest of the team. The concept of inexpensive Rebasing is the rewinding of existing commits on a branch with
local branches increases the frequency in which developers use the intent of moving the “branch start point” forward, then
branching, opening it up to use for quick private experiments that replaying the rewound commits. This allows developers to test
may be discarded if unsuccessful, or merged onto a well-known their branch changes safely in isolation on their private branch
just as if they were made on top of the mainline code, including
branch if successful.
any recent mainline bug fixes.
git branch <new branch name> <from branch>
git rebase <source branch name>
git branch <new branch name>
git rebase <source branch name> <destination branch name>
TAGGING
git pull
In Git, tagging operates in a simple manner that approximates other git pull <remote name>
VCSes, but unlike Subversion, tags are immutable from a commit git pull <remote name> <branch name>
Numerous other platform-native GUIs offer graphically rich to freshen the Git repo with those changes, rebase to the latest
history browsing, branch visualization, merging, staging, and state of the Subversion repo.
commit features.
git svn rebase
CVS, SUBVERSION
On the interoperability front, the most amazing thing about Git
ADVANCED COMMANDS
is its ability to read and write to a remote Subversion or CVS
repository while aiming to provide the majority of the benefits of Git offers commands for both the new user and the expert alike.
Git on the local copy of a repository. Some of the Git features requiring in-depth explanations can be
discovered through the resources links below. These advanced
CLONING features include the embedded (manpage-like) help and ASCII
To convert a Subversion repository that uses the traditional trunk, art visualization of branch merge statuses with show-branch. Git
tags, branches structure into a Git repository, use a syntax very is also able to undo the last commit with the revert command,
similar to that used with a traditional Git repository. binary search for (bisect) the commit over a range of history
that caused the unit tests to begin failing, check the integrity of
git svn clone --stdlayout <svn repo url> the repository with fsck, prune any orphaned blobs from the tree
with gc, and search through history with grep. And that is literally
Please be patient and note the progress messages. Clones of just the beginning.
large Subversion repositories can take hours to complete.
This quick overview demonstrates what a rich and deep DVCS
Git truly is, while still being approachable for the newcomer
PUSHING GIT COMMITS TO SUBVERSION to this bold new collaborative approach to source code and
Git commits can be pushed, transactionally, one for one to the version control.
cloned Subversion repository. When the Git commits are are a
good point for sharing with the Subversion colleagues, type:
A B O U T T H E AU T H O R
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.
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.
DZONE, INC. REFCARDZ FEEDBACK
"DZone is a developer's dream," says PC Magazine. 150 PRESTON EXECUTIVE DR. WELCOME
refcardz@dzone.com
CARY, NC 27513
Copyright © 2017 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval SPONSORSHIP
system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior 888.678.0399 OPPORTUNITIES
written permission of the publisher. 919.678.0300 sales@dzone.com