Upload
francesco-spegni
View
306
Download
4
Tags:
Embed Size (px)
DESCRIPTION
A tutorial on using git, with a brief introduction on the git vcs model and some practical scenarios to understand how to use it. Also, some notes on using the egit plugin for eclipse
Citation preview
GIT
May the source be with you
L.Spalazzi, F. Spegni, G. Taccari
DII Università Politecnica delle Marche
Software Engineering Course Projects 2012/2013
GIT – May the source be with you2/65
Source code
● Hard to manage● Many components ...
● Many files ...
● Many lines …
● Need tools to manage the complexity● Version Control Systems
GIT – May the source be with you3/65
Version Control Systems / PROs
● Keep track of:● Current source code status
● Previous source code modifications (history)
● Different “lines” of development (branches)
● Allow to:● Jump back & forth between modifications
● Merge modifications made by different developers
● Detect conflicting changes – Prevent inconsistencies
GIT – May the source be with you4/65
Version Control Systems / CONs
● More copies of the source code● One shared version (at least)
● Many “personal” version (developer side)
● No free lunch● Always possible to restore things, but ...
● Easy to mess things in the repository– Forget who added this piece of code …
– Easy to break dependencies
● Easy to loose local modifications– Overwritten/deleted local modifications cannot be undone
GIT – May the source be with you5/65
Version Control Systems / Types
● Different types● ClientServer
– SVN, CVS, ...
● Distributed– Git, Mercurial, ...
● Different interactions
GIT – May the source be with you6/65
Version Control Systems / Centralized
GIT – May the source be with you7/65
Version Control Systems / Centralized
● PROs● Easier
– One reference (THE server)
● CONs● Availability
– No network, no party
● SinglePointofFailure
● Waste of space– Every branch copies all the
files
GIT – May the source be with you8/65
Version Control Systems / Distributed
GIT – May the source be with you9/65
Version Control Systems / Distributed
● PROs● Availability
– Every developer can have it all
● Redundance
● Save space– Only modified files are copied
(lazy approach)
● Social development
● CONs● More complex
– Also: more fun ;)
GIT – May the source be with you10/65
Git
● Version Control System● Distributed● Invented by Linus Torvalds● Used by many FLOSS projects
(social development)● Bitbucket – www.bitbucket.org
● GitHub – www.github.com
● 3 … 2 … 1 ... Ignition ...
GIT – May the source be with you11/65
Start to develop
● Scenario● A remote shared repository already exists, let's get the
code and start develop
● The URL of the repository is: git://repository/project.git
$ git clone git://repository/project.git myproject
...
$ cd myproject
$ ls
app.yaml index.yaml main.py
GIT – May the source be with you12/65
Edit files locally / 1
● Scenario● We want to modify a local file (e.g. index.html)
$ vim main.py … do some work …
$ git status# On branch master# Changed but not updated# (use “git add <file> … to update …## modified main.py# no changes added to commit (use “git add” … )
GIT – May the source be with you13/65
Edit files locally / 3
$ git add main.py
$ git status# On branch master# Changes to be committed# (use “git reset HEAD <file> …## modified main.py#
GIT – May the source be with you14/65
Edit files locally / 4
$ vim app.yaml… do some work …
$ git status# On branch master# Changes to be committed:# (use “git reset HEAD <file>” …## modified main.py# # Changed but not updated:# (use “git add <file>” …# # modified app.yaml
GIT – May the source be with you15/65
Edit files locally / 5
$ vim main.py… do some work on previously edited file …
$ git status# On branch master# Changes to be committed:# (use “git reset HEAD <file>” …## modified main.py# # Changed but not updated:# (use “git add <file>” …# # modified app.yaml# modified main.py
GIT – May the source be with you16/65
Edit files locally / 6
$ git add app.yaml main.py
$ git status# On branch master# Changes to be committed:# (use “git reset HEAD <file>” …## modified main.py# modified app.yaml
GIT – May the source be with you17/65
Edit files locally / 7
$ git commit… an editor prompt appear …
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit.# On branch master# Changes to be committed:# (use “git reset HEAD <file> …” to unstage)## modified app.yaml# modified main.py
GIT – May the source be with you18/65
Edit files locally / 8
These are very important modifications!!!
# Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit.# On branch master# Changes to be committed:# (use “git reset HEAD <file> …” to unstage)## modified app.yaml# modified main.py
GIT – May the source be with you19/65
Edit files locally / 9
$ git commit
… control returns from editor …
Created commit XYZ: These are very important modifications 2 files changed, M insertions(+), N deletions()
GIT – May the source be with you20/65
Edit files locally / Break
● One modification, several statuses
● By default modifications are not added to the index
● In order to prevent messing the repository with temporary modifications
Working Directory
Index
Repository
git add
git commit
GIT – May the source be with you21/65
Edit files locally / Break
● Basic workflow● Edit
● Stage (git add)
● Review (git status)
● Confirm (git commit)
● Loop
Working Directory
Index
Repository
git add
git commit
GIT – May the source be with you22/65
One feature, one branch / 1
● Scenario● you have a copy of your
source code and you are assigned to work on a specific feature of the project
● in order to keeps code clean, you can work on a different “branch” of the code
● in this way your code doesn't cause any troubles to others
$ git branch myfeature* master myfeature remotes/origin/HEAD …/master→ remotes/origin/master
$ git checkout myfeature
$ git branch a master* myfeature remotes/origin/HEAD …/master→ remotes/origin/master
GIT – May the source be with you23/65
One feature, one branch / 2
● Scenario● you can jump backandforth between branches
(without making modifications)
$ git checkout master
$ git branch a* master myfeature remotes/origin/HEAD origin/master→ remotes/origin/master
$ git checkout myfeature
GIT – May the source be with you24/65
One feature, one branch / 3
● What's going on?● Commits form a graph
● Branches are labels to specific commits
● Git checkout● Move the HEAD
C0 C1
master
myfeature
HEAD
GIT – May the source be with you25/65
One feature, one branch / 3
$ git checkout myfeature● Move the HEAD
C0 C1
master
myfeature
HEAD
GIT – May the source be with you26/65
One feature, one branch / 4
● Start working on the new branch …
… do some work …
$ git add file1 file2 …
$ git commitCreated commit C2 … myfeature
C2C0 C1
master
HEAD
GIT – May the source be with you27/65
One feature, one branch / 5
● One branch is not enough …
C3$ git checkout master… modify files …
$ git add file1 file2 …
$ git commitCreated commit C3 …
myfeature
C2C0 C1
master
HEAD
GIT – May the source be with you28/65
One feature, one branch / 6
● Put everything together● Merge modifications
● Solve conflicts
● Create new commit (C4)
$ git merge myfeature
C0 C1
master
myfeature
HEAD
C2
C3 C4
GIT – May the source be with you29/65
One feature, one branch / 7
● Two outcomes● Nonconflicting merge
● Happy!!! :)
$ git merge myfeatureUpdating …Fastforward … X files changed, Y insertions (+), Z deletions () …
$ git commit a m 'Merged myfeature in master'Created commit C4
C0 C1
master
myfeature
HEAD
C2
C3 C4
GIT – May the source be with you30/65
One feature, one branch / 8
● Two outcomes● Conflicting merge
● When the going gets tough … ;)
$ git merge myfeatureAutomerging main.py
CONFLICT (content): Merge conflict in main.py
Automatic merge failed; fix conflicts and then commit the result.
C0 C1
master
myfeature
HEAD
C2
C3 C4
GIT – May the source be with you31/65
One feature, one branch / 9
● Resolve a conflict$ vim main.py
… switch to the editor … <<<<<<< HEAD:main.py
<div id="footer">contact : [email protected]</div>
=======
<div id="footer">
please contact us at [email protected]
</div>
>>>>>>> myfeature:main.py… edit your file and save …
$ git commit a m 'Merged myfeature in master'Created commit C4
C0 C1
master
myfeature
HEAD
C2
C3 C4
GIT – May the source be with you32/65
One feature, one branch / 9
● Resolve a conflict (alternative)$ git mergetoolmerge tool candidates: kdiff3 tkdiff xxdiff meld gvimdiff opendiff emerge vimdiff
Merging the files: main.py
Normal merge conflict for 'main.py':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (meld):… switch control to the tool …
$ git commit a m 'Merged myfeature in master'Created commit C4
C0 C1
master
myfeature
HEAD
C2
C3 C4
GIT – May the source be with you33/65
What after?
● Continue to work on your branch, or …● … you learnt how to do! ;)
● … delete it!
$ git branch d myfeature
GIT – May the source be with you34/65
What after?
● Continue to work on your branch, or …● … you learnt how to do! ;)
● … delete it!
$ git branch d myfeature
GIT – May the source be with you35/65
So far, so good ...
● One way interaction● One remote repository cloned into local repository
● Local changes made
● Local changes have been committed to local repository
● When/How do we share code?!?
GIT – May the source be with you36/65
Spread the code ...
Developer 1
Developer 2
(1)git clone
(2)git push
(3)git checkout / pull
git clone(1)
git push(2)
git checkout / pull(3)
git add / commit / branch / checkout / merge
git add / commit / branch / checkout / merge
GIT – May the source be with you37/65
Push a local branch to remote 1
● Scenario● You created a
local branch myfeature and now you want to share that code with your colleegues
$ git checkout myfeature
$ git branch a master* myfeature remotes/origin/HEAD origin/master→ remotes/origin/master
… work … git add … git commit …
$ git push origin myfeature
$ git branch a master* myfeature remotes/origin/HEAD origin/master→ remotes/origin/master remotes/origin/myfeature
GIT – May the source be with you38/65
Push a local branch to remote 2
● Problem● What if two developers push on the same repository and branch
concurrently?!? Solution: the second needs to do “git pull ...”
Develper 1
$ git push origin foo
Password for …
Counting objects: 4, done.Delta compression using up to 2 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 299 bytes, done.…
Developer 2
$ git push origin foo
Password for https://...
To https://... ! [rejected] foo > foo (nonfastforward)error: failed to push some refs to 'https://...'hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fastforwards' in 'git push help' for details.
GIT – May the source be with you39/65
Fetch a remote branch
● Scenario● You want to import a remote branch in your local
repository, and start to work with it (the first time)$ git branch a* master remotes/origin/HEAD origin/master→ remotes/origin/master remotes/origin/myfeature
$ git checkout track origin/myfeatureSwitched to a new branch 'myfeature'
$ git branch a master* myfeature remotes/origin/HEAD origin/master→ remotes/origin/master remotes/origin/myfeature
GIT – May the source be with you40/65
Pull modifications from a remote branch
● Scenario● You already fetched a remote branch; your colleegues
updated the code; they pulled their code on the shared repository and you want to import that code
$ git branch a master* myfeature remotes/origin/HEAD origin/master→ remotes/origin/master remotes/origin/myfeature
$ git pull origin myfeature… summary of modifications and automerge result …
GIT – May the source be with you41/65
Last steps / Without hands ...
● A local repository can be linked to more remote repositories
● Code can be merged from different repositories● Social development
● Work on a common projects
● Experiment independently from mainstream
GIT – May the source be with you42/65
Remote Repositories / 1
● Workflow● Group1 creates the mainstream repository R1
● Group2 creates repository R2 as a fork of R1 in order to work independently
– Forks can be created using the site interface (Bitbucket, GitHub, …)
● Dev2.1 from Group2 gitclone R2 and creates LR2.1– LR2.1 (remote)> R2
● Dev2.1 wants to track changes in R1 also– Social development
● Dev2 add a remote– LR2.1 (remote)> R2
– LR2.1 (remote)> R1
GIT – May the source be with you43/65
Remote Repositories / 2
● A remote repository (or just “remote”) is added to the local repository
$ git remote add mainstream git://URL/project.git
$ git fetch mainstream
$ git branch a* master remotes/origin/HEAD origin/master→ remotes/origin/master remotes/origin/myfeature remotes/mainstream/master
GIT – May the source be with you44/65
Remote Repositories / 3
● Scenario● The mainstream developers added a very important
feature, your project must integrate it
$ git pull mainstream master
# alternative solution # (not my favourite, because it fetches# all the branches in remote mainstream)
$ git fetch mainstrea$ git merge mainstream/master
GIT – May the source be with you45/65
Final Tricks / 1
● Scenario● You made some local modification after the last commit /
merge. Finally you realize you want to discard your work and go back to the last “stable” code you get from the repository
$ vim file1
… do some work …
$ git status# … show staged/unstaged modifications …$ git stash save$ git diff # this produce no output$ git stash drop # if you want to delete it forever$ git stash apply # if you want to have your changes back
GIT – May the source be with you46/65
Final Tricks / 2
● Scenario● Where does this code come from?!? You cloned a repository, worked
with it for a few months (years?!?) and you don't remember where is your code from (URL, repository name, … )
$ cd <PROJECT_HOME>
$ vim .git/config
… your project git config appear in your editor (vim) …
GIT – May the source be with you47/65
Git & Eclipse / 1
$ git clone …
GIT – May the source be with you48/65
Git & Eclipse / 2
$ git clone …
GIT – May the source be with you49/65
Git & Eclipse / 3
$ git clone …
GIT – May the source be with you50/65
Git & Eclipse / 4
$ git add …
$ git commit …
GIT – May the source be with you51/65
Git & Eclipse / 5
$ git add …
$ git commit …
GIT – May the source be with you52/65
Git & Eclipse / 6
$ git branch / checkout / merge
GIT – May the source be with you53/65
Git & Eclipse / 7
$ git branch / checkout / merge
GIT – May the source be with you54/65
Git & Eclipse / 8
$ git branch / checkout / merge
GIT – May the source be with you55/65
Git & Eclipse / 9
$ git branch / checkout / merge
GIT – May the source be with you56/65
Git & Eclipse / 10
$ git push …
GIT – May the source be with you57/65
Git & Eclipse / 11
$ git push …
GIT – May the source be with you58/65
Git & Eclipse / 5
$ git remote add …
$ git remote del …
GIT – May the source be with you59/65
Git & Eclipse / 6
$ git stash save …
$ git stash delete …
GIT – May the source be with you60/65
Final remarks
● Sometimes LESS IS MORE … ● … not with Version Control Systems (VCSs)● … not with GIT● Steep learning curve● It paysoff● Other VCSs are simpler to learn
● … but less powerful
GIT – May the source be with you61/65
References for the SWEng Projects
● Core project:● Site:
https://smc.dii.univpm.it/smc
● Git: https://<utente>@bitbucket.org/smcteam/smc.git
● Students project:● Site:
https://smc.dii.univpm.it/ids1213g<NUM>https://smc.dii.univpm.it/idstid1213g<NUM>e.g.https://smc.dii.univpm.it/ids1213g01
● Git:https://<utente>@bitbucket.org/smcteam/ids1213g<NUM>.githttps://<utente>@bitbucket.org/smcteam/idstid1213g<NUM>.gite.g.https://[email protected]/smcteam/ids1213g01.git
Use the force!
Questions?
GIT – May the source be with you63/65
Dictionary / Words matter / 1
● Repository● A directory containing some shared code. The repository is usually hosted in
some server and is thus characterized by a URL
● Clone● Action with which the content of a (remote) repository is copied in the local
workspace
● Add● Action used to stage a modified file
● Commit● An action with which staged modifications are added to the repository
● Staged:● A modification is staged when it has been marked as ready to be committed
GIT – May the source be with you64/65
Dictionary / Words matter / 2
● Branch● In the graph of the commits, it is a new line of development. A
branch has a name which refers to the last commit in that line of development. Every repository has a main branch usually called master
● Merge● The action with which the code of two branches is unified
● Conflict● A state where two modifications have been made in the same
file. In this case it is important to resolve the conflict, meaning that a programmer should approve/discard one/both of the modifications to the file
GIT – May the source be with you65/65
Credits
● http://gitscm.com/The tool
● http://gitscm.com/bookTHE book
● https://github.com/schacon/gitpresentationsThis is a examples and many things ...
● http://docs.joomla.orgFor the Centralized/Distributed VCS images
● http://www.github.comFor the background