11 Version control part 2 Hodson 2 Chacon

  • Slides: 43
Download presentation
11 Version control (part 2) (Hodson: 2 -, Chacon: 3, 4. 10, 5)

11 Version control (part 2) (Hodson: 2 -, Chacon: 3, 4. 10, 5)

Main concepts to be covered • • • The git repository architecture. Stage, commit

Main concepts to be covered • • • The git repository architecture. Stage, commit and undo. Branching, merging and rebasing. Local and remote repositories. Workflow models. Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 2

Safety warning When You are learning GIT always keep safety backups of your code!

Safety warning When You are learning GIT always keep safety backups of your code! Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 3

Git overview Here you work with your code using your favourite tools Stores the

Git overview Here you work with your code using your favourite tools Stores the revision history of your project Repo Stage Staging Area (index) Commit History Working directory Here you prepare your commits Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 4

The three main parts of Git • The repository (git directory) contains metadata and

The three main parts of Git • The repository (git directory) contains metadata and object database. • The working directory is a working copy of one version of the project files. • The staging area (index) is a file describing the contents of the next commit. Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 5

File status in the working directory tracked add the file untracked remove the file

File status in the working directory tracked add the file untracked remove the file edit the file unmodified/ committed modified stage the file commit Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers staged 6

Git snapshots Committed snapshot Files in the snapshot Objektorienterade applikationer, DAT 055, DAI 2,

Git snapshots Committed snapshot Files in the snapshot Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 7

Staging files • Collect related changes in the working directory and prepare them for

Staging files • Collect related changes in the working directory and prepare them for commit > git add <file> Staged Snapshot Working directory Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 8

Committing a snapshot • A commit is a saved version of the project. >

Committing a snapshot • A commit is a saved version of the project. > git commit Repo Staged Snapshot Commit History • A commit is identified by a unique checksum of its contents. Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 9

Commit contents • A commit contains – – – A snapshot of the project

Commit contents • A commit contains – – – A snapshot of the project A checksum of the contents User information The date A short description provided by the user Commit c 8 e 7 c 491 af 652 b 9 c 73 d Author: lisa <lisa@foo. com> Date: Thu Feb 12 15: 31: 22 2015 <commit message> Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 10

Branches, master and HEAD • A branch is a pointer to a commit. •

Branches, master and HEAD • A branch is a pointer to a commit. • The default branch is called master. • The pointer moves forward with each commit. master Commit Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 11

Branches, master and HEAD • A new branch is created by > git branch

Branches, master and HEAD • A new branch is created by > git branch <name> Ex: > git branch my-branch head master head keeps track of the current branch my-branch Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 12

Checking out a branch • Switch to a branch with checkout: > git checkout

Checking out a branch • Switch to a branch with checkout: > git checkout <name> Ex: > git checkout my-branch master my-branch This is now the current branch head Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 13

Splitting the history • Commit to the new branch > git commit master my-branch

Splitting the history • Commit to the new branch > git commit master my-branch head Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 14

Splitting the history • Do some work on the master branch > git checkout

Splitting the history • Do some work on the master branch > git checkout master > git commit head master Commit my-branch Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 15

Undoing in working directory • Restore the working directory and stage to match the

Undoing in working directory • Restore the working directory and stage to match the most recent commit (HEAD): Undo changes in tracked files > git reset –-hard HEAD > git clean -f Remove untracked files Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 16

Undoing in the staging area • Restore a file in the stage to match

Undoing in the staging area • Restore a file in the stage to match the most recent commit (HEAD): > git reset HEAD <file> This does not affect the file in the working directory. Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 17

Undoing commits • Reset moves HEAD backwards in history: > git reset HEAD~1 One

Undoing commits • Reset moves HEAD backwards in history: > git reset HEAD~1 One step before HEAD Forget last commit Only use reset on local repositories - never on public ones! Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 18

Undoing commits • Revert adds a new commit that “neutralizes” the changes in the

Undoing commits • Revert adds a new commit that “neutralizes” the changes in the specified commit: > git revert <commit-id> - + Neutralizing commit Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 19

Undoing commits • Amend replaces the most recent commit: > git commit --amend Ooops!

Undoing commits • Amend replaces the most recent commit: > git commit --amend Ooops! Forgot a file in the last commit Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 20

Master and topic branches • Use the master branch for stable code. • Develop

Master and topic branches • Use the master branch for stable code. • Develop new features in topic branches. • Only commit to topic branches, not to the master branch. • Integrate successful topic branches into master. Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 21

Fast-forward merge master some-feature Topic branch > git checkout master > git merge some-feature

Fast-forward merge master some-feature Topic branch > git checkout master > git merge some-feature Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 22

3 -way merge master some-feature > git checkout master > git merge some-feature Objektorienterade

3 -way merge master some-feature > git checkout master > git merge some-feature Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 23

Rebasing makes history linear Old base master 1 some-feature 2 some-feature > > git

Rebasing makes history linear Old base master 1 some-feature 2 some-feature > > git git rebase master some-feature checkout master merge some-feature #ff branch –d some-feature master (1) (2) (3) Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers some-feature (3) 24

Remote repositories • A remote branch is a copy of a branch in some

