Git Tutorial                                                                                           http://www.vogella.de/articles/Git/article.
html
                                                 Home      Blog      Java         Eclipse   Google   Web     Technology       Algorithms
               Git Tutorial
               Lars Vogel
               Version 3.0
               Copyright © 2009 - 2010 Lars Vogel
               20.11.2010
                Revision History
               Revision 0.1                             13.09.2009                                               Lars Vogel
               Created
               Revision 0.2 - 3.0                       21.11.2009 - 20.11.2010                                  Lars Vogel
               bug fixes and improvements
               Git Tutorial
               This article explains the usage of the distributed version control system Git via the command line. The examples were done
               on Linux (Ubuntu) but should also work on other operating systems like Microsoft Windows.
               Table of Contents
               1. Git
                      1.1. What is Git?
                      1.2. Important terminology
                      1.3. Staging index
               2. Installation
               3. Setup
                      3.1. User Configuration
                      3.2. Ignore certain files
               4. Getting started with Git
                      4.1. Create content
                      4.2. Create repository, add and commit
                      4.3. Commit changes
                      4.4. Correction of commit messages - git amend
                      4.5. Delete files
                      4.6. Setting up a remote git repository
                      4.7. Push changes to another repository
                      4.8. Add remote
                      4.9. Clone your repository
                      4.10. Push and pull some changes
               5. Revert Changes
               6. Tagging in Git
               7. Branches and Merging
                      7.1. Branches
                      7.2. Merging
                      7.3. Delete a branch
               8. Solving merge conflicts
               9. Rebase
               10. Working with Git
                      10.1. Status and change log
                      10.2. Create and apply patch
               11. Remote repository
                      11.1. Cloning remote repositories
                      11.2. Add more remotes
1 of 16                                                                                                                        11/24/2010 11:05 AM
Git Tutorial                                                                                                   http://www.vogella.de/articles/Git/article.html
                      11.3. Remote operations via http and a proxy
               12. Installating a git server
               13. GitHub
               14. Git and svn
               15. Graphical UI's for Git
                      15.1. UI for Git
                      15.2. TortoiseGit for Microsoft
                      15.3. Eclipse and EGit
               16. Thank you
               17. Questions and Discussion
               18. Links and Literature
               1. Git
               1.1. What is Git?
               Git is a distributed version control system (dvcs) written in C. A version control system allows that you can create a
               history for a collection of files with the possibility to revert them to another state. These collection of files is usually called
               "source code". In a distributed version control system everyone has a complete copy of the source code (including the
               complete history of the source code) and can perform version control operations against this local copy. The usage of a
               dvcs does not require a central code repository.
               Git supports branching, e.g. you can have different versions of your source code, for example if you want to develop a
               new feature you may open a branch in your source code and make the changes in this branch without affecting the main
               line of your code. Git keeps track of all versions. If you have done some changes which should be keept in the version
               control system you mark them as relevant (staging) and then you commit these changes to git.
               Git allows local commits to your repository and you can later merge these changes with remote repositories. If you want
               to get the source code from a repository you clone the repository. Once the repository is cloned it is possible to commit
               changes 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 can be used from the command line; this approach will be described in this tutorial. You also find graphical tools for
               example EGit for the Eclipse IDE .
               1.2. Important terminology
               Table 1. Git Terminology
                Term            Definition
               Repository      A repository contains the history, the different versions over time and all different branches and tags. In
                               Git each copy of the repository is again a complete repository. The repository allows to retrieve revisions
                               into your working copy.
               Branches        A Git repository contains always all branches and tags. One of the branches is the default (normally
               and Tags        named master). The user checkout a version branch to work in this branch, this is called the "working
                               copy".
               Commit          You commit your changes into a repository. This is create a new revision which can be later retrieved, for
                               example if you want to see the source code of an older version. Each commit contains the author and
                               commiter fields which identifies how create the change and who commited it.
               URL             An URL in Git determines the location of the repository.
               Revision        Represents a version of the source code. Git identifies revisions with SHA1 ids. SHA1 ids are 160-bit long
                               and are represented in hexadecimal. The latest version can be address via "HEAD", the version before
                               that via "HEAD~1" and so on.
2 of 16                                                                                                                                 11/24/2010 11:05 AM
Git Tutorial                                                                                                    http://www.vogella.de/articles/Git/article.html
               1.3. Staging index
               Git requires you to mark changes explicit to be relevant for committing them to the repository. For example if you
               what to have a file or changes in a file being relevant for the next change you have to add them to the so called
               "staging index" via git "add file". For files which were already committed once you can use the the "-a" flag during a
               commit. The staging index will be a complete snapshot of the changes.
               2. Installation
               On Ubuntu you can install the Git command line tool via "sudo apt-get install git-core". A windows version of git can be
               found on the msysgit Project site
               3. Setup
               3.1. User Configuration
               Configure your user and your email for git via the following command.
               To query your git settings via:
               3.2. Ignore certain files
               You can tell git to ignore files via the ".gitignore" file. This file can be in any directory and can contain pattern for files.
               For example you can tell git to ignore the directory "bin" via the following ".gitignore" in the main directory.
                         Git has also the global setting "core.excludesfile" for specifying global excludes.
                USB for DOS Print Driver DOS2USB is printer driver for DOS Application for USB Printer www.dos2usb.com
                SSH Client for Windows EmTec's powerful SSH terminal offers tabbed sessions and scripts. www.emtec.com/zoc
                Japanese Reliable VPS Your favorite OS can be installed. 50GB/256MB around $17/mo. SALE! www.DigitalSwift.net
