0% found this document useful (0 votes)
43 views

Getting Started With Git - Cheatsheet - Java Code Geeks

The document provides an introduction to getting started with Git. It discusses key advantages like the distributed version control system and ease of collaboration. The document also outlines downloading and installing Git on different operating systems. Basic Git commands and workflows are covered, along with concepts like branching, remote repositories, and resolving conflicts.

Uploaded by

Technical Novice
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Getting Started With Git - Cheatsheet - Java Code Geeks

The document provides an introduction to getting started with Git. It discusses key advantages like the distributed version control system and ease of collaboration. The document also outlines downloading and installing Git on different operating systems. Basic Git commands and workflows are covered, along with concepts like branching, remote repositories, and resolving conflicts.

Uploaded by

Technical Novice
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

GETTING STARTED WITH GIT

Getting Started with Git


TABLE OF CONTENTS

Preface 1
Introduction 1
Key Differentiators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Advantages of Git. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Disadvantages of Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Download and Install 1
Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
macOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Defining User Credentials 2
Getting Started 2
Git Treeish 3
Basic Workflow 4
Branching 4
Remote Repositories 4
Resolving Conflicts 5
Stashing Changes 5
Reverting Changes 5
Tags 5
gitignore 5
Helpful Commands 6
GUIs and IDEs 6

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!


1 GETTING STARTED WITH GIT

PREFACE
PREFACE integrity. Once data is committed, it is virtually
impossible to change it without leaving a trace.
This cheatsheet serves as your trusty companion on
• Collaboration: Git fosters seamless
your Git journey. Whether you’re a seasoned
collaboration among developers, allowing them
developer looking for a quick reference or a
to work independently and then merge their
newcomer navigating the Git landscape, you’ll find
changes effortlessly.
valuable insights, commands, and tips within these
pages. • Wide Adoption: Git is widely adopted across
the software development industry and is
INTRODUCTION
INTRODUCTION
supported by numerous hosting services like
GitHub and GitLab.
Git is a distributed version control system (DVCS)
that has gained immense popularity for its speed, DISADVANTAGES OF GIT
flexibility, and efficiency in managing source code
and collaborating on software projects. Unlike • Learning Curve: Git has a steeper learning
traditional version control systems like Subversion curve compared to simpler version control
(SVN) and CVS, Git employs a fundamentally systems, especially for beginners. The
different approach to tracking changes in code. multitude of commands and options can be
overwhelming.

KEY DIFFERENTIATORS • Complex History: The commit history in Git


can become complex, particularly in large
• Distributed Version Control: Git is a projects, making it challenging to track the
distributed version control system, which history effectively.
means that every developer working with Git
• Git Terminology: Some Git terminology, such
has a complete copy of the entire repository,
as "rebasing" and "detached HEAD," can be
not just the latest code snapshot. This
confusing for newcomers.
decentralized nature makes it more robust and
allows developers to work offline, fostering
This cheat sheet will guide you through the
collaboration without depending on a central
essential Git commands and concepts, equipping
server.
you with the knowledge to leverage Git’s power in
• Branching and Merging: Git excels
at your projects.
branching and merging. It enables developers
to create lightweight branches effortlessly, DOWNLOADAND
DOWNLOAD ANDINSTALL
INSTALL
making it easy to experiment and work on new
features without affecting the main codebase. Below are instructions for downloading and
Merging branches is also straightforward and installing Git on Windows, Linux, and macOS.
minimizes conflicts efficiently.

WINDOWS
ADVANTAGES OF GIT
• Download Git Windows installer from the
• Speed: Git is exceptionally fast when it comes official website for Windows at
to committing changes, branching, merging, https://gitforwindows.org/.
and searching through the commit history. This
• Run the downloaded installer. Follow the
speed ensures a smooth and efficient
installation wizard’s prompts:
development process.
◦ Choose your preferred installation
• Branching: Git’s branching model promotes
directory.
parallel development, making it easier to
manage multiple features or bug fixes ◦ Select components to install (the default
simultaneously. selections are usually fine).