Remote repositories • A remote branch is a copy of a branch in some developer’s repository. Local branch master Remote repo Remote branch > git fetch <remote> <branch> Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 25

Remote repositories • Cloning a remote repository creates a local copy into the current

Remote repositories • Cloning a remote repository creates a local copy into the current directory. > git clone <path-to-repo> Ex. > git clone https: //github. com/friend/repo. git • A remote is an alias for a remote repository > git remote add <name> <path-to-repo> Ex. > git remote add other https: //github. com/friend/repo. git # other can now be used as a ”bookmark” to friend’s repo. # (btw, this is a git comment!) Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 26

What does “origin” mean? • When a remote repository is cloned, origin is by

What does “origin” mean? • When a remote repository is cloned, origin is by convention the local default alias for the remote repository. • origin/master is a local copy of the master branch at the remote repository. • The command git push origin master updates the remote master branch with the local master branch. (for example at Git. Hub). Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 27

origin master Remote repo master Current state of remote master branch Local repo clone

origin master Remote repo master Current state of remote master branch Local repo clone origin/master Local copy Old state of remote master branch master Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers Current state of local master branch 28

Merging a remote branch • To use a remote branch it has to be

Merging a remote branch • To use a remote branch it has to be integrated in your own repository by merge or rebase. > git checkout master > git fetch origin > git merge origin/master Merge commit merge Remote branch Local branch master Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers master 29

Rebasing onto a remote branch • Integrating a remote branch with rebasing: > git

Rebasing onto a remote branch • Integrating a remote branch with rebasing: > git checkout master > git fetch origin > git rebase origin/master master Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 30

Push • push exports a branch to a remote repository. > git push <remote>

Push • push exports a branch to a remote repository. > git push <remote> <branch> origin/master Use push to upload changes to your own public repository. master Ex. > git master checkout master fetch origin merge origin/master push origin master Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers Allways fetch the remote branch and merge or rebase with your local branch before pushing! 31

Pull • A pull is a combined fetch of the remote master branch followed

Pull • A pull is a combined fetch of the remote master branch followed by a merge with the current branch: > git pull • Pull can be used with the --rebase option to perform a rebase instead of a merge. Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 32

Distributed workflows • Centralized workflow • Integration-manager workflow • Exercise (last page) Objektorienterade applikationer,

Distributed workflows • Centralized workflow • Integration-manager workflow • Exercise (last page) Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 33

Distributed workflows 1. Develop some new feature. 2. Rebase it onto your local master

Distributed workflows 1. Develop some new feature. 2. Rebase it onto your local master branch. 3. Push master to your public repository. Never rebase onto public branches! Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 34

Centralized Workflow Public version Public repo clone/pull push Developer Local repo Objektorienterade applikationer, DAT

Centralized Workflow Public version Public repo clone/pull push Developer Local repo Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 35

Integrator-Manager Workflow Master repo Public repo 1 Public repo 3 Public repo 2 2.

Integrator-Manager Workflow Master repo Public repo 1 Public repo 3 Public repo 2 2. clone 3. push Integration manager in s ll on pu uti 4. rib nt 5. push co 1. push Developer 1: Feature A Developer 2: Main Developer 3: Feature B Main A B Local repo 1 Local repo 2 Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers Local repo 3 36

Exercise (see separate PM) local repo 1 remote repo 2 clone (edit; add; commit

Exercise (see separate PM) local repo 1 remote repo 2 clone (edit; add; commit )* * merge or rebase push local repo 2 clone push fetch (edit; add; commit )* * fetch push Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers merge or rebase 37

Exploring version differences public class Prime. Generator implements Generator<Integer> { private int current. Prime

Exploring version differences public class Prime. Generator implements Generator<Integer> { private int current. Prime = 1; public Integer next() { return current. Prime; } private boolean is. Prime(int n) { for ( int f = 2; f < (int)Math. sqrt(n); f++) if ( n % f == 0 ) return false; return true; private boolean is. Prime(int n) { } int factor = 2; } while ( factor <= (int)Math. sqrt(n) ) { if ( n % factor == 0 ) Suppose is. Prime return false; above is changed factor++; to this version } return true; } Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 38

Exploring version differences Lines 18 -19 are replaced by old line 20 is now

Exploring version differences Lines 18 -19 are replaced by old line 20 is now 21 Some new lines added Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 39

Difference visualization in the Smart. Git tool Lines 18 -19 Objektorienterade applikationer, DAT 055,

Difference visualization in the Smart. Git tool Lines 18 -19 Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 40

Difference visualization in the Smart. Git tool Objektorienterade applikationer, DAT 055, DAI 2, 18/19,

Difference visualization in the Smart. Git tool Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 41

Difference visualization in the Smart. Git tool Objektorienterade applikationer, DAT 055, DAI 2, 18/19,

Difference visualization in the Smart. Git tool Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 42

A branching model • An advanced, but realistic, branching model is described in: http:

A branching model • An advanced, but realistic, branching model is described in: http: //nvie. com/posts/a-successful-git-branching-model/ Objektorienterade applikationer, DAT 055, DAI 2, 18/19, lp 3. (c)2019 Uno Holmer, DIT, Chalmers 43