Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Version control (part 2)

Similar presentations


Presentation on theme: "11 Version control (part 2)"— Presentation transcript:

1 11 Version control (part 2)
Objektorienterade applikationer d2, förel. 11 11 Version control (part 2) (Hodson: 2-, Chacon: 3, 4.10, 5) DAT055, 16/17, lp 3

2 Main concepts to be covered
Objektorienterade applikationer d2, förel. 11 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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

3 Objektorienterade applikationer d2, förel. 11
Safety warning When You are learning GIT always keep safety backups of your code! Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

4 Objektorienterade applikationer d2, förel. 11
Git overview Here you work with your code using your favourite tools Stores the revision history of your project Repo Working directory Staging Area (index) Stage Commit History Here you prepare your commits Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

5 The three main parts of Git
Objektorienterade applikationer d2, förel. 11 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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

6 File status in the working directory
Objektorienterade applikationer d2, förel. 11 File status in the working directory tracked add the file modified edit the file unmodified/ committed untracked stage the file commit remove the file staged Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

7 Objektorienterade applikationer d2, förel. 11
Git snapshots Committed snapshot Files in the snapshot Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

8 Objektorienterade applikationer d2, förel. 11
Staging files Collect related changes in the working directory and prepare them for commit > git add <file> Working directory Staged Snapshot Stage Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

9 Objektorienterade applikationer d2, förel. 11
Committing a snapshot A commit is a saved version of the project. > git commit A commit is identified by a unique checksum of its contents. Repo Staged Snapshot Commit History Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

10 Objektorienterade applikationer d2, förel. 11
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 c8e7c491af652b9c73d Author: lisa Date: Thu Feb 12 15:31: <commit message> Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

11 Branches, master and HEAD
Objektorienterade applikationer d2, förel. 11 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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

12 Branches, master and HEAD
Objektorienterade applikationer d2, förel. 11 Branches, master and HEAD A new branch is created by > git branch <name> Ex: > git branch my-branch head head keeps track of the current branch master my-branch Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

13 Objektorienterade applikationer d2, förel. 11
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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

14 Objektorienterade applikationer d2, förel. 11
Splitting the history Commit to the new branch > git commit master my-branch head Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

15 Objektorienterade applikationer d2, förel. 11
Splitting the history Do some work on the master branch > git checkout master > git commit head head master master Commit my-branch my-branch Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

16 Undoing in working directory
Objektorienterade applikationer d2, förel. 11 Undoing in working directory Restore the working directory and stage to match the most recent commit (HEAD): > git reset –-hard HEAD > git clean -f Undo changes in tracked files Remove untracked files Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

17 Undoing in the staging area
Objektorienterade applikationer d2, förel. 11 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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

18 Objektorienterade applikationer d2, förel. 11
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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

19 Objektorienterade applikationer d2, förel. 11
Undoing commits Revert adds a new commit that “neutralizes” the changes in the specified commit: > git revert <commit-id> + - Neutralizing commit Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

20 Objektorienterade applikationer d2, förel. 11
Undoing commits Amend replaces the most recent commit: > git commit --amend Ooops! Forgot a file in the last commit Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

21 Master and topic branches
Objektorienterade applikationer d2, förel. 11 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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

22 Objektorienterade applikationer d2, förel. 11
Fast-forward merge master master some-feature some-feature Topic branch > git checkout master > git merge some-feature Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

23 Objektorienterade applikationer d2, förel. 11
3-way merge master master some-feature some-feature > git checkout master > git merge some-feature Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

24 Rebasing makes history linear
Objektorienterade applikationer d2, förel. 11 Rebasing makes history linear Old base Old base master master 1 some-feature 2 some-feature master > git rebase master some-feature (1) > git checkout master (2) > git merge some-feature #ff (2) > git branch –d some-feature (3) some-feature (3) Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

25 Objektorienterade applikationer d2, förel. 11
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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

