Anda di halaman 1dari 24

2/8/13

Git Tutorial

530

Git Tutorial
Lars Vogel
Version 5.1 Copyright 2009, 2010, 2011, 2012 Lars Vogel 05.12.2012
Revision History Revision 0.1 13.09.2009 Lars Vogel Lars Vogel Created

Free tutorial, donate to support

by Lars Vogel

Revision 0.2 - 5.1

21.11.2009 - 05.12.2012

bug fixes and improvements

Git & SVN Hosting


ProjectLocker.com

Kindle Edition: Git Tutorial This tutorial 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.

Source code hosting made easy. Get started in 90 seconds!

Table of Contents
1. Git 1.1. What is Git? 1.2. Local and remote repositories 1.3. Branching and merging 1.4. How to commit in Git 2. Tools 3. Terminology 4. Installation 4.1. Ubuntu 4.2. Windows 4.3. Mac OS 5. Git Setup 5.1. Global configuration file 5.2. User Configuration 5.3. Color Highlighting 5.4. Query existing global Git settings 6. Ignore certain files 7. Tracking empty directories with .gitkeep 8. Getting started with Git 8.1. Overview 8.2. Create directory 8.3. Create Git repository 8.4. Create content 8.5. Add files to Git index 8.6. Commit to Git repository 9. Analyzing changes and the repository history 9.1. See differences since the last commit 9.2. Reviewing the status of the working copy 9.3. Repository history with Git log 9.4. See the changes in a commit 9.5. View the change history of a file 9.6. See which commmit and author changed a line in a file 10. Changing the last commit and deleting files 10.1. Correction of commit messages - git amend 10.2. Delete files

www.vogella.com/articles/Git/article.html

1/24

2/8/13
11. Adding more repositories

Git Tutorial

11.1. Setting up a remote (bare) Git repository 11.2. Adding shortname for a remote 11.3. Show the existing remote repositories 12. Cloning remote repositories and push and pull 12.1. Clone your repository 12.2. Push changes to another repository 12.3. Pull changes 13. Revert Changes 13.1. Revert changes in your working copy 13.2. Checkout existing versions 13.3. Undo or delete a commit 13.4. Undo all working directory changes including new files 13.5. Remove files based on .gitignore changes 14. Recovering lost commits 14.1. git reflog 14.2. Example 15. Tagging in Git 15.1. Creating tags in Git 15.2. Using tags 16. Branches 16.1. What are branches? 16.2. List available branches 16.3. Create new branch 16.4. Delete a branch 16.5. Push a branch to remote repository 17. Differences between branches 18. Remote branches, tracking branches and git fetch 18.1. Remote branches 18.2. Fetch 18.3. Tracking branches 18.4. Fetch vs. pull 19. Merging branches 19.1. Merging 19.2. Commands to merge two branches 20. Solving merge conflicts 20.1. What is a merge conflict 20.2. Example process for solving a merge conflict 21. Rebase 21.1. Rebasing commits in the same branch 21.2. Rebasing branches 21.3. Best practice for rebase 22. Stash to save uncommited changes 22.1. Stash to save uncommitted changes 22.2. Stash commands 23. Retrieving individual files 23.1. View file in different revision without checkout 23.2. See which commit deleted a file 24. Create and apply patches 24.1. What is a patch 24.2. Create and apply patches 25. Define alias 25.1. What is an alias 25.2. Example alias 26. Submodules - Repos inside other Git repos 26.1. Why use submodules 26.2. Cloning submodules

www.vogella.com/articles/Git/article.html

2/24

2/8/13
27. Git workflows 27.1. Providing a patch 27.2. Working with two repositories 27.3. Git blame 28. Installing a Git server 29. Online remote repositories

Git Tutorial

29.1. Cloning remote repositories 29.2. Add more remote repositories 29.3. Remote operations via http and a proxy 30. Git Hosting Provider 30.1. ssh key 30.2. GitHub 30.3. Bitbucket 31. Graphical UI's for Git 32. Get the Kindle edition 33. Questions and Discussion 34. Links and Literature

1. Git
1.1. What is Git?
Git is a distributed version control system (dvcs) written in the programming language C. A version control system allows the creation of a history for a collection of files and includes the functionality to revert the collection of files to another state. Another state might be a different collection of files or different content in the files. The collection of files is usually called source code. A distributed version control system has no central server which stores the data. Every local copy contains the full history of the source code. You may, for example, change the collection of files to a state from 2 days ago or you may switch between states for experimental features and production issues. Git keeps track of all versions. Therefore you can revert to any point in your source code history.

1.2. Local and remote repositories


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 use of a dvcs does not require a central code repository. Git commits file changes to your local repository and you can synchronize your repository with other (remote) repositories. Git allows you to clone repositories, e.g. create an exact copy of a repository including the complete history of the source code. Owners of repositories can synchronize changes via push (transferring changes to a remote repository) or pull (getting changes from a remote repository).

1.3. Branching and merging


Git supports branching, e.g. you can have different versions of your source code. 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. Branches in Git can be local. A branch created in a repository which was cloned from another repository does not need to have a counterpart in the remote repository. Git allows to merge changes from different branches. For example you may have a branch called master which contains the source code which you use to build your product which is delivered to your customers. You use another branch called feature_123 to finalize a certain feature and then use the Git merge command to bring the changes into your master branch.

1.4. How to commit in Git


If you modify a file and you want to persist this change in the repository you need to perform two steps in Git.
www.vogella.com/articles/Git/article.html 3/24

2/8/13

Git Tutorial

First you need to mark them to be relevant for Git. Marking changes as relevant for the version control is called staging or to add them to the index. After adding the files to the index, you store this change n the Git repository. Storing the changes in the Git repository is called committing. A commit creates a new version of the whole repository. For example, if you make a change in a file and want that this change is relevant for the next commit, you have to add the file to the index via the g i ta d df i l e command. The g i tc o m m i tm" y o u r c o m m i tm e s s a g e "commits the marked changes into the Git repository.

2. Tools
There is huge varity of Git tools. Git can be used from the command line. You also can use graphical tools, for example EGit for the Eclipse IDE.

3. Terminology
The following table provides a summary of important Git terminology. Table 1. Git Terminology
Term Repository Definition A repository contains the history, the different versions over time and all different branches and tags. In Git each copy of the repository is a complete repository. The repository allows you to retrieve revisions into your working copy. The working copy (sometimes also called working tree) contains the content of a commit which you can checkout from the Git repository. You can modify the content and commit the changes again to the Git repository. A branch is a separate code line with its own history. You can create a new branch from an existing one and change the code independently from other branches. One of the branches is the default (normally named master). Selecting a branch in Git terminology is called to checkout a branch. A tag points to a commit which uniquely identifies a version of the Git repository. With a tag, you can have a named point to which you can always revert, e.g. the coding of 25.01.2009 in the branch "testing". You commit your changes into a repository. This creates a new commit object in the Git repository which uniquely identifies a new revision of the content of the repository. This revision which can be retrieved later, for example if you want to see the source code of an older version. Each commit object contains the author and the committer, thus making it possible to identify the source of the change. The author and committer might be different people. A URL in Git determines the location of the repository. Represents a version of the source code. Git identifies revisions with SHA1 ids using a commit object. SHA1 ids are 160 bits long and are represented in hexadecimal. HEAD is a pointer to the currently selected commit object. The version before that can be addressed via HEAD~1 and so on.

Working copy

Branches

Tags

Commit

URL Revision

HEAD

4. Installation
4.1. Ubuntu
On Ubuntu you can install the Git command line tool via the following command:
s u d oa p t g e ti n s t a l lg i t c o r e

To install Git on other Linux distributions please check your vendor documentation.

4.2. Windows
A windows version of Git can be found on the msysgit Project site. The URL to this webpage is listed below.

www.vogella.com/articles/Git/article.html

4/24

2/8/13
h t t p : / / c o d e . g o o g l e . c o m / p / m s y s g i t /

Git Tutorial