• Data Integrity: Git uses a robust content- ◦ Choose an editor (the default is usually
addressable storage system to ensure data "Use the Nano editor by default," but you

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!


2 GETTING STARTED WITH GIT

can select a different text editor if you installing and setting up Git on your machine:
prefer).
• Getting Started and Installing Git
◦ Choose the terminal emulator used by Git
(the default is "Use Windows' default • Setting Up Git
console window," but you can choose "Use • Installing Git on Mac, Windows, and Linux
MinTTY" for a more Unix-like experience).

◦ Configure line ending conversions. The Working with remote repositories is one of the
default setting, "Checkout as-is, commit primary features of Git. Git repositories are most
Unix-style line endings," is typically typically shared via SSH. So to enable repository
suitable. sharing over SSH and Gitosis follow the instructions
documented at Git Community Book.
◦ Choose how Git should handle line endings
in your working directory (usually, "Use DEFININGUSER
USERCREDENTIALS
CREDENTIALS
Windows-style line endings" is the DEFINING
recommended option for Windows).
Git does not directly support repository
• Click "Install" to start the installation process. authentication or authorization. It delegates this to
the communication protocol (commonly SSH) or the
• After installation, you can open Git Bash or use
hosting operating system via file system
Git via the Windows Command Prompt or
permissions. Thus, the user information provided
PowerShell.
during your first Git setup (per machine) is used for
properly crediting your code contributions.
LINUX
Issue the following commands to setup your Git
Ubuntu/Debian user.

• Open a terminal. • git config --global user.name "Your Name": Set


• Update your package list: sudo apt update your Git username globally.

• Install Git: sudo apt install git • git config --global user.email
"youremail@example.com": Set your Git email
globally.
CentOS/Fedora

• Open a terminal. GETTINGSTARTED


GETTING STARTED
• Install Git using the package manager (yum on
Using a command prompt, navigate to either a
CentOS or dnf on Fedora): sudo yum install git
blank folder or the top folder of an existing project
or sudo dnf install git
that you want to put under version control.
Initialize the directory as a Git repository by typing:
MACOS git init. Git builds in place just a single directory,
namely .git, and uniquely stores all the metadata
• On macOS, you can use the Terminal, or you
and repository history in it.
can install Git using the Homebrew package
manager if you have it installed. If you don’t
If you want to build on someone else’s work, you
have Homebrew, you can install it from can clone a remote repository by using: git clone
https://brew.sh/. <repository URI>. Below are two examples of
• Open the Terminal. cloning a remote repository using SSH and HTTP
protocols.
• Install Git using Homebrew: brew install git

• Verify the installation by running: git


--version # ssh protocol (requires SSH
credentials to be established):
If you want to know all the potential nuances of a $ git clone git@github.com:kioub78
Git setup, here are several in-depth tutorials on /syntaxhighlighter-amplified.git

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!


3 GETTING STARTED WITH GIT

# https protocol: GIT


GITTREEISH
TREEISH
$ git clone https://github.
In Git, a "treeish" refers to a reference that
com/kioub78/syntaxhighlighter-
identifies a tree object or a commit object within
amplified.git
the Git repository. Tree objects represent directory
structures and file states, while commit objects
When you run git clone, several actions are represent a snapshot of the entire repository at a
performed: specific point in time. Treeish references can be
used in various Git commands to navigate and
• Connection to the Remote Repository: Git operate on the Git repository. Understanding how to
establishes a connection to the remote use them effectively is crucial for managing and
repository specified in the URI provided as an exploring your project’s history.
argument to git clone. This is typically hosted
on a Git hosting service like GitHub, GitLab, Treeish References:
Bitbucket, or a custom Git server.
• Commit Hash: A unique 40-character SHA-1
• Downloading the Repository: Git downloads
hexadecimal string that identifies a specific
the entire content of the remote repository to
commit in the Git history. This is the most
your local machine. This includes all branches,
precise way to specify a commit.
commits, files, directories, and commit history.
It essentially creates a complete snapshot of the • Branch Name: The name of a branch in your
remote repository, preserving the entire Git repository. A branch name is a symbolic
history. reference that points to the latest commit in
that branch.
• Creating a New Directory: Git creates a new
directory/folder on your local machine with the • Tag Name: A tag is a reference to a specific
same name as the remote repository. If you commit. You can use tag names to refer to
specify a directory name after the URI, Git will specific points in your Git history, such as
use that name instead. releases or milestones.

• Initializing a Local Git Repository: Within the • Ancestry References: These are references
newly created directory, Git initializes a local that specify a range of commits. For example,
Git repository. This local repository is a A..B refers to all commits reachable from B but
complete repository in itself, with its own .git not from A.
folder that contains all the necessary
configuration and metadata for version control. Commonly Used Treeish References:

• Setting Up Remote Tracking: Git sets up a