26 Objektorienterade applikationer d2, förel. 11
Remote repositories Cloning a remote repository creates a local copy into the current directory. > git clone <path-to-repo> Ex. > git clone A remote is an alias for a remote repository > git remote add <name> <path-to-repo> Ex. > git remote add other # other can now be used as a ”bookmark” to friend’s repo. # (btw, this is a git comment!) Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

27 What does “origin” mean?
Objektorienterade applikationer d2, förel. 11 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 GitHub). Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

28 Objektorienterade applikationer d2, förel. 11
origin Remote repo master master Current state of remote master branch clone origin Old state of remote master branch Local repo origin/master Current state of local master branch master Local copy Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

29 Merging a remote branch
Objektorienterade applikationer d2, förel. 11 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 origin/master origin/master origin/master Merge commit merge Remote branch Local branch master master Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

30 Rebasing onto a remote branch
Objektorienterade applikationer d2, förel. 11 Rebasing onto a remote branch Integrating a remote branch with rebasing: > git checkout master > git fetch origin > git rebase origin/master origin/master origin/master master master Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

31 Objektorienterade applikationer d2, förel. 11
Push push exports a branch to a remote repository. > git push <remote> <branch> origin/master origin/master Use push to upload changes to your own public repository. Allways fetch the remote branch and merge or rebase with your local branch before pushing! master master Ex. > git checkout master > git fetch origin > git merge origin/master > git push origin master Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

32 Objektorienterade applikationer d2, förel. 11
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, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

33 Distributed workflows
Objektorienterade applikationer d2, förel. 11 Distributed workflows Centralized workflow Integration-manager workflow Exercise (last page) Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

34 Distributed workflows
Objektorienterade applikationer d2, förel. 11 Distributed workflows Develop some new feature. Rebase it onto your local master branch. Push master to your public repository. Never rebase onto public branches! Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

35 Objektorienterade applikationer d2, förel. 11
Centralized Workflow Public version Public repo clone/pull push Developer Developer Local repo Developer Local repo Local repo Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

36 Integrator-Manager Workflow
Objektorienterade applikationer d2, förel. 11 Integrator-Manager Workflow Master repo Public repo 1 Public repo 2 Public repo 3 2. clone 3. push 1. push 5. push 4. pull in contributions Main Developer 2: Main Local repo 2 B Developer 3: Feature B Local repo 3 Integration manager Local repo 1 A Developer 1: Feature A Local repo Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

37 Exercise (see separate PM)
Objektorienterade applikationer d2, förel. 11 Exercise (see separate PM) local repo 1 remote repo 1 remote repo 2 local repo 2 clone push (edit; add; commit)* fetch merge or rebase * Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

38 Exploring version differences
Objektorienterade applikationer d2, förel. 11 Exploring version differences public class PrimeGenerator implements Generator<Integer> { private int currentPrime = 1; public Integer next() { return currentPrime; } private boolean isPrime(int n) { for ( int f = 2; f < (int)Math.sqrt(n); f++) if ( n % f == 0 ) return false; return true; private boolean isPrime(int n) { int factor = 2; while ( factor <= (int)Math.sqrt(n) ) { if ( n % factor == 0 ) return false; factor++; } return true; Suppose isPrime above is changed to this version Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

39 Exploring version differences
Objektorienterade applikationer d2, förel. 11 Exploring version differences Lines 18-19 are replaced by old line 20 is now 21 Some new lines added Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

40 Difference visualization in the SmartGit tool
Objektorienterade applikationer d2, förel. 11 Difference visualization in the SmartGit tool Lines 18-19 Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

41 Difference visualization in the SmartGit tool
Objektorienterade applikationer d2, förel. 11 Difference visualization in the SmartGit tool Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

42 Difference visualization in the SmartGit tool
Objektorienterade applikationer d2, förel. 11 Difference visualization in the SmartGit tool Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3

43 Objektorienterade applikationer d2, förel. 11
A branching model An advanced, but realistic, branching model is described in: Objektorienterade applikationer, DAT055, DAI2, 16/17, lp 3. (c)2017 Uno Holmer, DIT, Chalmers DAT055, 16/17, lp 3


Download ppt "11 Version control (part 2)"

Similar presentations


Ads by Google