git

Installation

Before we start using Git, we have to make it available on your computer. Even if it’s already installed, it’s probably a good idea to update to the latest version. We can either install it as a package or via another installer, or download the source code and compile it ourself

Linux

If we want to install the basic Git tools on Linux via a binary installer, we can generally do so through the basic package-management tool that comes with your distribution.

  • Debian/Ubuntu $ apt-get install git or sudo apt-get install git-core

  • Fedora $ dnf install git (Fedora 22 and later)

  • Arch Linux $ pacman -S git

and then open a terminal Ctrl + t and type git --version it wil shows the currently installed version

Windows

the easiest way to install Git on Windows is msysGit: or https://git-for-windows.github.io/ make sure you chek the option

Run the git from Windows Command Prompet

You’re done :) On the desktop should be a shortcut for

GitBash

to check just type git --version it will show the currently installed version

Mac/OS

An OSX Git installer is maintained and available for download at the Git website, at http://git-scm.com/download/mac. then open a terminal and type git --version it will show the currently installed version

Git via Git

If you already have Git installed, you can get the latest development version via Git itself: git clone https://github.com/git/git

First-Time Git Setup

Now that you have Git on your system, you’ll want to do a few things to customize your Git environment. You should have to do these things only once on any given computer; they’ll stick around between upgrades. You can also change them at any time by running through the commands again.

Your Identity

The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating:

$ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com

Again, you need to do this only once if you pass the –global option, because then Git will always use that information for anything you do on that system. If you want to override this with a different name or email address for specific projects, you can run the command without the –global option when you’re in that project.

Checking Your Settings

If you want to check your settings, you can use the git config --list command to list all the settings Git can find at that point:

$ git config --list
user.name=John Doe
user.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...
            

You can also check what Git thinks a specific key’s value is by typing git config <key>:

$ git config user.name
John Doe

Git Basics

This chapter covers every basic command you need to do the vast majority of the things you’ll eventually spend your time doing with Git. By the end of the chapter, you should be able to configure and initialize a repository, begin and stop tracking files, and stage and commit changes. We’ll also show you how to set up Git to ignore certain files and file patterns, how to undo mistakes quickly and easily, how to browse the history of your project and view changes between commits, and how to push and pull from remote repositories.

Getting a Git Repository

create a new directory, open it and perform a git init to create a new git repository.
This creates a new subdirectory named .git that contains all of your necessary repository files – a Git repository skeleton. At this point, nothing in your project is tracked yet.

checkout a repository

Create a working copy of a local repository by running the command git clone /path/to/repository when using a remote server, your command will be git clone username@host:/path/to/repository

Work Flow

your local repository consists of three “trees” maintained by git. the first one is your Working Directory which holds the actual files. the second one is the Index which acts as a staging area and finally the HEAD which points to the last commit you’ve made.

Add & Commit

You can propose changes (add it to the Index) using git add <filename> git add * This is the first step in the basic git workflow. To actually commit these changes use git commit -m "Commit message" Now the file is committed to the HEAD, but not in your remote repository yet.

Pushing Changes

Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute git push origin master Change master to whatever branch you want to push your changes to.

If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with git remote add origin <server> Now you are able to push your changes to the selected remote server

branching

Branches are used to develop features isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion. create a new branch named “feature_x” and switch to it using git checkout -b feature_x switch back to master git checkout master and delete the branch again git branch -d feature_x a branch is not available to others unless you push the branch to your remote repository git push origin <branch>

update & merge

to update your local repository to the newest commit, execute git pull in your working directory to fetch and merge remote changes. to merge another branch into your active branch (e.g. master), use git merge <branch> in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with git add <filename> before merging changes, you can also preview them by using git diff <source_branch> <target_branch>

tagging

it’s recommended to create tags for software releases. this is a known concept, which also exists in SVN. You can create a new tag named 1.0.0 by executing git tag 1.0.0 1b2e1d63ff the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag. You can get the commit id by looking at the...

log

You can add a lot of parameters to make the log look like what you want. To see only the commits of a certain author: git log --author=bob To see a very compressed log where each commit is one line: git log --pretty=oneline Or maybe you want to see an ASCII art tree of all the branches, decorated with the names of tags and branches: git log --graph --oneline --decorate --all See only which files have changed: git log --name-status These are just a few of the possible parameters you can use. For more, see git log –help

replace local changes

In case you did something wrong, which for sure never happens ;), you can replace local changes using the command git checkout -- <filename> this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.

If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this git fetch origin git reset --hard origin/master

Replace this text with your caption