1 of 21

git Fetch, Pull, and Push

MT03

Kris Jordan

UNC Department of Computer Science

COMP 423: Foundations of Software Engineering (Spring 2025)��Sit anywhere! Make friends! Put your bag in the basket under your seat.

🎒 Today is a whiteboard day, please stow your computers, phones, and tablets away.

Get out individual whiteboards!

2 of 21

COMP 423: Foundations of Software Engineering

  • Recall and practice what you learned in the Git reading on Branch/Merge�
  • Introduce the concepts of Fetch, Pull, and Push�
  • Practice simulating Fetch, Push, Pull�
  • Pairing form for Quiz 00 and Homework

Today's Goals

!

🌟

3 of 21

COMP 423: Foundations of Software Engineering

Using our conventions for representing:�

  • commits as circles labelled with their message�
  • arrows connecting commits to their parent, branch labels�
  • an asterisk for HEAD* (current working branch)

Diagram the result of sequence of commands to the right.

Note: Fast-forward merges ignore messages.

Assume no merge conflicts.

Warm-up

🔥

  1. git init
  2. # make & stage changes
  3. git commit -m "A"
  4. git switch -c wip_0
  5. # make & stage changes
  6. git commit -m "B"
  7. git switch -c wip_1
  8. # make & stage changes
  9. git commit -m "C"
  10. git switch wip_0
  11. git switch -c wip_2
  12. # make & stage changes
  13. git commit -m "D"
  14. git switch main
  15. git merge wip_2 -m "F"
  16. git merge wip_1 -m "E"

4 of 21

COMP 423: Foundations of Software Engineering

  • git init
  • # make & stage changes
  • git commit -m "A"
  • git switch -c wip_0
  • # make & stage changes
  • git commit -m "B"
  • git switch -c wip_1
  • # make & stage changes
  • git commit -m "C"
  • git switch wip_0
  • git switch -c wip_2
  • # make & stage changes
  • git commit -m "D"
  • git switch main
  • git merge wip_2 -m "F"
  • git merge wip_1 -m "E"

5 of 21

git merge, ignores -m in fast-forward commits, by default

What if you want a merge commit to maintain the divergent history?

  • git init
  • # make & stage changes
  • git commit -m "A"
  • git switch -c wip_0
  • # make & stage changes
  • git commit -m "B"
  • git switch -c wip_1
  • # make & stage changes
  • git commit -m "C"
  • git switch wip_0
  • git switch -c wip_2
  • # make & stage changes
  • git commit -m "D"
  • git switch main
  • git merge wip_2 -m "F"
  • git merge wip_1 -m "E"

$ git merge wip_2 -m "F"

Updating 1951f5d..bb3dab2

Fast-forward (no commit created; -m option ignored)

LICENSE | 1 +

README.md | 2 +-

2 files changed, 2 insertions(+), 1 deletion(-)

create mode 100644 LICENSE

git merge wip_2 –-no-ff -m "F"

6 of 21

COMP 423: Foundations of Software Engineering

Git Log's One-line Graph View

7 of 21

COMP 423: Foundations of Software Engineering

Warm-up: Deleting Branches

Complete the diagrams for each of the following scenarios.

🔥

A

B

C

  • git merge wip -m "D"
  • git branch -d wip

main*

wip

A

B

C

main*

wip

  • git branch -d wip

8 of 21

COMP 423: Foundations of Software Engineering

For both diagrams, main is HEAD and on B, wip is on C.

A

B

C

  • git merge wip -m "D"
  • git branch -d wip

A

B

C

  • git branch -d wip

9 of 21

COMP 423: Foundations of Software Engineering

Warm-up: Write the git commands to go from start state to end state.

Start

End

🔥

A

B

C

main

wip*

A

B

C

main*

wip

D

10 of 21

The main branch begins on B, as HEAD, and wip on C.

Start

End

A

B

C

main

wip*

A

B

C

D

11 of 21

COMP 423: Foundations of Software Engineering

Introducing: git fetch