4.3. Mac OS
The easiest way to install Git on a Mac is via a graphical installer. This installer can be found under the following URL.
h t t p : / / c o d e . g o o g l e . c o m / p / g i t o s x i n s t a l l e r

As this procedure it not an official Apple one, it may change from time to time. The easiest way to find the current procedure is to Google for the "How to install Git on a Mac" search term.

5. Git Setup
5.1. Global configuration file
Git allows you to store global settings in the . g i t c o n f i gfile. This file is located in the user home directory. Git stores the committer and author of a change in each commit. This and additional information can be stored in the global settings. The following will configure Git so that a certain user and email address is used, enable color coding and tell Git to ignore certain files.

5.2. User Configuration


Configure your user and email for Git via the following command.
#C o n f i g u r et h eu s e rw h i c hw i l lb eu s e db yg i t #O fc o u r s ey o us h o u l du s ey o u rn a m e g i tc o n f i gg l o b a lu s e r . n a m e" E x a m p l eS u r n a m e " #S a m ef o rt h ee m a i la d d r e s s g i tc o n f i gg l o b a lu s e r . e m a i l" y o u r . e m a i l @ g m a i l . c o m "

To make pushing to remote repositories easier and to avoid unnecessary commits, you can use the following commands.
#S e td e f a u l ts ot h a ta l lc h a n g e sa r ea l w a y sp u s h e dt ot h er e p o s i t o r y g i tc o n f i gg l o b a lp u s h . d e f a u l t" m a t c h i n g " #S e td e f a u l ts ot h a ty o ua v o i du n n e c e s s a r yc o m m i t s g i tc o n f i gg l o b a lb r a n c h . a u t o s e t u p r e b a s ea l w a y s

5.3. Color Highlighting


The following will enable some highlighting for the console.
g i tc o n f i gg l o b a lc o l o r . s t a t u sa u t o g i tc o n f i gg l o b a lc o l o r . b r a n c ha u t o

5.4. Query existing global Git settings


To query your Git settings, execute the following command:
g i tc o n f i gl i s t

6. Ignore certain files


Git can be configured to ignore certain files and directories. This is configured via the . g i t i g n o r efile. This file can be in any directory and can contain patterns for files. For example, you can tell Git to ignore the b i n directory via the following . g i t i g n o r efile in the main directory. You can use certain wildcards in this file. *will match several characters. The .(Dot) parameter will match one character.
#I g n o r ea l lb i nd i r e c t o r i e s b i n

www.vogella.com/articles/Git/article.html

5/24

2/8/13
#I g n o r ea l lf i l e se n d i n gw i t h~ * ~ #I g n o r et h et a r g e td i r e c t o r y #M a t c h e s" t a r g e t "i na n ys u b f o l d e r t a r g e t /

Git Tutorial

You can also setup a global . g i t i g n o r efile valid for all Git repositories via the c o r e . e x c l u d e s f i l e setting.
#C r e a t ea~ / . g i t i g n o r ei ny o u ru s e rd i r e c t o r y c d~ / t o u c h. g i t i g n o r e #E x c l u d eb i na n d. m e t a d a t ad i r e c t o r i e s e c h o" b i n "> >. g i t i g n o r e e c h o" . m e t a d a t a "> >. g i t i g n o r e e c h o" * ~ "> >. g i t i g n o r e e c h o" t a r g e t / "> >. g i t i g n o r e #C o n f i g u r eG i tt ou s et h i sf i l e #a sg l o b a l. g i t i g n o r e g i tc o n f i gg l o b a lc o r e . e x c l u d e s f i l e~ / . g i t i g n o r e

The local . g i t i g n o r efile can be committed into the Git repository and therefore is visible to everyone who clones the repository. The global . g i t i g n o r efile is only locally visible.

7. Tracking empty directories with .gitkeep


Git ignores empty directories, i.e. it does not put them under version control. If you want to track such a directory, it is a common practice to put a file called .gitkeep in the directory. The file could be called anything; Git assigns no special significance to this name. As the directory now contains a file, Git will include it into its version control mechanism.

8. Getting started with Git


