Version Control with Git - Politecnico di...

55
Giuseppe Massari [email protected] Version Control with Git Advanced Operating Systems [email protected]

Transcript of Version Control with Git - Politecnico di...

Page 1: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Giuseppe Massarigiuseppemassaripolimiit

Version Control with GitAdvanced Operating Systems

giuseppemassaripolimiit

Advanced Operating SystemsGiuseppe Massari

552Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Changes management Working with branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

553Why using version control tools

Single developer Do we need to keep track of the history of changes in a software project How to effectively ndash keep track of store ndash such changes

Copying sources into new directories (eg MyProject_v20151006_) What about disk occupancy

Multiple developers How to merge changes and additions coming from other developers

Manual copy-and-paste for each changed file Cloud storage (eg Dropbox)

How to keep track of whos the author of a change Mmm maybe looking for mails with source code attached

Advanced Operating SystemsGiuseppe Massari

554Why Git

Distributed approach The entire project history database is local

rarr Faster browsing among the changes rarr No traffic generated on the network rarr Changes can be committed locally before sharing rarr Higher reliability Central server DOWN is not an issue

Integrity mechanisms Every change is tracked SHA-1 checksum

40-character string composed of hexadecimal characterscalculated based on the contents of a file or directory structure in GIT

Advanced Operating SystemsGiuseppe Massari

555First steps

Help The command-line usage of Git is based on commands To get help about a specific command syntax

If no command is given an overview of the most common Git commands is returned

$ git help [command]$ git help [command]

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

Advanced Operating SystemsGiuseppe Massari

556First steps

Configuration Setup user name mail and preferences

Configuration ldquolevelsrdquo reference three possible configuration files

First configuration steps

$ git config ltlevelgt ltoptiongt value$ git config ltlevelgt ltoptiongt value

$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit

--system etcgitconfig System wide scope

--global ~gitconfig~configgit

User scope

--local gitconfig Local project scope

Advanced Operating SystemsGiuseppe Massari

557First steps

Basic concepts Repository

Concretely (sub)directory where in Git stores metadata and object database for the project (git)

Working directoryA checkout of one version of the project

The tracked files pulled out of the compressed database in the git directory

Staging area The set of selected changes ready to be committed In practice a file (ldquoINDEXrdquo) contained in git directory storing information about the changes to track in theforthcoming next commit

Workingdirectory

StagingArea

Repository(git directory)

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 2: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

552Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Changes management Working with branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

553Why using version control tools

Single developer Do we need to keep track of the history of changes in a software project How to effectively ndash keep track of store ndash such changes

Copying sources into new directories (eg MyProject_v20151006_) What about disk occupancy

Multiple developers How to merge changes and additions coming from other developers

Manual copy-and-paste for each changed file Cloud storage (eg Dropbox)

How to keep track of whos the author of a change Mmm maybe looking for mails with source code attached

Advanced Operating SystemsGiuseppe Massari

554Why Git

Distributed approach The entire project history database is local

rarr Faster browsing among the changes rarr No traffic generated on the network rarr Changes can be committed locally before sharing rarr Higher reliability Central server DOWN is not an issue

Integrity mechanisms Every change is tracked SHA-1 checksum

40-character string composed of hexadecimal characterscalculated based on the contents of a file or directory structure in GIT

Advanced Operating SystemsGiuseppe Massari

555First steps

Help The command-line usage of Git is based on commands To get help about a specific command syntax

If no command is given an overview of the most common Git commands is returned

$ git help [command]$ git help [command]

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

Advanced Operating SystemsGiuseppe Massari

556First steps

Configuration Setup user name mail and preferences

Configuration ldquolevelsrdquo reference three possible configuration files

First configuration steps

$ git config ltlevelgt ltoptiongt value$ git config ltlevelgt ltoptiongt value

$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit

--system etcgitconfig System wide scope

--global ~gitconfig~configgit

User scope

--local gitconfig Local project scope

Advanced Operating SystemsGiuseppe Massari

557First steps

Basic concepts Repository

Concretely (sub)directory where in Git stores metadata and object database for the project (git)

Working directoryA checkout of one version of the project

