Getting Started With Git - Cheatsheet - Java Code Geeks
Getting Started With Git - Cheatsheet - Java Code Geeks
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
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.
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
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.
• Install Git: sudo apt install git • git config --global user.email
"youremail@example.com": Set your Git email
globally.
CentOS/Fedora
• 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:
• 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 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/
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
• 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.
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