8.1. Overview
In this chapter you create a few files, create a local Git repository and commit your files into this repository. The comments (marked with #) before the commands explain the specific actions. Open a command line / shell for the operations.

8.2. Create directory


The following commands create an empty directory which will be used as Git repository.
# S w i t c ht oh o m e c d~ / #C r e a t ead i r e c t o r y m k d i r~ / r e p o 0 1 #S w i t c hi n t oi t c dr e p o 0 1 #C r e a t ean e wd i r e c t o r y m k d i rd a t a f i l e s

8.3. Create Git repository


Every Git repository is stored in the . g i tfolder of the directory in which the Git repository has been created. This directory contains the complete history of the repository. The . g i t / c o n f i gfile contains the local configuration for the repository. The following will create a Git repository in the current directory.
#I n i t i a l i z et h eG i tr e p o s i t o r y #f o rt h ec u r r e n td i r e c t o r y g i ti n i t

8.4. Create content


The following commands create some files with some content that will be placed under version control.

www.vogella.com/articles/Git/article.html

6/24

2/8/13

Git Tutorial
#S w i t c ht oy o u rn e wd i r e c t o r y c d~ / r e p o 0 1 #C r e a t ean e wd i r e c t o r y m k d i rd a t a f i l e s #C r e a t eaf e wf i l e s t o u c ht e s t 0 1 t o u c ht e s t 0 2 t o u c ht e s t 0 3 t o u c hd a t a f i l e s / d a t a . t x t #P u tal i t t l et e x ti n t ot h ef i r s tf i l e l s> t e s t 0 1

8.5. Add files to Git index


Before committing to a Git repository you need to mark which changes should be committed by adding the new and changed files to the Git index. i.e. the staging area. This creates a snapshop of the affected files, if you afterwards change one of the files before committing, you need to add it again to the index to commit the new changes.
#A d da l l( f i l e sa n dd i r e c t o r i e s )t ot h ei n d e xo ft h e #G i tr e p o s i t o r y g i ta d d.

8.6. Commit to Git repository


After adding the files to the Git index, you can commit them to the Git repository. This creates a new Services Publications Connect vogella.com snapshot of all Tutorials your files Training in your Git repository.
#M a k eac o m m i to fy o u rf i l et ot h el o c a lr e p o s i t o r y g i tc o m m i tm" I n i t i a lc o m m i t " #S h o wt h el o gf i l ew i t ht h ec o m m i t s g i tl o g

9. Analyzing changes and the repository history


9.1. See differences since the last commit
The g i td i f fcommand allows the user to see the changes made. In order to test this, make some changes to a file and check what the g i td i f fcommand shows to you. Then, commit the changes to the repository.
#M a k es o m ec h a n g e st ot h ef i l e e c h o" T h i si sac h a n g e ">t e s t 0 1 e c h o" a n dt h i si sa n o t h e rc h a n g e ">t e s t 0 2 #C h e c kt h ec h a n g e sv i at h ed i f fc o m m a n d g i td i f f #C o m m i tt h ec h a n g e s ,aw i l lc o m m i tc h a n g e sf o rm o d i f i e df i l e s #b u tw i l ln o ta d da u t o m a t i c a l l yn e wf i l e s g i tc o m m i tam" T h e s ea r en e wc h a n g e s "

9.2. Reviewing the status of the working copy


The following commands show the current status of your repository, i.e. which files have changed and the changes in the files between the last commit.
#M a k es o m ec h a n g e si nt h ef i l e e c h o" T h i si san e wc h a n g e ">t e s t 0 1 e c h o" a n dt h i si sa n o t h e rn e wc h a n g e ">t e s t 0 2

#S e et h ec u r r e n ts t a t u so fy o u rr e p o s i t o r y #( w h i c hf i l e sa r ec h a n g e d/n e w/d e l e t e d ) g i ts t a t u s #S h o wt h ed i f f e r e n c e sb e t w e e nt h eu n c o m m i t t e df i l e s #a n dt h el a s tc o m m i ti nt h ec u r r e n tb r a n c h g i td i f f
BACK TO TOP

#A d dt h ec h a n g e st ot h ei n d e xa n dc o m m i t g i ta d d.& &g i tc o m m i tm" M o r ec h a a n g e s-t y p oi nt h ec o m m i tm e s s a g e "

9.3. Repository history with Git log


www.vogella.com/articles/Git/article.html 7/24

2/8/13

Git Tutorial

The g i tl o gcommands shows the history of your repository in the current branch, i.e. the list of commits.
#S h o wt h eh i s t o r yo fc o m m i t si nt h ec u r r e n tb r a n c h g i tl o g #S h o wt h eh i s t o r yo fc o m m i t si no n el i n e #w i t has h o r t e n e dv e r s i o no ft h ec o m m i ti d g i tl o go n e l i n ea b b r e v c o m m i t #S h o wt h eh i s t o r ya sg r a p hi n c l u d i n gb r a n c h e s g i tl o gg r a p hp r e t t yo n e l i n ea b b r e v c o m m i t

9.4. See the changes in a commit


To see the files which have been changed in a commit use the following command.
g i td i f f t r e en a m e o n l yr< c o m m i t _ i d >

To see the changes in a commit use the following command.


g i ts h o w< c o m m i t _ i d >

9.5. View the change history of a file


To see changes in a file you can use the poption in the g i tl o gcommand.
#g i tl o gf i l e n a m es h o w st h ec o m m i t sf o rt h i sf i l e g i tl o g[ f i l e n a m e ] #U s ept os e et h ed i f f so fe a c hc o m m i t g i tl o gpf i l e n a m e #f o l l o ws h o w st h ee n t i r eh i s t o r y #i n c l u d i n gr e n a m e s g i tl o gf o l l o wpf i l e

9.6. See which commmit and author changed a line in a file


The g i tb l a m ecommand allows you to see which commit and author modified a file on a line by line basis.
#g i tb l a m es h o w st h ea u t h o ra n dc o m m i tp e r #l i n eo faf i l e g i tb l a m e[ f i l e n a m e ] #t h eLo p t i o na l l o w st ol i m i tt h es e l e c t i o n #f o re x a m p l eb yl i n en u m b e r #o n l ys h o wl i n e1a n d2i ng i tb l a m e g i tb l a m eL1 2[ f i l e n a m e ]

10. Changing the last commit and deleting files


10.1. Correction of commit messages - git amend
The git amend command makes it possible to change the last commit message. In the above example the commit message was incorrect as it contained a typo. The following will correct this via the a m e n dparameter.
g i tc o m m i ta m e n dm" M o r ec h a n g e s-n o wc o r r e c t "

10.2. Delete files


If you delete a file which is under version control g i ta d d .will not pick this file up. You can use the g i tr mcommand to delete the file from your working directory and mark it for the next commit.
#C r e a t eaf i l ea n dp u ti tu n d e rv e r s i o nc o n t r o l t o u c hn o n s e n s e 2 . t x t

www.vogella.com/articles/Git/article.html

8/24

2/8/13

Git Tutorial
g i ta d d.& &g i tc o m m i tm" m o r en o n s e n s e " #R e m o v et h ef i l ev i aG i t g i tr mn o n s e n s e 2 . t x t #C o m m i tt h er e m o v a l g i tc o m m i tm" R e m o v e sn o n s e n s e 2 . t x tf i l e "

Alternatively you can use the git commit command with the aflag or the Aflag in the g i ta d d command.
#C r e a t eaf i l ea n dp u ti tu n d e rv e r s i o nc o n t r o l t o u c hn o n s e n s e . t x t g i ta d d.& &g i tc o m m i tm" an e wf i l eh a sb e e nc r e a t e d " #R e m o v et h ef i l e r mn o n s e n s e . t x t #T r ys t a n d a r dw a yo fc o m m i t t i n g>w i l ln o tw o r k g i ta d d.& &g i tc o m m i tm" an e wf i l eh a sb e e nc r e a t e d " #N o wc o m m i tw i t ht h eaf l a g g i tc o m m i tam" F i l en o n s e n s e . t x ti sn o wr e m o v e d " #A l t e r n a t i v e l yy o uc o u l da d dd e l e t e df i l e st ot h es t a g i n gi n d e xv i a g i ta d dA. g i tc o m m i tm" F i l en o n s e n s e . t x ti sn o wr e m o v e d "

11. Adding more repositories


11.1. Setting up a remote (bare) Git repository
We will now create a remote Git repository. Git allows you to store this remote repository either on the network or locally. A standard Git repository is different from a remote Git repository. A standard Git repository contains the working directory (single checkout of one version of the project) and the Git repository. You can work in this working directory by modifying content and committing the changes to the Git repository. Remote repositories do not contain working copies of the files. They only contain repository files. To create such a repository, set the b a r eflag. In order to simplify the following examples, the Git repository will be created locally in the filesystem.
#S w i t c ht ot h ef i r s tr e p o s i t o r y c d~ / r e p o 0 1 # g i tc l o n eb a r e.. . / r e m o t e r e p o s i t o r y . g i t #C h e c kt h ec o n t e n t ,i ti si d e n t i c a lt ot h e. g i td i r e c t o r yi nr e p o 0 1 l s~ / r e m o t e r e p o s i t o r y . g i t

11.2. Adding shortname for a remote


You can always push to a Git repository via its full URL. But you can also add a shortname to a repository via the g i tr e m o t ea d d command. o r i g i nis a special name which is normally used automatically, 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.
#A d d. . / r e m o t e r e p o s i t o r y . g i tw i t ht h en a m eo r i g i n g i tr e m o t ea d do r i g i n. . / r e m o t e r e p o s i t o r y . g i t #A g a i ns o m ec h a n g e s e c h o" Ia d d e dar e m o t er e p o ">t e s t 0 2 #C o m m i t g i tc o m m i tam" T h i si sat e s tf o rt h en e wr e m o t eo r i g i n " #I fy o ud on o tl a b e lar e p o s i t o r yi tw i l lp u s ht oo r i g i n g i tp u s ho r i g i n

11.3. Show the existing remote repositories


To see the existing definitions of the remote repositories, use the following command.
#s h o wt h ed e t a i l so ft h er e m o t er e p oc a l l e do r i g i n g i tr e m o t es h o wo r i g i n

To see the details of the remote repostory, e.g. the URL from which it was cloned you can use the following command.
www.vogella.com/articles/Git/article.html 9/24

2/8/13

Git Tutorial
#S h o wt h ee x i s t i n gd e f i n e dr e m o t er e p o s i t o r i e s g i tr e m o t e

12. Cloning remote repositories and push and pull


12.1. Clone your repository
Clone a repository and checkout a working copy in a new directory via the following commands.
#S w i t c ht oh o m e c d~ #M a k en e wd i r e c t o r y m k d i rr e p o 0 2 #S w i t c ht on e wd i r e c t o r y c d~ / r e p o 0 2 #C l o n e g i tc l o n e. . / r e m o t e r e p o s i t o r y . g i t.

12.2. Push changes to another repository


Make some changes and push them from your first repository to the remote repository via the following commands.
#M a k es o m ec h a n g e si nt h ef i r s tr e p o s i t o r y c d~ / r e p o 0 1 #M a k es o m ec h a n g e si nt h ef i l e e c h o" H e l l o ,h e l l o .T u r ny o u rr a d i oo n ">t e s t 0 1 e c h o" B y e ,b y e .T u r ny o u rr a d i oo f f ">t e s t 0 2 #C o m m i tt h ec h a n g e s ,aw i l lc o m m i tc h a n g e sf o rm o d i f i e df i l e s #b u tw i l ln o ta d da u t o m a t i c a l l yn e wf i l e s g i tc o m m i tam" S o m ec h a n g e s " #P u s ht h ec h a n g e s g i tp u s h. . / r e m o t e r e p o s i t o r y . g i t

12.3. Pull changes


Pull allows you to get the latest changes from another repository. In your second repository, pull in the recent changes in the remote repository, make some changes, push them to your remote repository.
#s w i t c ht os e c o n dd i r e c t o r y c d~ / r e p o 0 2 #p u l li nt h el a t e s tc h a n g e so fy o u rr e m o t er e p o s i t o r y g i tp u l l #m a k ec h a n g e s e c h o" Ac h a n g e ">t e s t 0 1 #c o m m i tt h ec h a n g e s g i tc o m m i tam" Ac h a n g e " #p u s hc h a n g e st or e m o t er e p o s i t o r y #o r i g i ni sa u t o m a t i c a l l yc r e a t e da sw ec l o n e do r i g i n a lf r o mt h i sr e p o s i t o r y g i tp u s ho r i g i n

You can pull in the changes in your first repository.


#s w i t c ht ot h ef i r s tr e p o s i t o r ya n dp u l li nt h ec h a n g e s c d~ / r e p o 0 1 g i tp u l l. . / r e m o t e r e p o s i t o r y . g i t / #c h e c kt h ec h a n g e s g i ts t a t u s

Secure Git Hosting


CloudForge.com/GitHosting Secure Git Hosting with CloudForge! Easy Setup - Get Free 30 Day Trial.

13. Revert Changes


www.vogella.com/articles/Git/article.html 10/24

2/8/13

Git Tutorial

13.1. Revert changes in your working copy


If you create files in your working copy which you do not want to commit, you can discard them with the g i t c l e a ncommand.
#C r e a t ean e wf i l ew i t hc o n t e n t e c h o" t h i si st r a s ht ob ed e l e t e d ">t e s t 0 4 #M a k ead r y r u nt os e ew h a tw o u l dh a p p e n #ni st h es a m ea sd r y r u n g i tc l e a nn #N o wd e l e t e g i tc l e a nf

If you deleted or changes a file but you have not yet added it to the index or committed the change, you can check out the file again.
#D e l e t eaf i l e r mt e s t 0 1 #R e v e r tt h ed e l e t i o n g i tc h e c k o u tt e s t 0 1 #C h a n g eaf i l e e c h o" o v e r r i d e ">t e s t 0 1 #R e s t o r et h ef i l e g i tc h e c k o u tt e s t 0 1

13.2. Checkout existing versions


You can check out older revisions of your source code via the commit ID. The commit ID is shown if you enter the g i tl o gcommand. It is displayed behind the c o m m i tword.
#S w i t c ht oh o m e c d~ / r e p o 0 1 #G e tt h el o g g i tl o g #C h e c k o u tt h eo l d e rr e v i s i o nv i a g i tc h e c k o u tc o m m i t _ i d

If you have added the changes to the staging index, you can also revert the changes in the index and checkout the file from the index.
# S o m en o n s e n s ec h a n g e e c h o" n o n s e n s ec h a n g e ">t e s t 0 1 #N o ta d d e dt ot h es t a g i n gi n d e x .T h e r e f o r ew ec a n #j u s tc h e c k o u tt h eo l dv e r s i o n g i tc h e c k o u tt e s t 0 1 #C h e c kt h er e s u l t c a tt e s t 0 1 #A n o t h e rn o n s e n s ec h a n g e e c h o" a n o t h e rn o n s e n s ec h a n g e ">t e s t 0 1 #W ea d dt h ef i l et ot h es t a g i n gi n d e x g i ta d dt e s t 0 1 #R e s t o r et h ef i l ei nt h es t a g i n gi n d e x g i tr e s e tH E A Dt e s t 0 1 #G e tt h eo l dv e r s i o nf r o mt h es t a g i n gi n d e x g i tc h e c k o u tt e s t 0 1

If you add a file to the index but do not want to commit the file, you can remove it from the index via the g i t r e s e tf i l ecommand.
#C r e a t eaf i l e t o u c hi n c o r r e c t . t x t # A c c i d e n t l ya d di tt ot h ei n d e x g i ta d d. # R e m o v ei tf r o mt h ei n d e x g i tr e s e ti n c o r r e c t . t x t # D e l e t et h ef i l e r mi n c o r r e c t . t x t

If you deleted a directory and you have not yet committed the changes, you can restore the directory via the following command:
g i tc h e c k o u tH E A D-y o u r _ d i r _ t o _ r e s t o r e

www.vogella.com/articles/Git/article.html

11/24

2/8/13

Git Tutorial

13.3. Undo or delete a commit


You can revert commits via the g i tr e v e r tcommand. g i tr e v e r twill revert the changes of a commit and record a new commit which documents that the other commit was reverted.
#R e v e r tac o m m i t g i tr e v e r tc o m m i t _ i d

You can also delete a commit with the g i tr e s e th a r dcommand. To push such a change you need to use the f o r c eparameter. In case you pushed a commit already, you should better use the g i t r e v e r tcommand.
#D e l e t et h ec o m m i tb e f o r eh e a d #~ 2w o u l dd e l e t et h el a s tt w o ,e t c g i tr e s e th a r dH E A D ~ 1 #D e l e t eu pt oac e r t a i nc o m m i t g i tr e s e th a r d< s h a 1 c o m m i t i d >

13.4. Undo all working directory changes including new files


If you want to undo all changes in your working directory including the deletion of new file you can use the following commands. Please note that you may lose data by these commands.
#R e m o v e ss t a g e da n dw o r k i n gd i r e c t o r yc h a n g e s g i tr e s e th a r d #R e m o v e sn e wf i l e sw h i c ha r es t i l lu n t r a c k e d g i tc l e a nfd

13.5. Remove files based on .gitignore changes


Sometimes you change your . g i t i g n o r e file. Git will stop tracking the new entries from this moment. The last version is still in the Git repository. If you want to remove the related files from your Git repository you need to do this explicitely via the following command. use.
#R e m o v ed i r e c t o r y. m e t a d a t af r o mg i tr e p o g i tr mrc a c h e d. m e t a d a t a #R e m o v ef i l et e s t . t x tf r o mr e p o g i tr mc a c h e dt e s t . t x t

This will not remove the file from the commit history. If the file should also be removed from the history, have a look at g i tf i l t e r b r a n c h which allows you to rewrite the commit history.

14. Recovering lost commits


14.1. git reflog
The Git reflog command gives a history of the complete changes of your current branch based on the HEAD revision.
g i tr e f l o g #O u t p u t #. . .s n i p. . . 1 f 1 a 7 3 aH E A D @ { 2 } :c o m m i t :M o r ec h a a n g e s-t y p oi nt h ec o m m i tm e s s a g e 4 5 c a 2 0 4H E A D @ { 3 } :c o m m i t :T h e s ea r en e wc h a n g e s c f 6 1 6 d 4H E A D @ { 4 } :c o m m i t( i n i t i a l ) :I n i t i a lc o m m i t

Git reflog lists also commits which you removed.

14.2. Example
The following example shows how you can use git reflog to revert to a commit which has been removed.
#A s s u m et h e I Df o rt h es e c o n dc o m m i ti s #4 5 c a 2 0 4 5 b e 3 a e d a 0 5 4 c 5 4 1 8 e c 3 c 4 c e 6 3 b 5 f 2 6 9 f 7 #R e s e t st h eh e a df o ry o u rt r e et ot h es e c o n dc o m m i t

www.vogella.com/articles/Git/article.html

12/24

2/8/13

Git Tutorial
g i tr e s e th a r d4 5 c a 2 0 4 5 b e 3 a e d a 0 5 4 c 5 4 1 8 e c 3 c 4 c e 6 3 b 5 f 2 6 9 f 7 #S e et h el o g g i tl o g #O u t p u ts h o w st h eh i s t o r yu n t i lt h e4 5 c a 2 0 4 5 b ec o m m i t #S e ea l lt h eh i s t o r yi n c l u d i n gt h ed e l e t i o n g i tr e f l o g #< O u t p u t > c f 6 1 6 d 4H E A D @ { 1 } :r e s e t :m o v i n gt o4 5 c a 2 0 4 5 b e 3 a e d a 0 5 4 c 5 4 1 8 e c 3 c 4 c e 6 3 b 5 f 2 6 9 f 7 #. . . s n i p . . . . 1 f 1 a 7 3 aH E A D @ { 2 } :c o m m i t :M o r ec h a a n g e s-t y p oi nt h ec o m m i tm e s s a g e 4 5 c a 2 0 4H E A D @ { 3 } :c o m m i t :T h e s ea r en e wc h a n g e s c f 6 1 6 d 4H E A D @ { 4 } :c o m m i t( i n i t i a l ) :I n i t i a lc o m m i t g i tr e s e th a r d1 f 1 a 7 3 a

15. Tagging in Git


15.1. Creating tags in Git
Git has the option to tag certain versions in the history so that you find them more easily at a later point in time. Most commonly, this is used to tag a certain version which has been released. You can list the available tags via the following command:
g i tt a g

15.2. Using tags


You can create a new tag via the g i tt a gcommand. Via the m parameter, you specify the description of this tag. The following command tags the current active HEAD.
g i tt a gv e r s i o n 1 . 6m' v e r s i o n1 . 6 '

You can also create tags for a certain commit id.


g i tt a gv e r s i o n 1 . 5m' v e r s i o n1 . 5 '[ c o m m i ti d ]

If you want to use the code associated with the tag, use:
g i tc h e c k o u t< t a g _ n a m e >

16. Branches
16.1. What are branches?
Git allows you to create branches, i.e. independent copies of the source code which can be changed independently from each other. The default branch is called master. Git allows you to create branches very fast and cheaply in terms of resource consumption. Developers are encouraged to use branches frequently. If you decide to work on a branch, you checkout this branch. This means that Git moves the HEAD pointer to the latest commit of the branch and populates the Working directory with the content of this commit. Untracked files remain unchanged and are available in the new branch. This allows you to create a branch for unstaged and uncommited changes at any point in time.

16.2. List available branches


The g i tb r a n c hcommand lists all locally available branches. The currently active branch is marked with * .
#l i s t sa v a i l a b l eb r a n c h e s g i tb r a n c h

www.vogella.com/articles/Git/article.html

13/24

2/8/13

Git Tutorial

If you want to see all branches (including remote tracking branches), use the afor the g i tb r a n c h command.
#l i s t sa l lb r a n c h e si n c l u d i n gt h er e m o t eb r a n c h e s g i tb r a n c ha

16.3. Create new branch


You can create a new branch via the g i tb r a n c h[ n e w n a m e ]command. This command allows optional to specify the commit id, if not specified the currently checked out commit will be used to create the branch.
#S y n t a x :g i tb r a n c h< n a m e >< h a s h > #< h a s h >i nt h ea b o v ei so p t i o n a l g i tb r a n c ht e s t i n g #S w i t c ht oy o u rn e wb r a n c h g i tc h e c k o u tt e s t i n g #S o m ec h a n g e s e c h o" C o o ln e wf e a t u r ei nt h i sb r a n c h ">t e s t 0 1 g i tc o m m i tam" n e wf e a t u r e " #S w i t c ht ot h em a s t e rb r a n c h g i tc h e c k o u tm a s t e r #C h e c kt h a tt h ec o n t e n to ft e s t 0 1i st h eo l do n e c a tt e s t 0 1

To create a branch and to switch to it at the same time you can use the g i tc h e c k o u tcommand with the bparameter.
#C r e a t eb r a n c ha n ds w i t c ht oi t g i tc h e c k o u tbb u g r e p o r t 1 2 #C r e a t e san e wb r a n c hb a s e do nt h em a s t e rb r a n c h # w i t h o u tt h el a s tc o m m i t g i tc h e c k o u tbm y b r a n c hm a s t e r ~ 1

16.4. Delete a branch


To delete a branch which is not needed anymore, you can use the following command.
# D e l e t eb r a n c ht e s t i n g g i tb r a n c hdt e s t i n g #C h e c ki fb r a n c hh a sb e e nd e l e t e d g i tb r a n c h

16.5. Push a branch to remote repository


By default Git will only push matching branches to a remote repository. That means that you have to manually push a new branch once. Afterwards "git push" will also push the new branch.
#P u s ht e s t i n gb r a n c ht or e m o t er e p o s i t o r y g i tp u s ho r i g i nt e s t i n g #S w i t c ht ot h et e s t i n gb r a n c h g i tc h e c k o u tt e s t i n g #S o m ec h a n g e s e c h o" N e w sf o ry o u ">t e s t 0 1 g i tc o m m i tam" n e wf e a t u r ei nb r a n c h " #P u s ha l li n c l u d i n gb r a n c h g i tp u s h

This way you can decide which branches should be visible to other repositories and which should be local branches.

17. Differences between branches


To see the difference between two branches you can use the following command.
#s h o w st h ed i f f e r e n c e si ny o u r

www.vogella.com/articles/Git/article.html

14/24

2/8/13
#b r a n c hb a s e do nt h ec o m m o n #a n c e s t o rf o rb o t hb r a n c h e s g i td i f fm a s t e r . . . y o u r _ b r a n c h

Git Tutorial

If you want to see the different in a branch since you diverted from another branch you can use the ... shortcut. For example if you compare a branch called your_branch with the master branch the following command does only show the changes in your_branch since it diverted from the master branch.
#s h o w st h ed i f f e r e n c e sb e t w e e n #c u r r e n th e a do fm a s t e ra n dy o u r _ b r a n c h g i td i f fm a s t e ry o u r _ b r a n c h

18. Remote branches, tracking branches and git fetch


18.1. Remote branches
Your local Git repository contains references to the state of the branches on the remote repositories to which it is connected. These local copies are called remote branches. You can see your remote branches with the following command.
#l i s ta l lr e m o t eb r a n c h e s g i tb r a n c hr

To see all branches or only the local branches you can use the following commands.
#l i s ta l ll o c a lb r a n c h e s g i tb r a n c h #l i s tl o c a la n dr e m o t eb r a c h e s g i tb r a n c ha

18.2. Fetch
You can update your remote branches with the g i tf e t c hcommand. The g i tf e t c hcommand updates your remote branches. The fetch command only updates the remote branches and none of the local branches and it does not change the working copy of the Git repository. Therefore you can run the g i tf e t c hcommand at any point in time. After reviewing the changes in the remote tracking branch you can merge or rebase these changes onto your local branches. Alternatively you can also use the g i tc h e r r y p i c k" s h a " command to take over only selected commits. The following code shows a few options how you can compare your branches.
#s h o wt h el o n ge n t r i e sb e t w e e nt h el a s tl o c a lc o m m i ta n dt h e #r e m o t eb r a n c h g i tl o gH E A D . . o r i g i n #s h o wt h ed i f ff o re a c hp a t c h g i tl o gpH E A D . . o r i g i n #s h o was i n g l ed i f f g i td i f fH E A D . . . o r i g i n

You can apply the changes of the remote branches on your local branch for example with the following command.
g i tp u l lr e b a s eo r i g i nm a s t e r

18.3. Tracking branches


Tracking branches are local branches which are directly connected to a remote branch. Tracking branches allow you to use the g i tp u l l and g i tp u s hcommand directly without specifying the branch and repository.
www.vogella.com/articles/Git/article.html 15/24

2/8/13

Git Tutorial

If you clone a Git repository, your local main is setup as a tracking branches for origin/master by Git. You can create new tracking branches by specifying the remote branch during the creation of a branch. The following example demonstrates that.
#s e t u pat r a c k i n gb r a n c hc a l l e dn e w b r a c h #w h i c ht r a c k so r i g i n / n e w b r a n c h g i tc h e c k o u tbn e w b r a n c ho r i g i n / n e w b r a n c h

18.4. Fetch vs. pull


The g i tp u l lcommand performs a g i tf e t c hand g i tm e r g e(or g i tr e b a s ebased on your Git settings). The g i tf e t c hdoes not perform any operations on your local branches. I can always run the fetch command and review the incoming changes.

19. Merging branches


19.1. Merging
Git allows to combine the changes of two branches. This process is called merging. Merge performs a so-called 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 in the branch onto which you merged the changes of the other branch.

19.2. Commands to merge two branches


You can merge changes from one branch to the current active one via the following command.
#S y n t a x :g i tm e r g e< b r a n c h n a m e > g i tm e r g et e s t i n g

20. Solving merge conflicts


20.1. What is a merge conflict
A merge conflicts occurs, if two people have modified the same content and Git cannot automatically determine how both changes should be applied. If a merge conflict occurs Git will mark the conflict in the file and the programmer has to resolve the conflict manually. After resolving it, he can add the file to the staging index and commit the change.

20.2. Example process for solving a merge conflict


The following example first creates a merge conflict and then resolve it and apply the change to the Git repository. The following code will create a merge conflict.
#S w i t c ht ot h ef i r s td i r e c t o r y c d~ / r e p o 0 1 #M a k ec h a n g e s e c h o" C h a n g ei nt h ef i r s tr e p o s i t o r y ">m e r g e c o n f l i c t . t x t #S t a g ea n dc o m m i t g i ta d d.& &g i tc o m m i tam" W i l lc r e a t em e r g ec o n f l i c t1 " #S w i t c ht ot h es e c o n dd i r e c t o r y c d~ / r e p o 0 2 #M a k ec h a n g e s t o u c hm e r g e c o n f l i c t . t x t e c h o" C h a n g ei nt h es e c o n dr e p o s i t o r y ">m e r g e c o n f l i c t . t x t #S t a g ea n dc o m m i t g i ta d d.& &g i tc o m m i tam" W i l lc r e a t em e r g ec o n f l i c t2 " #P u s ht ot h em a s t e rr e p o s i t o r y

www.vogella.com/articles/Git/article.html

16/24

2/8/13
g i tp u s h

Git Tutorial

#N o wt r yt op u s hf r o mt h ef i r s td i r e c t o r y #S w i t c ht ot h ef i r s td i r e c t o r y c d~ / r e p o 0 1 #T r yt op u s h>y o uw i l lg e ta ne r r o rm e s s a g e g i tp u s h #G e tt h ec h a n g e s g i tp u l lo r i g i nm a s t e r

Git marks the conflict in the affected file. This file looks like the following.
< < < < < < <H E A D C h a n g ei nt h ef i r s tr e p o s i t o r y = = = = = = = C h a n g ei nt h es e c o n dr e p o s i t o r y > > > > > > >b 2 9 1 9 6 6 9 2 f 5 e b f d 1 0 d 8 a 9 c a 1 9 1 1 c 8 b 0 8 1 2 7 c 8 5 f 8

The above is the part from your repository and the below one from the remote repository. You could now edit the file manually and then commit the changes. Alternatively, you could use the g i tm e r g e t o o l command. g i tm e r g e t o o lstarts a configurable merge tool that displays the changes in a split screen.
#E i t h e re d i tt h ef i l em a n u a l l yo ru s e g i tm e r g e t o o l #Y o uw i l lb ep r o m p t e dt os e l e c tw h i c hm e r g et o o ly o uw a n tt ou s e #F o re x a m p l eo nU b u n t uy o uc a nu s et h et o o l" m e l d " #A f t e r m e r g i n gt h ec h a n g e sm a n u a l l y ,c o m m i tt h e m g i tc o m m i tm" m e r g e dc h a n g e s "

21. Rebase
21.1. Rebasing commits in the same branch
The r e b a s ecommand allows you to combine several commits into one commit. This is useful as it allows the user to rewrite some of the commit history (cleaning it up) before pushing your changes to a remote repository. The following will create several commits which should be combined at a later point in time.
#C r e a t ean e wf i l e t o u c hr e b a s e . t x t #A d di tt og i t g i ta d d.& &g i tc o m m i tm" r e b a s e . t x ta d d e dt oi n d e x " #D os o m es i l l yc h a n g e sa n dc o m m i t e c h o" c o n t e n t "> >r e b a s e . t x t g i ta d d.& &g i tc o m m i tm" a d d e dc o n t e n t " e c h o"m o r ec o n t e n t "> >r e b a s e . t x t g i ta d d.& &g i tc o m m i tm" a d d e dm o r ec o n t e n t " e c h o"m o r ec o n t e n t "> >r e b a s e . t x t g i ta d d.& &g i tc o m m i tm" a d d e dm o r ec o n t e n t " e c h o"m o r ec o n t e n t "> >r e b a s e . t x t g i ta d d.& &g i tc o m m i tm" a d d e dm o r ec o n t e n t " e c h o"m o r ec o n t e n t "> >r e b a s e . t x t g i ta d d.& &g i tc o m m i tm" a d d e dm o r ec o n t e n t " e c h o"m o r ec o n t e n t "> >r e b a s e . t x t g i ta d d.& &g i tc o m m i tm" a d d e dm o r ec o n t e n t " #C h e c kt h eg i tl o gm e s s a g e g i tl o g

We will combine the last seven commits. You can do this interactively via the following command.
g i tr e b a s eiH E A D ~ 7

This will open your editor of choice and let you edit the commit message or s q u a s h/ f i x u pthe commit with the last one. Squash will combine the commit messages while f i x u pwill disregard the commit message.

21.2. Rebasing branches


You can use Git to rebase one branches on another one. As described the m e r g ecommand combines the changes of two branches. Rebase takes the changes of a branch, creates a patch for each commit and applies it to the other branch.
www.vogella.com/articles/Git/article.html 17/24

2/8/13

Git Tutorial

The final result for the source code is the same as with merge but the commit history is cleaner; the history appears to be linear.
#C r e a t en e wb r a n c h g i tb r a n c ht e s t i n g #C h e c k o u tt h eb r a n c h g i tc h e c k o u tt e s t i n g #M a k es o m ec h a n g e s e c h o" T h i sw i l lb er e b a s e dt om a s t e r ">t e s t 0 1 #C o m m i ti n t ot e s t i n gb r a n c h g i ta d dt e s t 0 1 g i tc o m m i tm" N e wf e a t u r ei nb r a n c h " #R e b a s et h em a s t e r g i tc h e c k o u tm a s t e r g i tr e b a s et e s t i n g

21.3. Best practice for rebase


You should always check your local branch history before pushing changes to another Git repository or review system. Git allows you to do local commits. This feature is frequently used to have points to which you can go back, if something should go wrong later during a feature development. If you do so you, before pushing, should look at your local branch history and validate, whether or not these commits are relevant for others. If they all belong to the implementation of the same feature you, most likely, want to summarize them in one single commit before pushing. The interactive rebase is basically rewriting the history. It is safe to do this as long as the commits have not been pushed to another repository. This means commits should only be rewritten as long as they have not been pushed. If you rewrite and push a commit that is already present in other Git repositories, it will look as if you implemented something that somebody already implemented in the past.

22. Stash to save uncommited changes


22.1. Stash to save uncommitted changes
Git provides the s t a s hcommand which allows to save the current uncommmitted changes and checkout the last committed revision. This allows you to pull in the latest changes or to develop an urgent fix. Afterwards you can restore the stashed changes, which will reapply the changes to the current version of the source code. In general using the stash command should be the exception in using Git. Typically you would create new branches for new features and switch between branches.

22.2. Stash commands


The following commands will save a stash and reapply them after some changes.
#C r e a t eas t a s hw i t hu n c o m m i t e dc h a n g e s g i ts t a s h #T O D Od oc h a n g e st ot h es o u r c e ,e . g .b yp u l l i n g #n e wc h a n g e sf r o mar e m o v er e p o #A f t e r w a r d sr e a p p l yt h es t a s h e dc h a n g e s #a n dd e l e t et h es t a s hf r o mt h el i s to fs t a s h e s g i ts t a s hp o p

It is also possible to keep a list of stashes.


#C r e a t eas t a s hw i t hu n c o m m i t e dc h a n g e s g i ts t a s hs a v e #S e et h el i s to fa v a i l a b l es t a s h e s g i ts t a s hl i s t #R e s u l tm i g h tb es o m e t h i n gl i k e : s t a s h @ { 0 } :W I Po nm a s t e r :2 7 3 e 4 a 0R e s i z ei s s u ei nD i a l o g s t a s h @ { 1 } :W I Po nm a s t e r :2 7 3 e 4 a 0S i l l yt y p oi nC l a s s n a m e #Y o uc a nu s et h eI Dt oa p p l yas t a s h g i ts t a s ha p p l ys t a s h @ { 0 }

www.vogella.com/articles/Git/article.html

18/24

2/8/13

Git Tutorial
#A l s oy o uc a nr e m o v eas t a s h e dc h a n g e g i ts t a s hd r o ps t a s h @ { 0 } #O rd e l e t ea l ls t a s h e s g i ts t a s hc l e a r #O ra p p l yt h el a t e s ts t a s ha n dd e l e t ei ta f t e r w a r d s g i ts t a s hp o p #A f t e r w a r d sr e a p p l yt h es t a s h e dc h a n g e s #a n dd e l e t et h es t a s hf r o mt h el i s to fs t a s h e s g i ts t a s hp o p

23. Retrieving individual files


23.1. View file in different revision without checkout
The g i ts h o wcommand allows to see and retrieve files from branches and commits. It allows to see files from branches or commits without switches to these branches or commits. The following commands demonstrate that. You can also make a copy of the file.
#[ r e f e r e n c e ]c a nb eab r a n c h ,t a g ,H E A Do rc o m m i tI D #[ f i l e n a m e ]i st h ef i l e n a m ei n c l u d i n gp a t h g i ts h o w[ r e f e r e n c e ] : [ f i l e n a m e ] #T om a k eac o p yt oc o p i e d f i l e . t x t g i ts h o w[ r e f e r e n c e ] : [ f i l e n a m e ]>c o p i e d f i l e . t x t

23.2. See which commit deleted a file


You can use the -option in g i tl o gto see the commit history for file, even if you have delete the file.
g i tl o g-[ f i l e n a m e ]

24. Create and apply patches


24.1. What is a patch
A patch is a text file that contains changes to the source code. This file can be sent to someone else and this person can use this file to apply the changes to his/her local repository.

24.2. Create and apply patches


The following example creates a branch, changes the files and commits these changes into the branch.
#C r e a t ean e wb r a n c h g i tb r a n c hm y b r a n c h #U s et h i sn e wb r a n c h g i tc h e c k o u tm y b r a n c h #M a k es o m ec h a n g e s t o u c ht e s t 0 5 #C h a n g es o m ec o n t e n ti na ne x i s t i n gf i l e e c h o" N e wc o n t e n tf o rt e s t 0 1 "> t e s t 0 1 #C o m m i tt h i st ot h eb r a n c h g i ta d d. g i tc o m m i tam" F i r s tc o m m i ti nt h eb r a n c h "

The next example creates a patch for these changes.


#C r e a t eap a t c h>g i tf o r m a t p a t c hm a s t e r g i tf o r m a t p a t c ho r i g i n / m a s t e r #T h i sc r e a t e dt h ef i l e : #p a t c h0 0 0 1 F i r s t c o m m i t i n t h e b r a n c h . p a t c h

To apply this patch to your master branch, switch to it and use the g i ta p p l ycommand.
#S w i t c ht ot h em a s t e r g i tc h e c k o u tm a s t e r #A p p l yt h ep a t c h

www.vogella.com/articles/Git/article.html

19/24

2/8/13

Git Tutorial
g i ta p p l y0 0 0 1 F i r s t c o m m i t i n t h e b r a n c h . p a t c h

Afterwards you can commit the changes introduced by the patches and delete the patch file.
#P a t c hi sa p p l i e dt om a s t e r #C h a n g ec a nb ec o m m i t e d g i ta d d. g i tc o m m i tam" A p p l i e dp a t c h " #D e l e t et h ep a t c hf i l e r m0 0 0 1 F i r s t c o m m i t i n t h e b r a n c h . p a t c h

25. Define alias


25.1. What is an alias
An alias in Git allows you to setup your own Git command. For example, you can define an alias which is a short form of your own favorite commands or you can combine several commands with an alias. Unfortunately, defining an alias is at the time of writing not completely supported in msysGit for Windows. You can do single aliases, e.g. c afor c a= commit -a) but you can not do ones beginning with ! .