3 of 16                                                                                                                                  11/24/2010 11:05 AM
Git Tutorial                                                                                                   http://www.vogella.de/articles/Git/article.html
               4. Getting started with Git
               The following will guide you though a typical Git workflow. You will create a few files, create a local Git repository
               and commit your file into this repository. Afterwards you clone the repository and push and pull some changes
               between the repositories. Open a command line / shell for the operations. The comments before the commands try
               to explain the action your are performing.
               4.1. Create content
               Create some content which we later put under version control.
               4.2. Create repository, add and commit
               Create a Git repository, add the files to the repository and commit your changes.
               Every git repository is stored in the .git folder of the root directory. This directory contains in the .git/config the local
               configuration for the repository and the complete history of the repo.
               4.3. Commit changes
               Make some changes to the file, see the differences and commit them again to the repository.
4 of 16                                                                                                                                 11/24/2010 11:05 AM
Git Tutorial                                                                                                   http://www.vogella.de/articles/Git/article.html
               4.4. Correction of commit messages - git amend
               In the above example the commit message was incorrect. You can change the last commit message via the
               --amend parameter.
               4.5. Delete files
               If you delete a file which is under version control "git add ." will not pick this file up. You need to use the git
               commit command with the -a flag or to use the -u flag in the git add command.
               4.6. Setting up a remote git repository
               We will now create a remote git repository. Git allows to store this remote repository either on the network or
               locally. For simplification we will a local remote git repository. A standard git repository is different from a remote
               git repository. A standard git repository contains the source code and the git repository. You can work directly in
               this directory, e.g. the repo contains the working copy for all 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.
5 of 16                                                                                                                                11/24/2010 11:05 AM
Git Tutorial                                                                                             http://www.vogella.de/articles/Git/article.html
               4.7. Push changes to another repository
               Do some changes and push them from your first repository to the remote repository via the following
               commands.
               4.8. Add remote
               You can always push to a git repository via the full URL to it. But you can also add a "shortname" to a
               repository via the "git remote add" command. "origin" is a special name which is usually automatically used if
               you clone a git repository. Origin indicates the original repository from which you started. As we started from
               scratch this name is still available.
               4.9. Clone your repository
               Checkout a new version of your repository into a new directory via the following commands.
6 of 16                                                                                                                          11/24/2010 11:05 AM
Git Tutorial                                                                                           http://www.vogella.de/articles/Git/article.html
               4.10. Push and pull some changes
               Pull allows you to get the latest changes from another repository. In your new repository, make some
               changes, push them to your remote repository and pull these changes from your first repository.
               5. Revert Changes
               If you create files in your working copy which you don't want to commit you can discard them.
7 of 16                                                                                                                        11/24/2010 11:05 AM
Git Tutorial                                                                                           http://www.vogella.de/articles/Git/article.html
               You can checkout older revisions of your source code via the commit name.
               If you have not added the changes to the staging index you can also directly revert the changes.
               You can also revert commits via.
8 of 16                                                                                                                        11/24/2010 11:05 AM
Git Tutorial                                                                                               http://www.vogella.de/articles/Git/article.html
               If you deleted a file but you have not yet added it to the index or commited the change you can
               checkout the file again.
               You can also revert commits via.
               If you added a file to the index but don't want to commit the file you can remove it from the index via
               git reset file
               6. Tagging in Git
               Git has the option to tag certain versions in the history so that you later find then easier. Most
               commonly this is used to tag a certain version which has been released.
               You can list the available tags via:
               You can create a new tag via:
               If you want to use the code associated with the tag, use:
               7. Branches and Merging
9 of 16                                                                                                                            11/24/2010 11:05 AM
Git Tutorial                                                                                              http://www.vogella.de/articles/Git/article.html
               7.1. Branches
               Git allows to create branches, e.g. independent copies of the source code which can be changed
               independently from each other. The default branch is called "master". Git allows to create branches
               very fast and cheap in the sense of resource consumption. Developers are encouraged to use
               branches frequently.
               The following command lists all available branches. The currently active branch is marked with "*".
               You can create a new branch via the following.
               7.2. Merging
               Merge allows to combine the changes of two branches. Merge does perform a three way merge
               between the latest snapshot of two branches based on the most recent common ancestor of both.
               As a result you have a new snapshot. You can merge changes from one branch to the current
               active one via the following command.
               If a merge conflict occurs Git will mark the conflict in the file and the programmer has to manually
               fix the conflict. After resolving it he can add the file to the staging index and commit the change.
               7.3. Delete a branch
               To delete a branch which is not needed anymore you can use the following command.