The tracked files pulled out of the compressed database in the git directory

Staging area The set of selected changes ready to be committed In practice a file (ldquoINDEXrdquo) contained in git directory storing information about the changes to track in theforthcoming next commit

Workingdirectory

StagingArea

Repository(git directory)

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 3: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

553Why using version control tools

Single developer Do we need to keep track of the history of changes in a software project How to effectively ndash keep track of store ndash such changes

Copying sources into new directories (eg MyProject_v20151006_) What about disk occupancy

Multiple developers How to merge changes and additions coming from other developers

Manual copy-and-paste for each changed file Cloud storage (eg Dropbox)

How to keep track of whos the author of a change Mmm maybe looking for mails with source code attached

Advanced Operating SystemsGiuseppe Massari

554Why Git

Distributed approach The entire project history database is local

rarr Faster browsing among the changes rarr No traffic generated on the network rarr Changes can be committed locally before sharing rarr Higher reliability Central server DOWN is not an issue

Integrity mechanisms Every change is tracked SHA-1 checksum

40-character string composed of hexadecimal characterscalculated based on the contents of a file or directory structure in GIT

Advanced Operating SystemsGiuseppe Massari

555First steps

Help The command-line usage of Git is based on commands To get help about a specific command syntax

If no command is given an overview of the most common Git commands is returned

$ git help [command]$ git help [command]

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

Advanced Operating SystemsGiuseppe Massari

556First steps

Configuration Setup user name mail and preferences

Configuration ldquolevelsrdquo reference three possible configuration files

First configuration steps

$ git config ltlevelgt ltoptiongt value$ git config ltlevelgt ltoptiongt value

$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit

--system etcgitconfig System wide scope

--global ~gitconfig~configgit

User scope

--local gitconfig Local project scope

Advanced Operating SystemsGiuseppe Massari

557First steps

Basic concepts Repository

Concretely (sub)directory where in Git stores metadata and object database for the project (git)

Working directoryA checkout of one version of the project

The tracked files pulled out of the compressed database in the git directory

Staging area The set of selected changes ready to be committed In practice a file (ldquoINDEXrdquo) contained in git directory storing information about the changes to track in theforthcoming next commit

Workingdirectory

StagingArea

Repository(git directory)

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 4: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

554Why Git

Distributed approach The entire project history database is local

rarr Faster browsing among the changes rarr No traffic generated on the network rarr Changes can be committed locally before sharing rarr Higher reliability Central server DOWN is not an issue

Integrity mechanisms Every change is tracked SHA-1 checksum

40-character string composed of hexadecimal characterscalculated based on the contents of a file or directory structure in GIT

Advanced Operating SystemsGiuseppe Massari

555First steps

Help The command-line usage of Git is based on commands To get help about a specific command syntax

If no command is given an overview of the most common Git commands is returned

$ git help [command]$ git help [command]

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

Advanced Operating SystemsGiuseppe Massari

556First steps

Configuration Setup user name mail and preferences

Configuration ldquolevelsrdquo reference three possible configuration files

First configuration steps

$ git config ltlevelgt ltoptiongt value$ git config ltlevelgt ltoptiongt value

$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit

--system etcgitconfig System wide scope

--global ~gitconfig~configgit

User scope

--local gitconfig Local project scope

Advanced Operating SystemsGiuseppe Massari

557First steps

Basic concepts Repository

Concretely (sub)directory where in Git stores metadata and object database for the project (git)

Working directoryA checkout of one version of the project

The tracked files pulled out of the compressed database in the git directory

Staging area The set of selected changes ready to be committed In practice a file (ldquoINDEXrdquo) contained in git directory storing information about the changes to track in theforthcoming next commit

Workingdirectory

StagingArea

Repository(git directory)

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 5: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

555First steps

Help The command-line usage of Git is based on commands To get help about a specific command syntax

If no command is given an overview of the most common Git commands is returned

$ git help [command]$ git help [command]

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

usage git [--version] [--help] [-C ltpathgt] [-c name=value] [--exec-path[=ltpathgt]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=ltpathgt] [--work-tree=ltpathgt] [--namespace=ltnamegt] ltcommandgt [ltargsgt]