25.2. Example alias


For example, the following defines the g i ta d d c o m m i t command which combines g i ta d d.A and g i tc o m m i tm . After defining this command, you can use it via the g i ta d d c o m m i tm " m e s s a g e " command.
g i tc o n f i gg l o b a la l i a s . a d d c o m m i t' ! g i ta d d.A& &g i tc o m m i t '

26. Submodules - Repos inside other Git repos


26.1. Why use submodules
Git allows you to include another Git repository into a Git repository. This is useful in case you want to include a certain library in another repository of in case you want to aggregate certain Git repositories. Git call these included Git repository submodules. Git allows you to commit, pull and push to these repositories independently. You add a submodule to a Git repository via the g i ts u b m o d u l ea d dcommand.
#A d das u b m o d u l et oy o u rG i tr e p o g i ts u b m o d u l ea d d[ U R Lt oG i tr e p o ]

26.2. Cloning submodules


To clone a Git repository which contains submodules you need to run in addition to the clone command the g i ts u b m o d u l ei n i t and g i ts u b m o d u l eu p d a t e command. The init command creates the local configuration file for the submodules if it does not yet exists and the update command clones the submodules.

27. Git workflows


The following description highlights typical Git workflows.

27.1. Providing a patch


Git emphasizes the creation of branches for feature development or to create bug fixes. The following description lists a typical Git workflow for fixing a bug in your source code (files) and providing a patch for it. This patch contains the changes and can be used by another person to apply the changes to his local Git repository. This description assumes that the person which creates the changes cannot push changes directly to the remote repository. For example you may solve an issue in the source code of an Open Source project and
www.vogella.com/articles/Git/article.html 20/24

