CS 241: Systems Programming Lecture 34. Advanced Git Fall 2019 - - PowerPoint PPT Presentation

cs 241 systems programming lecture 34 advanced git
SMART_READER_LITE
LIVE PREVIEW

CS 241: Systems Programming Lecture 34. Advanced Git Fall 2019 - - PowerPoint PPT Presentation

CS 241: Systems Programming Lecture 34. Advanced Git Fall 2019 Prof. Stephen Checkoway 1 Using "branches" Development and release versions Trying out new features Focusing on fixing a bug Simpler to do in Git than other VCS, consider


slide-1
SLIDE 1

CS 241: Systems Programming Lecture 34. Advanced Git

Fall 2019

  • Prof. Stephen Checkoway

1

slide-2
SLIDE 2

Using "branches"

Development and release versions Trying out new features Focusing on fixing a bug Simpler to do in Git than other VCS, consider using more frequently

2

slide-3
SLIDE 3

Branches

Visualize a project’s development as a “linked list” of commits. When a development track splits, a new branch is created. In Git, branches are actually just a pointer to these commits

3

slide-4
SLIDE 4

Git branching

List all branches in the project

  • git branch

Create a new branch

  • git branch <branchname>

Switch to a branch

  • git checkout <branchname>

Create and immediately switch

  • git checkout –b <branchname>

Delete a branch

  • git branch –d <branchname>

4

slide-5
SLIDE 5

Using branches

Create and switch to a branch

5

$ git branch working $ git checkout working M README Switched to branch 'working' $ git branch master * working

slide-6
SLIDE 6

Stashing

Working tree should be clean when switching branches Save/hide changes you don't want to commit with git stash

  • Pushes changes onto a stash stack

Recover changes lager with git stash pop

6

slide-7
SLIDE 7

Using branches

7

slide-8
SLIDE 8

Using branches

Integrate changes back into master

8

$ git checkout master Switched to branch 'master' $ git merge working Merge made by the 'recursive' strategy. newfile.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 newfile.txt

slide-9
SLIDE 9

Before git merge

9

slide-10
SLIDE 10

After git merge

10

slide-11
SLIDE 11

Merged history

11

* cdd07b2 - (HEAD, master) Merge branch 'working' |\ | * 1ccf9e7 - (working) Added a new file * | 3637a76 - Second change * | cf98d00 - First change |/ * cf31a23 - Updated README to 2.0 * 2a8fc15 - Initial commit

slide-12
SLIDE 12

Rebasing

Like merging, rebasing transfers changes from one branch to another Does not create a new commit Replays changes from current branch onto head of other branch

12

slide-13
SLIDE 13

Before git rebase

13

slide-14
SLIDE 14

After git rebase

14

’ ’ ’

slide-15
SLIDE 15

git rebase

Powerful tool Can change the commit order Merge/split commits Make fixes in earlier commits

  • DO NOT DO ON PUSHED CHANGES OR PUBLIC BRANCH

15

$ git rebase –i master

slide-16
SLIDE 16

Conflicts

16

slide-17
SLIDE 17

Git conflict markers

17

$ cat foo.c <<<<<<< HEAD current content ======= branch content >>>>>>> newbranch $ vim foo.c $ git add foo.c $ git rebase --continue

slide-18
SLIDE 18

Pull requests with Github

Contributing changes to repositories on Github Requests the owner of the code integrate your changes

18

slide-19
SLIDE 19

Setup

GitHub

19

slide-20
SLIDE 20

Setup

upstream
 (theirs)

20

slide-21
SLIDE 21

Setup

upstream
 (theirs) fork

21

slide-22
SLIDE 22

Setup

upstream
 (theirs)

  • rigin


(yours) fork

22

slide-23
SLIDE 23

Setup

upstream
 (theirs)

  • rigin


(yours) clone fork

23

slide-24
SLIDE 24

Setup

upstream
 (theirs)

  • rigin


(yours) local
 (yours) clone fork

24

slide-25
SLIDE 25

Setup

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

upstream

clone fork

25

slide-26
SLIDE 26

Setup

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream

26

slide-27
SLIDE 27

Contribute Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream

27

slide-28
SLIDE 28

Contribute Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream push

28

slide-29
SLIDE 29

Contribute Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream push

29

slide-30
SLIDE 30

Contribute Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream push pull request

30

slide-31
SLIDE 31

Contribute Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream push pull request

31

slide-32
SLIDE 32

Integrate Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream

32

slide-33
SLIDE 33

Integrate Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream

fetch

33

slide-34
SLIDE 34