The most commonly used git commands are add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List create or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository

Advanced Operating SystemsGiuseppe Massari

556First steps

Configuration Setup user name mail and preferences

Configuration ldquolevelsrdquo reference three possible configuration files

First configuration steps

$ git config ltlevelgt ltoptiongt value$ git config ltlevelgt ltoptiongt value

$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit

--system etcgitconfig System wide scope

--global ~gitconfig~configgit

User scope

--local gitconfig Local project scope

Advanced Operating SystemsGiuseppe Massari

557First steps

Basic concepts Repository

Concretely (sub)directory where in Git stores metadata and object database for the project (git)

Working directoryA checkout of one version of the project

The tracked files pulled out of the compressed database in the git directory

Staging area The set of selected changes ready to be committed In practice a file (ldquoINDEXrdquo) contained in git directory storing information about the changes to track in theforthcoming next commit

Workingdirectory

StagingArea

Repository(git directory)

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 6: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

556First steps

Configuration Setup user name mail and preferences

Configuration ldquolevelsrdquo reference three possible configuration files

First configuration steps

$ git config ltlevelgt ltoptiongt value$ git config ltlevelgt ltoptiongt value

$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit$ git config --global username Giuseppe Massari$ git config --global useremail giuseppemassaripolimiit

--system etcgitconfig System wide scope

--global ~gitconfig~configgit

User scope

--local gitconfig Local project scope

Advanced Operating SystemsGiuseppe Massari

557First steps

Basic concepts Repository

Concretely (sub)directory where in Git stores metadata and object database for the project (git)

Working directoryA checkout of one version of the project

The tracked files pulled out of the compressed database in the git directory

Staging area The set of selected changes ready to be committed In practice a file (ldquoINDEXrdquo) contained in git directory storing information about the changes to track in theforthcoming next commit

Workingdirectory

StagingArea

Repository(git directory)

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 7: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

557First steps

Basic concepts Repository

Concretely (sub)directory where in Git stores metadata and object database for the project (git)

Working directoryA checkout of one version of the project

The tracked files pulled out of the compressed database in the git directory

Staging area The set of selected changes ready to be committed In practice a file (ldquoINDEXrdquo) contained in git directory storing information about the changes to track in theforthcoming next commit

Workingdirectory

StagingArea

Repository(git directory)

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 8: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

558First steps

Basic workflow

Workingdirectory

StagingArea

Repository(git directory)

Checkout a project branch

Stage changes

Commit changes

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 9: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

559Local repository management

Your first Git repository Enter the new project directory Initialize the Git repository

This will create the git directory From now on you can trackchanges in our project committing them in the Git repository

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

$ cd ~DevelopmentMyProject$ git initInitialized empty Git repository in ~DevelopmentMyProjectgit

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

~DevelopmentMyProject git

branches config description HEAD hooks info objects refs

All the ldquomagicsrdquo behind Git is here

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 10: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5510Local repository management

Your first Git repository Create some files and do you first commit

Now we have the very first step of our project history

$ touch maincpp$ git add maincpp$ git commit -s

$ touch maincpp$ git add maincpp$ git commit -s

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

$ git log

commit 8c837e5e223ee55bb9c9d1818fca67b8a365e94aAuthor Giuseppe Massari ltjoemassangagmailcomgtDate Fri Oct 16 135953 2015 +0200

Initial commit Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 11: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5511Local repository management

Diff As we continue introducing changes we can list them for each file

If no files are specified all the changes for each file are shown

$ git diff [file-name]$ git diff [file-name]

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 12: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5512Local repository management

Status After performing some changes we may want to have an overview of the project status

The command status recaps the status of the working directory and the staging area

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

On branch masterChanges to be committed (use git reset HEAD ltfilegt to unstage)

new file mainh

Changes not staged for commit (use git add ltfilegt to update what will be committed) (use git checkout -- ltfilegt to discard changes in working directory)

modified maincpp

Untracked files (use git add ltfilegt to include in what will be committed)

README

$ git status$ git status

Staging area

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 13: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5513Local repository management

