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
by Lars Vogel
21.11.2009 - 05.12.2012
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.
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.
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.
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
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.
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
#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
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
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 "
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
2/8/13
Git Tutorial
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
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
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 >
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.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
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.
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
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
This way you can decide which branches should be visible to other repositories and which should be local branches.
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
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
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
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.
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
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
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
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.
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
g i tb l a m ef i l e n a m e
2/8/13
Git Tutorial
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
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
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
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
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
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.
www.vogella.com/articles/Git/article.html
24/24