2/8/13

Git Tutorial

want that the maintainer of the Open Source project integrates this into his project. 1. 2. 3. 4. 5. 6. Clone the repository, in case you have not done that. Create a new branch for the bug fix Modify the files (source code) Commit changes to your branch Create patch Send patch to another person or attach it to a bug report, so that is can be applied to the other Git repository You may also want to commit several times during 3.) and 4.) and rebase your commits afterwards. Even if you have commit rights, creating a local branch for every feature or bug fix is a good practice. Once your development is finished you merge your changes to your master and push the changes from master to your remote Git repository.

27.2. Working with two repositories


Sometimes you want to add another remote repository to your local Git repo and pull and push from and two both repositories. The following example describes how to add another remote repository and to pull and fetch from both repositories. You can add another remote repository called remote_name via the following command.
#a d dr e m o t e g i tr e m o t ea d d< r e m o t e _ n a m e >< u r l _ o f _ g i t r e p o > #s e ea l lr e p o s g i tr e m o t ev

For merging the changes in remote_name create a new branch called newbranch.
#c r e a t ean e wb r a n c hw h i c hw i l lb eu s e d #t om e r g ec h a n g e si nr e p o s i t o r y1 g i tc h e c k o u tb< n e w b r a n c h >

Afterwards you can pull from your new repository called remote_name and push to your original repository.
#r e m i n d e r :y o u ra c t i v eb r a n c hi sn e w b r a n c h #p u l lr e m o t e _ n a m ea n dm e r g e g i tp u l l< r e m o t e _ n a m e > #o rf e t c ha n dm e r g ei nt w os t e p s g i tf e t c h< r e m o t e _ n a m e > g i tm e r g e< r e m o t e _ n a m e > / < n e w b r a n c h > #a f t e r w a r d sp u s ht of i r s tr e p o s i t o r y g i tp u s huo r i g i nm a s t e r