Commit What it is

A tracked change in the project new files or changes in already existing files When to do it

Whenever we consider the changes introduced as a ldquostablerdquo part of the project development

What TO commit Whatever can be considered a source file

What TO DO NOT commit Whatever is generated from source files

How to commit add files or changes in files to staging area and then commit

$ git add ltfilesgt$ git commit $ git add ltfilesgt$ git commit

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 14: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5514Local repository management

How to make a ldquogoodrdquo commit Description

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

Model Power-thermal models support The purpose of this support is to providing a well-defined interface to powerthermal resource allocation policy hiding platform specific details A ModelManager allows the registration of platform-specific models according to the target platform selected Such models are implemented as derived classes of the base class Model Signed-off-by Giuseppe Massari ltgiuseppemassaripolimiitgt

A ldquotaglabelrdquo to easily identify the project module touched

A brief title to summarize the changes introduced

Authors signature (git commit -s)

An exhaustive explanation of what has been done and why

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 15: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5515Local repository management

How to make a ldquogoodrdquo commit Include only consistent changes

A single source file can include changes having different goals Bug fixing Code refactoring New functionality implemented

Use git gui to be more productive

Select lines (also from different files) changed for the same single specific purpose

Right-click on changed lines Select ldquoStage lines for commitrdquo

$ git gui$ git gui

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 16: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5516Local repository management

Log To recap the history of the project development

$ git log$ git log

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 7bb21c0837093be82d391ad9d03d5d8235193704Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180625 2015 +0200

[Main] Using a People object Added an instance of People object (joe) walking and greeting Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

commit 5124691db36e150b2839fd923b3ca71186b22fe6Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 180308 2015 +0200

[People] Class first version Including member functions Walk() and Greet() Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 17: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5517Local repository management

Show To show commit details (description + changes)

$ git show [SHA1_number]$ git show [SHA1_number]

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

giuseppeplutone~DevelopmentTestGitTutorial$ git show 5552cefcommit 5552cefd12be253fd10c50cd03b64525eb0217f5Author Giuseppe Massari ltjoemassangagmailcomgtDate Mon Oct 19 172040 2015 +0200

[Main] First implementation The main function now prints a message Signed-off-by Giuseppe Massari ltjoemassangagmailcomgt

diff --git amaincpp bmaincppindex e69de29d9d9396 100644--- amaincpp+++ bmaincpp -00 +19 +include ltiostreamgt++int main(int argc const char argv[])++ stdcout ltlt Hello GIT World ltlt stdendl++ return 0++

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 18: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5518Local repository management

Graphical front-ends

$ gitk --all$ gitk --all

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 19: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5519Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git add

git commit

git init

Do changes

git diffgit statusgit show

Repeat

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 20: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5520Local repository management

Undo commands How to ldquounstagerdquo changes already added to the staging area

$ git checkout filename$ git checkout filename

Workingdirectory

StagingArea

git add file

git checkout file

Just a step back without cleaning the changes in the sources

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 21: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5521Local repository management

Undo commands How to clean out unstaged changes

Clean out all the (unstageduncommited) changes in the tracked files

How to ldquocorrectrdquo a commit

ldquoOpenrdquo the current commit and allows us to addremove fileslines or modify the commit message

It overwrites the commit creating a new one (with a different SHA-1) Not recommended if the commit has been already pushed on the remote repository

Revert the changes of the specified commit Added lines become removed lines and viceversa

$ git commit --amend$ git commit --amend

$ git revert SHA-1_number$ git revert SHA-1_number

$ git reset --hard$ git reset --hard

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 22: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5522Local repository management

Branches In real projects the development history is far from progressing on a straight line

Split developers to work in parallel each focusing on specific features

Software versions targeting different versions following different paths

The concept of branch is all about this

By default the Git repository initializationcreates the master branch

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 23: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5523Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master HEAD HEAD is basically a reference to the topmost commit on the current branch

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 24: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5524Local repository management

Branches Creating (and switching to) a new branch

Initialcommit

d45e26

ee2e90

master

$ git checkout -b dev$ git checkout -b dev

master dev

dev

The new dev branch points to the same topmost master commit

