1 of 28

CSE 391

Git branches, merging

Remote repositories

Slides created by Josh Ervin and Hunter Schafer. �Based off slides made by Marty Stepp, Jessica Miller, Ruth Anderson, Brett Wortzman, and Zorah Fung

2 of 28

AGENDA

  • Logistics, be sure to check your homework scores
  • More git
    • Branching
    • Merging
    • Working with remote repositories

3 of 28

ROADMAP

  • Introduction to the command line
  • Input/output redirection, pipes
  • More input/output redirection, tee, xargs
  • Git: Fundamentals
  • Git: Branches, merging, and remote repositories
  • Regular expressions
  • More regular expressions, sed
  • Users and permissions
  • Bash scripting
  • Industry applications

4 of 28

REVIEW: FOUR PHASES

Working Directory

Staging Area/Index

Local Repository

Remote Repository

Working changes

Changes you’re preparing to commit

Local copy of the repository with your committed changes

Remote shared repository (Usually stored with a platform like GitHub/Gitlab)

5 of 28

GIT: BRANCHING

  • So far, all the operations we’ve done have been on the master branch.
  • However, it’s very rare you’ll be working on master directly. Instead, you’ll work on a separate branch:
    • Master is the “single source of truth” - the history of the project
    • The code on master should be stable and compile
    • Because of this, it’s difficult to share and collaborate on in progress work.
    • Working directly on master makes it difficult to work simultaneously on unrelated features.

NOTE: On most newer repos, master has been renamed to main. In practice you will see both names used in documentation and repos.

6 of 28

COMMIT HISTORY

A

B

C

These are commits. “A” is the first commit, “C” is the most recent

Each commit references it’s parent - the one that came before it in time.

master

HEAD

7 of 28

COMMIT HISTORY

A

B

C

master

HEAD

  • HEAD: This is a pointer, or reference, to the git branch that you are currently working on.
  • Master: This the main branch of your repository, the single “source of truth”, and the only branch created by default.
  • NOTE: The pointer from one commit to another points to the previous commit, so the arrows go back in time.

8 of 28

COMMIT HISTORY

A

B

C

master

HEAD

To view this same information on the command line:

$ git log

Or, to make it more similar to what we see here:

$ git log --graph --oneline

9 of 28

10 of 28

BRANCHING

A

B

C

master

HEAD

Commands that have been run:

$ git branch feature

$ git checkout feature

feature

11 of 28

BRANCHING

A

B

C

master

HEAD

Commands that have been run:

$ git branch feature

$ git checkout feature

$ echo “D” >> file.txt

$ git add file.txt

$ git commit -m “D”

feature

D

12 of 28

BRANCHING

A

B

C

master

Commands that have been run:

$ git branch feature

$ git checkout feature

$ echo “D” >> file.txt

$ git add file.txt

$ git commit -m “D”

$ git checkout master

$ echo “E” >> file.txt

$ git add file.txt

$ git commit -m “E”

feature

D

E

HEAD

13 of 28

BRANCHING

A

B

C

master

Commands that have been run:

$ git branch feature

$ git checkout feature

$ echo “D” >> file.txt

$ git add file.txt

$ git commit -m “D”

$ git checkout master

$ echo “E” >> file.txt

$ git add file.txt

$ git commit -m “E”

$ git merge feature

feature

D

E

M

HEAD

14 of 28

WORKING WITH REMOTE

A

B

master

Local

Remote

A

B

origin/master

master

HEAD

15 of 28

WORKING WITH REMOTE

A

B

master

Local

Remote

A

B

origin/master

C

master

HEAD

16 of 28

WORKING WITH REMOTE

A

B

master

Local

Remote

D

A

B

origin/master

C

master

HEAD

17 of 28

WORKING WITH REMOTE

A

B

master

Local

Remote

D

git push

A

B

origin/master

C

master

HEAD

18 of 28

WORKING WITH REMOTE

A

B

master

Local

Remote

D

git push

A

B

origin/master

C

master

HEAD

19 of 28

WORKING WITH REMOTE

A

B

A

B

master

Local

Remote

origin/master

C

master

HEAD

D

git fetch

D

20 of 28

WORKING WITH REMOTE

A

B

A

B

master

Local

Remote

origin/master

C

master

HEAD

D

D

git merge

M

Note: It’s more common to do git pull which is an alias for git fetch + get merge

21 of 28

WORKING WITH REMOTE

A

B

Local

Remote

origin/master

C

master

HEAD

D

M

git push

A

B

C

master

D

M

22 of 28

PULL REQUESTS

  • In practice, it’s very rare that we would merge branches locally and push them to remote.
  • Instead, we use a service like GitHub or GitLab to help us:
    • Step 1: Create a local branch and make some commits
    • Step 2: Push those commits to remote
    • Step 3: Open a pull/merge request on GitLab
    • Step 4: Collaborate with others, leave comments, and fix conflicts
    • Step 5: Merge into master (or other branch)
  • BUT, it’s important to know what’s going on when you branch and merge, because that’s what’s going on under the hood on GitHub/GitLab
  • The main goal here is to be very deliberate about what we put on master.

23 of 28

WORKING WITH REMOTE

Local

A

B

origin/master

C

master

HEAD

D

M

Remote

A

B

A

B

C

D

M

master

24 of 28

WORKING WITH REMOTE

Local

A

B

origin/master

C

master

D

M

X

feature

HEAD

Remote

A

B

A

B

C

D

M

master

25 of 28

WORKING WITH REMOTE

Local

Remote

A

B

A

B

origin/master

C

master

D

M

A

B

C

D

M

X

feature

HEAD

master

Y

Commit happens on origin

26 of 28

WORKING WITH REMOTE

Local

Remote

A

B

A

B

origin/master

C

master

D

M

A

B

C

D

M

X

feature

HEAD

master

Y

X

feature

git push feature

27 of 28

WORKING WITH REMOTE

Local

Remote

A

B

A

B

origin/master

C

master

D

M

A

B

C

D

M

X

feature

HEAD

master

Y

X

feature

Do this on GitLab

M1

28 of 28

WORKING WITH REMOTE

Local

Remote

A

B

A

B

origin/master

C

master

D

M

A

B

C

D

M

X

feature

HEAD

Y

X

M1

master

feature

M2

PULL REQUEST!!!