What is GIT

By far, the most widely used modern version control system in the world today is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. Developers who have worked with Git are well represented in the pool of available software development talent and it works well on a wide range of operating systems and IDEs (Integrated Development Environments).

Having a distributed architecture, Git is an example of a DVCS (hence Distributed Version Control System). Rather than have only one single place for the full version history of the software as is common in once-popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer's working copy of the code is also a repository that can contain the full history of all changes.

In addition to being distributed, Git has been designed with performance, security and flexibility in mind.

Install GIT

Install GIT on Mac OS X

The easiest way to install Git on a Mac is via the stand-alone installer:

  1. Download the latest Git for Mac installer.
  2. Follow the prompts to install Git.
  3. Open a terminal and verify the installation was successful by typing git --version:
    $ git --version
    git version 2.9.2
  4. Configure your Git username and email using the following commands, replacing Emma's name with your own. These details will be associated with any commits that you create:
    $ git config --global user.name "Emma Paris"
    $ git config --global user.email "eparis@atlassian.com"
  5. (Optional) To make Git remember your username and password when working with HTTPS repositories, configure the git-credential-osxkeychain helper.

Install GIT on Windows

Git for Windows stand-alone installer:

  1. Download the latest Git for Windows installer.
  2. When you've successfully started the installer, you should see the Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation. The default options are pretty sensible for most users.
  3. Open a Command Prompt (or Git Bash if during installation you elected not to use Git from the Windows Command Prompt).
  4. Run the following commands to configure your Git username and email using the following commands, replacing Emma's name with your own. These details will be associated with any commits that you create:
    $ git config --global user.name "Emma Paris" $ git config --global user.email "eparis@atlassian.com"
  5. Optional: Install the Git credential helper on Windows
    Bitbucket supports pushing and pulling over HTTP to your remote Git repositories on Bitbucket. Every time you interact with the remote repository, you must supply a username/password combination. You can store these credentials, instead of supplying the combination every time, with the Git Credential Manager for Windows.
Basics

Create empty Git repo in specified directory. Run with no arguments to initialize the current directory as a git repository:

git init

Clone repo located at <repo> onto local machine. Original repo can be located on the local filesystem or on a remote machine via HTTP or SSH:

git clone <repo>

Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user:

git config user.name <name>

Stage all changes in <directory> for the next commit. Replace <directory> with a <file> to change a specific file:

git add <directory>

Commit the staged snapshot, but instead of launching a text editor, use "message" as the commit message:

git commit -m "message"

List which files are staged, unstaged, and untracked:

git status

Display the entire commit history using the default format. For customization see additional options:

git log
Branches

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

git branch

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

git checkout -b <branch>

Merge <branch> into the current branch:

git merge <branch>
Remote repositories

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 remote add <name> <url>

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

git fetch <remote> <branch>

Fetch the specified remote;s copy of current branch and immediately merge it into the local copy:

git pull <remote>

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

git push <remote> <branch>
Reset

Reset staging area to match most recent commit, but leave the working directory unchanged:

git reset

Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory.

git reset --hard

Move the current branch tip backward to "commit", reset the staging area to match, but leave the working directory alone:

git reset <commit>

Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after <commit>.

git reset --hard <commit>
Push

Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing:

git push <remote> --force

Push all of your local branches to the specified remote:

git push <remote> --all

Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo:

git push <remote> --tags