Command checkout action is different in case the argument is a branch instead of a file name

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 25: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5525Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

devc82dda

dev

The dev branch starts ldquogrowingrdquo

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 26: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5526Local repository management

Branches Committing on top of a new branch

Initialcommit

d45e26

ee2e90

master

$ git commit $ git commit

master

c82dda

deva157dc

dev

The project history is slowly starting to show a ldquotreerdquo shape

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 27: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5527Local repository management

Branches Switching back to master branch

Initialcommit

d45e26

ee2e90

master

$ git checkout master$ git checkout master

master

c82dda

deva157dc

dev

Once back to master the changes introduced in the last dev commits apparently ldquodisappearrdquo

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 28: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5528Local repository management

Branches Merging master and dev branches

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

deva157dc

dev

Now if we switch between master and dev we would not see changes in the project

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 29: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5529Local repository management

Branches Labels on commits

Initialcommit

d45e26

ee2e90

master

$ git tag -a v01 a157dc$ git tag -a v01 a157dc

master

c82dda

deva157dc

dev

The tag can be thought as a label to mark our releases and improve the git tree readability

v01

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 30: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5530Local repository management

Conflicts In some cases merging branches may lead to conflicts

Initialcommit

d45e26

ee2e90

master

$ git merge dev$ git merge dev

master

c82dda

dev

a157dc

dev

479bMaster and dev branches include commits that touched the same lines of the same file

CONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the resultCONFLICT (content) Merge conflict in maincppAutomatic merge failed fix conflicts and then commit the result

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 31: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5531Local repository management

Conflicts The status command reports the conflict in this way

To solve the conflict the conflict we need to open the files involved and check the content

Git automatically add ldquospecial linesrdquo into the file to identify the conflict and help us

$ git status dev$ git status dev

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

On branch masterYou have unmerged paths (fix conflicts and run git commit)

Unmerged paths (use git add ltfilegt to mark resolution)

both modified maincpp

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 32: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5532Local repository management

Conflicts maincpp is the file containing the conflict

Lines 7-9 HEAD status changes in commit on top of current branch (master) Lines 9-10 changes coming from the merging branch (dev)

Resolution Make a choice about the lines to keep or deleted remove Delete special lines ldquoltltltlt===gtgtgtrdquo Add the files to the staging area (INDEX) Commit

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

7 ltltltltltltlt HEAD 8 stdcout ltlt Hello walking man ltlt stdendl 9 =======10 gtgtgtgtgtgtgt dev

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 33: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5533Local repository management

Rebasing Move a branch on top of another

May lead to conflicts Do it as long as your are moving a local branch

$ git rebase master dev$ git rebase master dev

d45e26

ee2e90masterc82dda

a157dc

d45e26

ee2e90

c82dda

dev a157dc

master

dev

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 34: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5534Local repository management

Stashing Temporarily hide current (uncommited) changes on a ldquostackrdquo

Useful in case of switching on a branch already including changes on files we currently working on

I can stack multiple stashed changes

To discard stashed changes

To resume the stashed changes

$ git stash save ldquoChanges about feature1 implementationrdquo$ git stash save ldquoChanges about feature1 implementationrdquo

$ git stash list$ git stash list

$ git stash drop stashN$ git stash drop stashN

$ git stash pop [stashN]$ git stash pop [stashN]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 35: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5535Local repository management

Basic work-flow in commands

Workingdirectory

StagingArea

Repository(git directory)

git checkout ltbranch_namegt

git add

git commit

git init

Do changesgit diffgit statusgit show

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 36: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5536Local repository management

Work-flow example To manage the software release cycles we can design a work-flow by splitting bug fixing feature development and release-candidate versions in dedicated branches

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 37: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5537Outline

Why using version control tools Why Git

First steps Getting help and configuration Basic concepts

Local repository management Initialization Change management Branches Conflicts management

Shared remote repository management Cooperative development

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 38: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5538Remote repository management

Cooperative development Several developers working on the same project

Joe Simon Fred

Project

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 39: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5539Remote repository management

Remote repository work-flow

Workingdirectory

StagingArea

Local repository