10 of 16                                                                                                                          11/24/2010 11:05 AM
Git Tutorial                                                                                            http://www.vogella.de/articles/Git/article.html
               8. Solving merge conflicts
               Git allows to solve merge conflicts. The following will create a merge conflict.
               Git marks the conflict in the affected file. This file looks like the following.
               The above part is the part from your repo and the below one from the remote repository. You
               could now edit the file manually and then commit the changes. Alternatively you could use "git
               mergetool" which would use a configurable merge tool which displays the changes in a split
               screen.
11 of 16                                                                                                                        11/24/2010 11:05 AM
Git Tutorial                                                                                        http://www.vogella.de/articles/Git/article.html
               9. Rebase
               In addition to merge, Git allow also to use rebase. As described merge does combine the
               changes of two branches. Rebase takes the changes of a branch, creates a patch and apply
               it to another branch. The end result is the same as with merge but the commit history is
               cleaner; the history appears to be linear.
               10. Working with Git
               Of course Git allows more then just committing and push and pulling. The following
               demonstrates how to work with branches and how to create patches.
               10.1. Status and change log
               The following helps you to see the changes in your repository.
               10.2. 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.
12 of 16                                                                                                                    11/24/2010 11:05 AM
Git Tutorial                                                                                              http://www.vogella.de/articles/Git/article.html
               11. Remote repository
               11.1. Cloning remote repositories
               Git allows of courses also remote operations. Git supports several transport types, the
               native one of Git is the "git protocol".
               Alternatively you could clone the same repository via a the http protocol.
               11.2. Add more remotes
               If you clone a remote repository the original repository will automatically stored as "origan"
               and if you have write access to the remote repository you can send your changes to it via
               "git push origin" You can add more remotes repositories to your repository via "git remote
               add name gitrepo". For example if you cloned the repository from above via the git protocol
               you could add the http protocol via:
13 of 16                                                                                                                          11/24/2010 11:05 AM
Git Tutorial                                                                                                   http://www.vogella.de/articles/Git/article.html
               11.3. Remote operations via http and a proxy
               It is possible to use the HTTP protocol to clone Git 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. You can either set
               the proxy variable in general for all applications or set it only for git.
               This example uses environment variables.
               This example uses the git config settings.
               12. Installating a git server
               As described you don't need a server, you can just use a file system or a public git
               provider, as Github . But sometimes is it conviniant to have your own server and
               installing it under Ubuntu is very easy.
               First make sure you have ssh installed.
               If you have not yet installed git then you need to do this also.
14 of 16                                                                                                                               11/24/2010 11:05 AM
Git Tutorial                                                                                         http://www.vogella.de/articles/Git/article.html
               Create a new user for git.
               Now logon to the git user and create a bare repository.
               Now you can commit to the remote repository.
               13. 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 .
               Github requires you to create a ssh key. For creating such a key in Ubuntu please
               see ssh key creation in Ubuntu . For windows please see msysgit ssh key generation
               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.
               14. 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 .
15 of 16                                                                                                                     11/24/2010 11:05 AM
Git Tutorial                                                                                          http://www.vogella.de/articles/Git/article.html
               15. Graphical UI's for Git
               15.1. UI for Git
               git ships with two graphical tools. "gitk" shows the history and "git gui" show an
               editor which allows to perform git operations.
               15.2. TortoiseGit for Microsoft
               Similar to TortoiseSVN you can use TortoiseGit to have a integration into the file
               explorer under Windows.
               15.3. Eclipse and EGit
               The Eclipse EGit project provides Git integration into Eclipse. See Git with Eclipse
               for details.
               16. Thank you
               If you like this tutorial please support this website.
               17. Questions and Discussion
               Before posting questions, please see the vogella FAQ . If you have questions or
               find an error in this article please use the www.vogella.de Google Group . I have
               created a short list how to create good questions which might also help you.
               18. Links and Literature
               Git homepage
               Video with Linus Torwalds on Git
               Progit book - Free Git book
               Video casts about Git
               http://code.google.com/p/msysgit/ Git on Windows
               http://github.com/guides/git-cheat-sheet Git Cheat Sheets
               http://github.com/blog/626-announcing-svn-support SVN Support for GitHub
               Git in 5 Minutes by Scott Paul Robertson
               Git - SVN Crash Course
               http://www.ibm.com/developerworks/opensource/library/l-git-subversion-
               1/index.html Git Intro for subversion users by Teodor Zlatanov
16 of 16                                                                                                                      11/24/2010 11:05 AM