Treeish Reference Explanation
remote tracking branch called origin/master
HEAD Refers to the latest
(assuming "master" is the default branch) that
tracks the remote repository’s default branch. commit on the current
This allows you to fetch and pull updates from branch.
the remote repository easily. HEAD^ or HEAD~1 Refers to the parent of
• Checking Out the Default Branch: Git the latest commit.
switches to the default branch (usually HEAD~2 Refers to the
"master" or "main") as the current working grandparent of the latest
copy. commit.
origin/master Refers to the latest
After these actions, the local copy of the remote Git
repository is initialized, and ready for you to start commit on the "master"
using Git commands to interact with it. You can branch in the remote
create new branches, make changes, commit them, repository called
and push your changes back to the remote "origin."
repository, effectively participating in the
collaborative development of the project. Examples:

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!


4 GETTING STARTED WITH GIT

• git log <commit-hash>: View the commit history local branch, you can use the --track option e.g.
starting from the specified commit. git branch --track <branch-name>
origin/<base-branch>. Remote branches are
• git diff <branch-name>: Compare the current
read-only until "tracked" and copied to a local
branch with the specified branch.
branch.
• git checkout <tag-name>: Switch to a specific
• git checkout <branch-name>: Switch to an
commit or tag.
existing branch.
• git diff HEAD2..HEAD1: Compare changes between
• git merge <branch-name>: Merge changes from
the grandparent and parent of the latest
one branch into the current working branch.
commit.
Multiple branches can be defined separated by
space. In case of conflicts, the files involved are
BASICWORKFLOW
BASIC WORKFLOW
internally marked with >>>>>>>>> and
<<<<<<<<< around the conflicting portions of
• git mv <file1> <file2>: Move and/or rename a their contents. Once manually updated, you can
file and track its new destination. use git add to stage the resolved files and then
• git rm <file>: Remove a file from the current git commit to commit the changes in the
state of the branch. current working branch as usual.

• git status: Check the status of your working


REMOTEREPOSITORIES
REMOTE REPOSITORIES
directory (modified, new, deleted, or untracked
files and folders).
• git remote: List remote repositories. Use the -v
• git add <filename, directory name, or flag to get full repository addresses rather than
wildcard>: Stage (start tracking) changes for names only.
commit. You can use a directory name or
• git remote add <name> <URI>: Add a remote
wildcard for multiple file/folder selections. Use
repository residing at the specified URI, under
the -i option for interactive mode (prompts for
the specified name. In Git terms, this name/URI
each file to be added). Remember, only staged
assignment is called a remote. Remote
changes can be committed.
references can be used in Git commands to
• git commit -m "Your commit message": identify specific remote repositories. Git
Transactionally saves staged changes to the automatically creates a remote called origin
local repository. A commit message is required. when you clone a remote repository.
Leaving the message blank will abort the
• git fetch <remote> <branch>: Retrieve changes
commit and leave the staged blobs in place. In
from a remote repository and branch without
case you desire to make changes to the last
merging them into your current local working
commit’s message you can use git commit
branch. Just stores the blobs in .git directory
--amend.
waiting for further instructions. "<remote>"
• git show: View statistics and facts about the last denotes the assigned name of the remote
commit. repository as defined with a git remote add
• git log: View the commit history. You may use command.
the --since flag to apply time restrictions e.g • git pull <remote> <branch>: Retrieve changes
--since=yesterday or --since=3weeks. from a remote repository and branch and then
merges them into the current local working
BRANCHING
BRANCHING branch. "<remote>" denotes the assigned name
of the remote repository as defined with a git
• git branch -a: List all branches (local and remote add command. Pulling is the
remote) in the repository. combination of a git fetch and a git merge all
in one seamless action.
• git branch <branch-name>: Create a new branch.
• git push <remote> <branch>: Send committed
• git branch <branch-name> <from>: Create a new
changes from your current local working
branch on a specific branch/tag/commit. To
branch to a remote repository and branch.
take a remote branch as the basis for your new

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!


5 GETTING STARTED WITH GIT

"<remote>" denotes the assigned name of the commit.


remote repository as defined with a git remote
add command. You need to have sufficient open TAGS
TAGS
permissions as to allow you to write to the
remote branch. Use --tags flag to push tags • git tag: List all tags.
also.
• git tag <tag-name> <commit>: Create a new tag
referencing the specified commit hash. Omit
RESOLVINGCONFLICTS
RESOLVING CONFLICTS
the commit attribute to reference the last
commit.
• git diff: Show the differences between the
working directory and the last commit - • git tag -a <tag-name> -m "Tag message": Create
Includes everything unstaged (not git add’ed). an annotated tag with a message. Annotated
Use the -cached flag to get everything staged (git tags are stored as full objects in the Git
add’ed) instead. database. They’re checksummed; contain the
tagger name, email, and date and have a
• git diff <commit1> <commit2>: Show differences
tagging message required.
between two commits - Includes everything
unstaged (not git add’ed) and staged (git
GITIGNORE
GITIGNORE
add’ed).