Integrate Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream

fetch

34

slide-35
SLIDE 35

Integrate Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream

push

fetch

35

slide-36
SLIDE 36

Integrate Changes

upstream
 (theirs)

  • rigin


(yours) local
 (yours)

  • rigin

upstream

push

fetch

36

slide-37
SLIDE 37

You want to contribute code to the Github project fancy/project (fancy is the name of the owner, project is the name of the repo). You fork the repo (producing student/project), commit your changes, and push to student/project. Next, you make a pull request for fancy/project. Which statement is true?

  • A. Your code is now integrated into fancy/project via merging
  • B. Your code is now integrated into fancy/project via rebasing
  • C. You have requested that your code be integrated into fancy/project,

but no changes have been made

  • D. You cannot make any additional commits until the pull request has been

accepted

37

slide-38
SLIDE 38
  • rigin

upstream local master master master

Branches

38

slide-39
SLIDE 39
  • rigin

upstream local master master master

$ git checkout -b feature

feature

39

slide-40
SLIDE 40
  • rigin

upstream local master master master

$ git commit

feature

40

slide-41
SLIDE 41
  • rigin

upstream local master master master

$ git push -u origin feature

feature feature

41

slide-42
SLIDE 42
  • rigin

upstream local master master master feature feature

pull request

42

slide-43
SLIDE 43
  • rigin

upstream local master master master

Great idea, now can you do it more like this?

feature feature

pull request

43

slide-44
SLIDE 44
  • rigin

upstream local master master master

$ git commit $ git push

feature feature

pull request

44

slide-45
SLIDE 45
  • rigin

upstream local master master master

Awesome, but please update with new changes in master

feature feature

pull request

45

slide-46
SLIDE 46
  • rigin

upstream local master master master

$ git remote add upstream https://github.com/… $ git fetch upstream master:master

feature feature

pull request

46

slide-47
SLIDE 47
  • rigin

upstream local master master master

$ git rebase master

feature feature

pull request WARNING:
 You may have
 to resolve conflicts.

47

slide-48
SLIDE 48
  • rigin

upstream local master master master

$ git rebase master

feature feature

pull request

48

slide-49
SLIDE 49
  • rigin

upstream local master master

$ git push -f origin master feature

feature

pull request

master feature

49

slide-50
SLIDE 50
  • rigin

upstream local master master feature

pull request

master feature

  • Great. Please squash your commits.

50

slide-51
SLIDE 51
  • rigin

upstream local master master

$ git rebase –i master

feature

pull request

master feature

51

slide-52
SLIDE 52
  • rigin

upstream local master master

$ git rebase –i master

feature

pull request

master feature

52

slide-53
SLIDE 53
  • rigin

upstream local master master

$ git rebase –i master

feature

pull request

master feature

53

slide-54
SLIDE 54
  • rigin

upstream local master master

$ git push -f origin feature

feature

pull request

master feature

54

slide-55
SLIDE 55
  • rigin

upstream local master master feature

pull request

master feature

Perfect, I accept!

55

slide-56
SLIDE 56
  • rigin

upstream local master master

Time to Clean Up

feature master feature

56

slide-57
SLIDE 57
  • rigin

upstream local master master

I accept!

feature master feature

$ git fetch upstream master:master

57

slide-58
SLIDE 58
  • rigin

upstream local master master

I accept!

feature master feature

$ git push origin master

58

slide-59
SLIDE 59
  • rigin

upstream local master master

I accept!

master feature

$ git checkout master
 $ git branch -d feature

59

slide-60
SLIDE 60
  • rigin

upstream local master master

I accept!

master

$ git push origin -d feature

60

slide-61
SLIDE 61

After a PR is accepted, Github will ask you if you want to delete your feature

  • branch. If you say yes, which branches get deleted?
  • A. feature — the branch named feature in your local repo
  • B. origin/feature — the branch named feature in your remote repo
  • C. upstream/feature — the branch named feature in their remote repo
  • D. feature and origin/feature
  • E. feature, origin/feature, and upstream/feature

61

slide-62
SLIDE 62

Now that origin/feature has been deleted, how do you delete feature?

  • A. $ git delete feature
  • B. $ git delete -b feature
  • C. $ git branch -d feature
  • D. $ git push origin -d feature
  • E. I would google "delete a git branch" and then click on https://

stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch- locally-and-remotely like every other programmer

62

slide-63
SLIDE 63

In-class exercise

https://checkoway.net/teaching/cs241/2019-fall/exercises/Lecture-34.html Grab a laptop and a partner and try to get as much of that done as you can!

63