27.3. Git blame


The following lists a few Git commands that are useful in the daily work with Git. Table 2. Useful Git Commands
Command Description Who created / modified the file

g i tb l a m ef i l e n a m e

28. Installing a Git server


As described before, you do not need a server. You can just use a file system or a public Git provider, such as Github or Bitbucket. Sometimes, however, it is convenient to have your own server, and installing it under Ubuntu is relatively easy.
www.vogella.com/articles/Git/article.html 21/24

2/8/13

Git Tutorial

First make sure you have installed ssh.


a p t g e ti n s t a l ls s h

If you have not yet installed Git on your server, you need to do this too.
s u d oa p t g e ti n s t a l lg i t c o r e

Create a new user for git.


s u d oa d d u s e rg i t

Now log on with your Git user and create a bare repository.
#L o g i nt os e r v e r #t ot e s tu s el o c a l h o s t s s hg i t @ I P _ A D D R E S S _ O F _ S E R V E R #C r e a t er e p o s i t o r y g i ti n i tb a r ee x a m p l e . g i t

Now you can commit to the remote repository.


m k d i rg i t e x a m p l e c dg i t e x a m p l e g i ti n i t t o u c hR E A D M E g i ta d dR E A D M E g i tc o m m i tm' f i r s tc o m m i t ' g i tr e m o t ea d do r i g i ng i t @ I P _ A D D R E S S _ O F _ S E R V E R : e x a m p l e . g i t g i tp u s ho r i g i nm a s t e r

