Java, Eclipse and Web programming Tutorials
Follow me on twitter About Lars Vogel

Version control with Git - Tutorial

Lars Vogel

Version 1.4

20.02.2010

Revision History
Revision 0.113.09.2009Lars Vogel
Created
Revision 0.221.11.2009Lars Vogel
Added installation and link to wiki
Revision 0.322.11.2009Lars Vogel
Getting the Eclipse source via Git
Revision 0.426.11.2009Lars Vogel
Minor improvements
Revision 0.508.12.2009Lars Vogel
Added git-svn
Revision 0.610.12.2009Lars Vogel
Command line usage of Git described
Revision 0.711.12.2009Lars Vogel
Moved EGit to own article
Revision 0.812.12.2009Lars Vogel
How to create and apply a patch
Revision 0.913.12.2009Lars Vogel
Added link to EGit (Git for Eclipse)
Revision 1.020.12.2009Lars Vogel
How to check git via http and behind a proxy
Revision 1.121.01.2010Lars Vogel
correction in example code
Revision 1.212.02.2010Lars Vogel
how to see the changes
Revision 1.319.02.2010Lars Vogel
fixed explanation of -a option
Revision 1.420.02.2010Lars Vogel
fixed typos

Using Git from command line

This article explains the usage of the distributed version control system Git via the command line.

The examples were done on Linux (Ubuntu).


Table of Contents

1. Overview
2. Installation
3. Using Git on the command line
3.1. Setup
3.2. Content
3.3. Git Repository
3.4. Setting up a remote git repository
3.5. Push some changes
3.6. Clone your repository
3.7. Pull some changes
3.8. Checkout a older version
3.9. Branching
3.10. Status and change log
3.11. Create and apply patch
4. Clone existing repositories via http
5. GitHub
6. Git and svn
7. Eclipse
8. Thank you
9. Questions and Discussion
10. Links and Literature

1. Overview

Git is a distributed version control system (dvcs) written in C.

A distributed version control system does not require a central code repository. Everyone has a complete copy of the source code (including the history of the source code) and can perform version control operations against this local copy.

Git allows local commits and to merge changes with remote repositories. If you want to copy a repository you clone it. Everyone can clone a Git repository and commit to the clone. Owners of repositories can merge changes via push (transfer changes to a remote repository) or via pull (getting changes from a remote repository).

Git provides command line tools for performing version control operations, e.g. for commits.

2. Installation

On Ubuntu you can install the Git command line tool via "sudo apt-get install git-core".

3. Using Git on the command line

Git can be used from the command line. Open a shell for the following operations. The comments before the commands try to explain the action your are performing.

3.1. Setup

Configure your user and your email for git via the following command.

				
# Config the user which will be used by git
# Of course you should use your name
git config --global user.name "Lars Vogel"
# Same for the email addess
git config --global user.email "Lars.Vogel@gmail.com"
# Set default so that always all changes are pushed to the repository
git config --global push.default "matching"

			

3.2. Content

Create some content which we later put under version control.

				
#Switch to home
cd ~/
# create a directory
mkdir repo01.git
# switch into it
cd repo01.git
# create a new directory
mkdir datafiles
# Create a few files
touch test01
touch test02
touch test03
touch datafiles/data.txt
# put a little text into the first file
ls >test01

			

3.3. Git Repository

Create a Git repository, add the files and commit your changes.

				
# Initialize the local Git repository
git init
# Add all (files and directory) to the Git repository
git add .
# Make a commit of your file to the local repository
git commit -m "Initial commit"
# Show the log file
git log


			

Tip

The git repository is stored in the .git folder of the root directory.

3.4. Setting up a remote git repository

We will now setup a remote repository which would be usually hosted on a webserver. Git allows to store this "remote" directory also locally and we will use this option.

Tip

A default / normal git repository is used also as a working directory. Git stores the repository files in a .git directory together with the normal files. Remote repositories do not contain working copies of the files, they only contain repository files. To create such are repository such the "--bare" flag.

				
# Switch to first repository
cd ~/repo01.git
# 
git clone --bare . ../remote-repository.git

# Check the content, should be equal to the .git file in repo01.git
ls ~//remote-repository.git

			

3.5. Push some changes