• git merge --abort: Abort a merge in progress. Create a .gitignore file to specify which files and
• Manually edit conflicted files and use git add to directories should be ignored by Git. An example is
mark conflicts as resolved. provided below. More information is available
here.
• git blame <file>: Annotate each line of a
source file with the name and date it was last
modified. # Ignore specific files
filename.txt
STASHINGCHANGES
STASHING CHANGES secret.key

• git stash: Temporarily save/push changes that # Ignore all files in certain
are not ready for commit onto a stack. This is directories
particularly useful when your changes are in
/logs/
an incomplete state so you are not ready to
/tmp/
commit them, but you need to temporarily
return to the last committed state e.g. a fresh
/node_modules/
checkout. /build/

• git stash pop: Apply the most recent stashed


# Ignore all files with specific
changes back into the working copies of the
extensions
files and remove them from the stash list.
*.log
• git stash list: List all stashes. *.tmp

REVERTINGCHANGES
REVERTING CHANGES # Ignore hidden files
.*
• git reset <file>: Unstage changes in a file,
restoring the working copy to the last
# Ignore a specific directory but
committed state of the file.
not its subdirectories
• git reset --hard <commit>: Reset to a specific /logs/
commit, discarding all changes after that
commit.
# Ignore files with a specific
• git revert <commit>: Create a new commit that prefix
undoes the changes made in a previous debug-*.log

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!


6 GETTING STARTED WITH GIT

Git Gui Description


# Negate patterns (exclude specific
Sourcetree A free Git GUI for
files) Windows and macOS
# Ignore all .log files except that provides a visual
important.log way to manage Git
*.log repositories, offering
!important.log features like Git-flow
support and repository
# Ignore macOS and Windows system visualization.
files GitKraken A cross-platform Git
.DS_Store client with a sleek and
Thumbs.db visually appealing
interface. It includes
collaboration features,
HELPFULCOMMANDS
HELPFUL COMMANDS Git-flow support, and
integrations with
• git help <command>: Get help for a specific Git various services.
command.
TortoiseGit A Windows-specific Git
• git --version: Check your Git version. GUI that integrates with
Windows File Explorer.
• git clean -n: Dry run of removing untracked
It provides context
files.
menu integration, icon
• git clean -f: Remove untracked files. overlays, and many Git
• git gc: prune any orphaned blobs from the features within
tree. Explorer.

• git fsck: check the integrity of the repository. SmartGit A cross-platform Git
client with a user-
• git grep: search through history. friendly interface and a
focus on simplicity. It
GUISAND
GUIS ANDIDES
IDES supports Git and
Mercurial, and it offers
Bundled with the standard Git distribution are two various features for Git
user interfaces Gitk and Git-Gui. Nevertheless there workflow.
is a plethora of 3rd party Git GUIs available.
GitExtensions A Windows Git client
Furthermore the most popular IDEs and code
that integrates with
editors including IntelliJ, Eclipse, NetBeans,
Windows File Explorer.
Sublime Text, Atom, VS Code, Vim, and Emacs all
It includes Git Bash, Git
offer native or simple plugin support for Git. Below
command-line, and
is a list containing some of the most popular Git
Visual Studio
GUIs.
integration, among
other features.
Git Gui Description
Magit A Git interface for
GitHub Desktop GitHub’s official desktop
Emacs, making it
application for
convenient for Emacs
managing Git
users to interact with Git
repositories. It offers an
repositories without
intuitive interface for
leaving their preferred
performing common Git
text editor.
tasks and easy
integration with GitHub.

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!


7 GETTING STARTED WITH GIT

Git Gui Description

Git Cola A cross-platform Git GUI


for those who prefer a
lightweight and
straightforward
interface. It provides
basic Git features
without being overly
complex.

Tower A Git client for macOS


and Windows that
focuses on providing a
powerful and user-
friendly interface. It
offers features like Git-
flow, submodule
support, and
integrations with
popular Git hosting
services.

JCG delivers over 1 million pages each month to more than 700K software
developers, architects and decision makers. JCG offers something for everyone,
including news, tutorials, cheat sheets, research guides, feature articles, source code
and more.
CHEATSHEET FEEDBACK
WELCOME
support@javacodegeeks.com

Copyright © 2014 Exelixis Media P.C. All rights reserved. No part of this publication may be SPONSORSHIP
reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, OPPORTUNITIES
mechanical, photocopying, or otherwise, without prior written permission of the publisher. sales@javacodegeeks.com

JAVACODEGEEKS.COM | © EXELIXIS MEDIA P.C. VISIT JAVACODEGEEKS.COM FOR MORE!

You might also like