29. Online remote repositories


29.1. Cloning remote repositories
Git also support remote operations. Git supports several transport types; the native protocol for Git is also called g i t . The following will clone an existing repository via the Git protocol.
g i tc l o n eg i t @ g i t h u b . c o m : v o g e l l a / g i t b o o k . g i t

Alternatively you could clone the same repository via the h t t pprotocol.
#T h ef o l l o w i n gw i l lc l o n ev i aH T T P g i tc l o n eh t t p : / / v o g e l l a @ g i t h u b . c o m / v o g e l l a / g i t b o o k . g i t

29.2. Add more remote repositories


If you clone a remote repository, the original repository will automatically be called o r i g i n . You can push changes to this origin repository via g i tp u s ho r i g i n . Of course, pushing to a remote repository requires write access to this repository. You can add more remote repositories to your repository via the g i tr e m o t ea d dn a m eg i t r e p o command. For example if you cloned the repository from above via the Git protocol, you could add the http protocol via:
/ /A d dt h eh t t p sp r o t o c o l g i tr e m o t ea d dg i t h t t ph t t p s : / / v o g e l l a @ g i t h u b . c o m / v o g e l l a / g i t b o o k . g i t

29.3. Remote operations via http and a proxy


It is possible to use the HTTP protocol to clone Git repositories. This is especially helpful, if your firewall blocks everything except http. Git also provides support for http access via a proxy server. The following Git command could, for
www.vogella.com/articles/Git/article.html 22/24