Do some changes and push them from your first repository to your second one via the following commands.

					
# Make some change in the first repos
cd ~/repo01

# Make some changes in the file
echo "Hello, hello. Turn your radio on" > test01
echo "Bye, bye. Turn your radio off" > test02

# commit the changes, -a will commit change changes for modified files
# but will not add automatically new files
git commit -a -m "Some changes"

# push the changes
git push ../remote-repository.git

				

3.6. Clone your repository

Checkout a new version of your repository into a new directory via the following commands.

					
# Switch to home
cd ~
# make new directory
mkdir repo02.git 

# Switch to new directory
cd ~/repo02.git
# 
git clone ../remote-repository.git .

				

3.7. Pull some changes

In your new repository, make some changes, push them to your remote repository and pull these changes from your first repository.

					
# Switch to home
cd ~

# Switch to second directory
cd ~/repo02.git
# Make changes
echo "A change" > test01
# Commit
git commit -a -m "A change"
# Push changes to remote repository
git push ../remote-repository.git/
# Switch to the first repository can pull in the changes
cd ~/repo01.git
git pull ../remote-repository.git/
# Check the changes
less test01

				

3.8. Checkout a older version

You can checkout older revisions of your source code via the hashcode.

					
# Switch to home
cd ~/repo01.git 
# get the log
git log

# Copy one of the older hashcodes and checkout the older revision vis 
git checkout HASHCODE


				

3.9. Branching

Create and use branches via the following command.

					
# Create a new branch
git branch mybranch
# Use this new branch
git checkout mybranch
# Do some changes
touch test04
# Commit these to the branch
git add .
git commit -a -m "First commit in the branch"
# Switch back to the master (this is always the main branch)
git checkout master
# Check that you have not test04 file
ls
# Merge your branch with the master 
git merge  mybranch
git add .
git commit -a -m "Merged branch"
# Delete the branch as it is not needed anymore
git branch -d  mybranch



				

3.10. Status and change log

The following helps you to see the changes in your repository.

					
// See the status
git status
// Shows a log file of the commits
git log
// This start a nice graphical view of the changes
gitk --all

				

3.11. Create and apply patch

The following creates a branch, makes some changes in this branch, creates a patch and applies the patch to the master.

					
# Create a new branch
git branch mybranch
# Use this new branch
git checkout mybranch
# Do some changes
touch test05
# Change some content in an existing file
echo "New content for test01" >test01
# Commit these to the branch
git add .
git commit -a -m "First commit in the branch"
# Create a patch --> git format-patch master
git format-patch origin/master
# Creates patch 0001-First-commit-in-the-branch.patch
# Switch to the master
git checkout master
# Apply the patch
git apply 0001-First-commit-in-the-branch.patch
# Do your normal commit in the master 
git add .
git commit -a -m "Applied patch"
# delete the patch 
rm 0001-First-commit-in-the-branch.patch
				

4. Clone existing repositories via http

Git allow to use the http protocol to clone repositories. That is especially helpful if your firewall blocks everything expect http. Git also support a proxy. For example the following command could clone a Eclipse project via http and a proxy.

			
export http_proxy=http://proxy:8080
// On Windows
// set http_proxy=http://proxy:8080 
git clone http://dev.eclipse.org/git/org.eclipse.jface/org.eclipse.jface.snippets.git

		

5. GitHub

So far all operations we done have been done without a server. To use a server you can use the free hosting offering from Github .

Create an account at Github and create a repository. After creating a repository at GitHub you get a description which commands to run to upload for project. Follow these instructions.

Tip

Github requires you to create a ssh key. After creating your private ssh key you have to add this in Ubuntu via "ssh-add id_rsa".

6. Git and svn

Git provides the "git-svn" command line tool to run git against a svn repository. A good explanation for this can be found Git and svn .

7. Eclipse

The Eclipse EGit project provides Git integration into Eclipse. See Git with Eclipse (EGit) - Tutorial for details.

8. Thank you

Thank you for practicing with this tutorial.

Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.

9. Questions and Discussion

For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.

I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.

10. Links and Literature

http://git-scm.com/ Git homepage

http://progit.org/ Free Git book

http://code.google.com/p/msysgit/ Git on Windows

http://github.com/guides/git-cheat-sheet Git Cheat Sheets