Git Cheat Sheet

In 2005, Linus Torvalds (the man who created the Linux OS kernel) developed GIT for version control of the Linux kernel, but later another person, a Japanese software engineer, Junio Hamano, took over its support. Today, GIT is one of the most well-known open source version control systems that millions of projects around the world rely on (including both commercial and free projects). GIT is completely free software that supports many operating systems, such as Mac, Linux, Windows, and Solaris. Here are a few GIT features worth mentioning:

  1. Distributed version control system, GIT follows the peer — to-peer principle, unlike other systems like Subversion (SVN), which is based on the client-server model.
  2. GIT allows developers to have many completely independent code branches. Creating, deleting, and merging these branches is easy and time-consuming.
  3. In GIT, all operations are atomic; this means that any action can be completely successful or fail (without any changes). This is really important, because in some version control systems (like CVS), where actions are not atomic, some hung operations across the entire repository can leave it in an unstable state.
  4. Unlike other VCS such as SVN or CVS where metadata is stored in hidden folders (.cvs,. svn, etc.), in GIT all data is located in.git directories.
  5. It uses a data model that helps ensure the cryptographic integrity of everything that is present in the repository. Each time files are added or committed, their checksums are generated; a similar process occurs when they are extracted.
  6. Another excellent feature present in GIT is its index. Within the index, developers can format commits and view them before they are actually applied.

Installation

Installing GIT on Windows

  1. Installing GIT on Windows is as easy as installing any other application; downloading the installer and running it. Follow these steps to install GIT on Windows:
  2. Visit this site and download the GIT installer for Windows. After downloading, start the installation by double-clicking. Follow the on-screen instructions and continue to click Next and finally Finish to complete the installation successfully.
  3. Run a command prompt and enter the following commands in the terminal:

git config –global user.name ” Kate Smith”

git config –global user.email example@email.com

Installing GIT on Mac

There are many ways to install GIT on a Mac, and there is even a chance that GIT is already installed on your computer. If you have XCode installed; run the following command in the terminal to check:

git –version

If your result will be very similar to that git version 2.7.0 (Apple Git-66), then you can safely start working with GIT, if not, perform the following steps:

  1. Visit this site and download the latest version of the installer for Mac.
  2. Follow the installer’s instructions and complete the installation.
  3. Use the git-version command again to confirm that the installation was successful.
  4. Run the following commands in the terminal to configure your username and email address that will be associated with your GIT account:

git config –global user.name ” Name Surname”

git config –global user.email example@email.com

GIT Basics

SETUP

git config –global user.name “[firstname lastname]”

set a name that is identifiable for credit when review version history

git config –global user.email “[valid-email]”

set an email address that will be associated with each history marker

git config –global color.ui auto

set automatic command line coloring for Git for easy reviewing

SETUP & INIT

git init

initialize an existing directory as a Git repository

git clone [url]

retrieve an entire repository from a hosted location via URL

STAGE AND SNAPSHOT

git status

show modified files in working directory, staged for your next commit

 git add [file]

add a file as it looks now to your next commit (stage)

git reset [file]

unstage a file while retaining the changes in working directory

git diff

diff of what is changed but not staged

git diff –staged

diff of what is staged but not yet commited

git commit -m “[descriptive message]”

commit your staged content as a new commit snapshot

GIT BRANCHES

git branch

List all of the branches in your repo. Add a argument to create a new branch with the name.

git checkout –b

Create and check out a new branch named. Drop the -b flag to checkout an existing branch.

git merge

Merge into the current branch.

REWRITING GIT HISTORY

git commit –amend

Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message

git rebase <base>

Rebase the current branch onto <base>. <base> can be a commit ID, branch name, a tag, or a relative reference to HEAD.

git reflog

Show a log of changes to the local repository’s HEAD.  Add –relative-date flag to show date info or –all to show all refs

REMOTE REPOSITORIES

git remote add <name> <url>

Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands.

git fetch <remote> <branch>

Fetches a specific <branch>, from the repo. Leave off <brach> to fetch all remote refs.

git pull <remote>

Fetch the specified remote’s copy of current branch and immediately merge it into the local copy.

git push <remote> <branch>

Push the branch to <remote>, along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.

GIT CONFIG

git config –global user.name <name>

Define the author name to be used for all commits by the current user.

git config –global user.email <email>

Define the author email to be used for all commits by the current user.

git config –global alias. <alias-name> <git-command>

Create shortcut for a Git command. E.g. alias.glog “log –graph –oneline” will set “git glog” equivalent to”git log –graph –oneline.

git config –system core.editor <editor>

Set text editor used by commands for all users on the machine. <editor>arg should be the command that launches the desired editor (e.g., vi).

git config –global –edit

Open the global configuration file in a text editor for manual editing.

TEMPORARY COMMITS

git stash

Save modified and staged changes

git stash list

list stack-order of stashed file changes

git stash pop

write working from top of stash stack

git stash drop

discard the changes from top of stash stack

GIT PULL

git pull –rebase <remote>

Fetch the remote’s copy of current branch and rebases it into the local copy. Uses GIT rebase instead of merge to integrate the branches.