2/8/13

Git Tutorial

example, clone a repository 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.
#L i n u x e x p o r th t t p _ p r o x y = h t t p : / / p r o x y : 8 0 8 0 #O nW i n d o w s #S e th t t p _ p r o x y = h t t p : / / p r o x y : 8 0 8 0 g i tc l o n eh t t p : / / d e v . e c l i p s e . o r g / g i t / o r g . e c l i p s e . j f a c e / o r g . e c l i p s e . j f a c e . s n i p p e t s . g i t #P u s hb a c kt ot h eo r i g i nu s i n gh t t p g i tp u s ho r i g i n

This example uses the Git config settings.


/ /S e tp r o x yf o rg i tg l o b a l l y g i tc o n f i gg l o b a lh t t p . p r o x yh t t p : / / p r o x y : 8 0 8 0 / /T oc h e c kt h ep r o x ys e t t i n g s g i tc o n f i gg e th t t p . p r o x y / /J u s ti nc a s ey o un e e dt oy o uc a na l s or e v o k et h ep r o x ys e t t i n g s g i tc o n f i gg l o b a lu n s e th t t p . p r o x y

30. Git Hosting Provider


Instead of setting up your own server, you can also use a hosting service. The most popular Git hosting sites are GitHub and Bitbucket. Both offer free hosting with certain limitations.

30.1. ssh key


Most hosting provider allow to use the http protocol with manual user authentication or to use an ssh key for automatic authentication. An ssh key has a public and private part. The public part is uploaded to the hosting provider. If you interact with the hosting provider via ssh, the public key will be validated based on the private key which is hold locally. The ssh key is usually generated in the . s s hdirectory. Ensure that you backup existing keys in this directory before running the following commands. To create an ssh key under Ubuntu switch to the command line and issue the following commands.
#S w i t c ht oy o u r. s s hd i r e c t o r y c d~ / . s s h #I ft h ed i r e c t o r y #d o e sn o te x i s t ,c r e a t ei tv i a : #m k d i r. s s h #M a n u a l l yb a c k u pa l le x i s t i n gc o n t e n to ft h i sd i r ! ! ! #A f t e r w a r d sg e n e r a t et h es s hk e y s s h k e y g e ntr s aC" y o u r _ e m a i l @ y o u r e m a i l . c o m " #P r e s se n t e rt os e l e c tt h ed e f a u l td i r e c t o r y #Y o uw i l lb ep r o m p t e df o ra no p t i o n a lp a s s p h r a s e #Ap a s s p h r a s ep r o t e c t sy o u rp r i v a t ek e y #b u ty o u h a v et oe n t e ri tm a n u a l l yd u r i n gs s ho p e r a t i o n s

The result will be two files, i d _ r s awhich is your private key and i d _ r s a . p u bwhich is your public key. You find more details for the generation of a rsa key on the following webpages.
#L i n kt os s hk e yc r e a t i o no nG i t h u b h t t p s : / / h e l p . g i t h u b . c o m / a r t i c l e s / g e n e r a t i n g s s h k e y s

30.2. GitHub
GitHub can be found under the URL https://github.com/. GitHub is free for all public repositories, i.e. if you want to have private repositories which are only visible to people you select, you have to pay GitHub a monthly fee. Create an account at GitHub and create a repository. After creating a repository at GitHub you will get a description of all the commands you need to execute to upload your project to GitHub. Follow the instructions.
www.vogella.com/articles/Git/article.html 23/24

2/8/13

Git Tutorial

These instructions will be similar to the following:


G l o b a ls e t u p : S e tu pg i t g i tc o n f i gg l o b a lu s e r . n a m e" Y o u rN a m e " g i tc o n f i gg l o b a lu s e r . e m a i ly o u r . e m a i l @ g m a i l . c o m N e x ts t e p s : m k d i rg i t b o o k c dg i t b o o k g i ti n i t t o u c hR E A D M E g i ta d dR E A D M E g i tc o m m i tm' f i r s tc o m m i t ' g i tr e m o t ea d do r i g i ng i t @ g i t h u b . c o m : v o g e l l a / g i t b o o k . g i t g i tp u s huo r i g i nm a s t e r E x i s t i n gG i tR e p o ? c de x i s t i n g _ g i t _ r e p o g i tr e m o t ea d do r i g i ng i t @ g i t h u b . c o m : v o g e l l a / g i t b o o k . g i t g i tp u s huo r i g i nm a s t e r

30.3. Bitbucket
Bitbucket can be found under the URL https://bitbucket.org/. Bitbucket allows unlimited public and private repositories, while the number of participants for one private repository is currently limited to 5 collaborators. I.e. if you have more then 5 developers which need access to a private repository you have to pay money to Bitbucket.

31. Graphical UI's for Git


This tutorial focused on the usage of the command line for Git. After finishing this tutorial, you may want to look at graphical tools for working with Git. Git provides two graphical tools. g i t kshows the history and g i tg u ishows an editor that allows you to perform Git operations. The Eclipse EGit project provides Git integration into Eclipse, which is included in the latest Eclipse release.

32. Get the Kindle edition


This tutorial is available for your Kindle.

33. 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.com Google Group. I have created a short list how to create good questions which might also help you.

34. Links and Literature


Git homepage EGit - Teamprovider for Eclipse Video with Linus Torvalds on Git Git on Windows

www.vogella.com/articles/Git/article.html

24/24

Anda mungkin juga menyukai