Remote:

origin

(e.g. GitHub)

Local Repo

A

B

C

main

wip

D

A

B

main*

Let's explore the effect of running:

git fetch origin main vs

git fetch origin

12 of 21

COMP 423: Foundations of Software Engineering

Introducing: git pull

Remote:

origin

(e.g. GitHub)

Local Repo

Definition: git pull <remote> <remote-branch>

Expands, effectively, to:

1. git fetch <remote> <remote-branch>

2. git merge <remote>/<remote-branch>

A

B

C

main

wip

D

A

B

main*

Simulate what happens:

git pull origin main

13 of 21

COMP 423: Foundations of Software Engineering

Word of Caution with git pull

Remote:

origin

(e.g. GitHub)

Local Repo

A

B

C

main

wip

D

A

B

main*

Simulate what happens:

git pull origin wip

14 of 21

COMP 423: Foundations of Software Engineering

Trying out a teammate's work locally

Remote:

origin

(e.g. GitHub)

Local Repo

What commands would you run to get your local repo to have its working branch on wip? Hint: fetch and switch

A

B

C

main

wip

D

A

B

main*

15 of 21

COMP 423: Foundations of Software Engineering

Trying out a teammate's work locally

Remote:

origin

(e.g. GitHub)

Local Repo

State After:

git fetch origin

Step 1: Fetch Remote Branches

A

B

C

main

wip

D

A

B

main*

C

origin/main

D

origin/wip

16 of 21

COMP 423: Foundations of Software Engineering

Trying out a teammate's work locally

Remote:

origin

(e.g. GitHub)

Local Repo

State After:

git switch wip

Step 2: Switch to Desired Branch

A

B

C

main

wip

D

A

B

main

C

origin/main

D

origin/wip

wip*

Note: This usage of switch is a shorthand idiom that works when the branch name is unambiguous. Its longer form equivalent is git switch --create wip origin/wip. Good reason to choose meaningful branche names and tidy up when a branch is merged! This tracks origin/wip as upstream.

17 of 21

COMP 423: Foundations of Software Engineering

Introducing: git push

Remote:

origin

(e.g. GitHub)

Local Repo

Definition: git push <remote> <remote-branch>

Sync commits from HEAD to a remote repository and update remote branch.

A

B

C

main*

wip

D

A

B

main

Simulate what happens:

git push origin main

18 of 21

COMP 423: Foundations of Software Engineering

Introducing: git push

Remote:

origin

(e.g. GitHub)

Local Repo

Definition: git push --set-upstream <remote> <remote-branch>

When pushing a local branch remotely for the first time, establish upstream tracking.

A

B

C

main

wip*

D

A

B

main

Simulate what happens:

git push --set-upstream origin wip

19 of 21

COMP 423: Foundations of Software Engineering

Word of Caution with: git push

Remote:

origin

(e.g. GitHub)

Local Repo

Know where your HEAD is!

A

B

C

main

wip*

D

A

B

main

Simulate what happens:

git push origin main

20 of 21

Common Issue: Local/Remote Branch Divergence

Remote:

origin

(e.g. GitHub)

Local Repo

A

B

main*

D

A

B

main

What happens here?

git push origin

C

! [rejected] main -> main (fetch first)

error: failed to push some refs to 'origin'

hint: Updates were rejected because the remote contains work that you do not have

hint: locally. This is usually caused by another repository pushing to the same ref.

hint: You may want to first integrate the remote changes (e.g., 'git pull')

hint: before pushing again.

21 of 21

COMP 423: Foundations of Software Engineering

  • Released Today:
    • By 5PM TODAY Pairing form for QZ00 and EX00: See Course Web Site for Link
    • RD06 - Starting a Static Website Project with MkDocs - Tue 1/21
  • Releasing by Friday:
    • RD07 - git Ch 4 - Remote Operations: Fetch, Pull, and Push - Tue 1/21
    • EX00 - Collaborating on a Documentation Project - Thu 1/23

Homework

!

🌟