| Java, Eclipse and Web programming Tutorials |
Version 1.4
Copyright © 2009 - 2010 Lars Vogel
20.02.2010
| Revision History | ||
|---|---|---|
| Revision 0.1 | 13.09.2009 | Lars Vogel |
| Created | ||
| Revision 0.2 | 21.11.2009 | Lars Vogel |
| Added installation and link to wiki | ||
| Revision 0.3 | 22.11.2009 | Lars Vogel |
| Getting the Eclipse source via Git | ||
| Revision 0.4 | 26.11.2009 | Lars Vogel |
| Minor improvements | ||
| Revision 0.5 | 08.12.2009 | Lars Vogel |
| Added git-svn | ||
| Revision 0.6 | 10.12.2009 | Lars Vogel |
| Command line usage of Git described | ||
| Revision 0.7 | 11.12.2009 | Lars Vogel |
| Moved EGit to own article | ||
| Revision 0.8 | 12.12.2009 | Lars Vogel |
| How to create and apply a patch | ||
| Revision 0.9 | 13.12.2009 | Lars Vogel |
| Added link to EGit (Git for Eclipse) | ||
| Revision 1.0 | 20.12.2009 | Lars Vogel |
| How to check git via http and behind a proxy | ||
| Revision 1.1 | 21.01.2010 | Lars Vogel |
| correction in example code | ||
| Revision 1.2 | 12.02.2010 | Lars Vogel |
| how to see the changes | ||
| Revision 1.3 | 19.02.2010 | Lars Vogel |
| fixed explanation of -a option | ||
| Revision 1.4 | 20.02.2010 | Lars Vogel |
| fixed typos | ||
Table of Contents
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.
On Ubuntu you can install the Git command line tool via "sudo apt-get install git-core".
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.
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"
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
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
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.
# 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
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
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 .
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
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
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
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
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
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
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.
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 .
The Eclipse EGit project provides Git integration into Eclipse. See Git with Eclipse (EGit) - Tutorial for details.
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.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.
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