git checkout ltbranch_namegt

git add

git commit

Remote repository

git clone

git fetchpull

git push

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 40: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5540Remote repository management

Basic commands To clone a project (from a remote repository)

To download all the changes from the remote repository New commits new branches new tags

To download changes and merge it in the current local branch Fetch + merge

To upload local changes to remote repository

$ git clone repository_path$ git clone repository_path

$ git fetch repository_name$ git fetch repository_name

$ git pull repository_name [branch_name]$ git pull repository_name [branch_name]

$ git push repository_name [branch_name]$ git push repository_name [branch_name]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 41: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5541Remote repository management

Cooperative development

Joe Simon Fred

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 42: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5542Remote repository management

Cooperative development Joe starts a project tracking changes on a local repository

Joe Simon Fred

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

[Joe]$ git init[Joe]$ git add hellip[Joe]$ git commit hellip

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 43: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5543Remote repository management

Cooperative development Joe adds a remote repository to the project

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git remote add origin gitmyserverorgjoemyprojgit[Joe]$ git remote add origin gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 44: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5544Remote repository management

Cooperative development Joe shares its work by uploading its current git tree to the remote repository Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push -u --all origin[Joe]$ git push -u --all origin

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 45: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5545Remote repository management

Cooperative development Simon and Fred clone the remote git repository so that they can contribute to the project Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit[Simon]$ git clone gitmyserverorgjoemyprojgit[Fred]$ git clone gitmyserverorgjoemyprojgit

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 46: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5546Remote repository management

Cooperative development Simon fix bugs committing changes on a new branch (hotfix)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Simon]$ git checkout -b hotfix[Simon]$ git commit [Simon]$ git checkout -b hotfix[Simon]$ git commit

HEAD

HEADHEAD

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 47: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5547Remote repository management

Cooperative development Fred implements a new feature committing changes on a new branch (feature1) Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git checkout -b feature1[Fred]$ git commit [Fred]$ git checkout -b feature1[Fred]$ git commit

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 48: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5548Remote repository management

Cooperative development Fred and Simon upload their changes (branches)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git push origin feature1[Simon]$ git push origin hotfix[Fred]$ git push origin feature1[Simon]$ git push origin hotfix

HEAD

HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 49: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5549Remote repository management

Cooperative development Joe updates its local repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git fetch origin[Joe]$ git fetch origin

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 50: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5550Remote repository management

Cooperative development Joe merge the contributions in the project ldquomain linerdquo (ie master)

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix[Joe]$ git merge originfeature1[Joe]$ git merge originhotfix

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 51: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5551Remote repository management

Cooperative development Joe uploads the (updated) master branch to the remote repository

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Joe]$ git push origin master[Joe]$ git push origin master

HEADHEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 52: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5552Remote repository management

Cooperative development Simon and Fred synchronize their local repository with the remote

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[Fred]$ git fetch origin [Simon]$ git fetch origin[Fred]$ git fetch origin [Simon]$ git fetch origin

HEAD HEAD

HEAD

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 53: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5553Remote repository management

Cooperative development Simon and Fred get the updated master

Git server

Joe Simon Fred

gitmyserverorgjoemyprojgit

(origin)

[FredSimon]$ git checkout master [FredSimon]$ git pull origin master[FredSimon]$ git checkout master [FredSimon]$ git pull origin master

HEAD HEAD HEAD

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 54: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5554Extras

Cherry-pick How to import a single commit from a branch

Example a bugfix patch has been committed on a development branch

d45e26

ee2e90masterc82dda

a157dcbugfixes

d45e26

ee2e90

master

c82dda

a157dcc82dda

bugfixes

$ git cherry-pick ltcommit-SHA1gt$ git cherry-pick ltcommit-SHA1gt

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit

Page 55: Version Control with Git - Politecnico di Milanohome.deib.polimi.it/...media=teaching:aos:2017:aos_l2_version_control_with_git.pdf · [--git-dir=] [--work-tree=]

Advanced Operating SystemsGiuseppe Massari

5555Resources

httpsgit-scmcom httpswwwatlassiancomgit httpwwwtutorialspointcomgit