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!
COMP 423: Foundations of Software Engineering
Today's Goals
!
🌟
COMP 423: Foundations of Software Engineering
Using our conventions for representing:�
Diagram the result of sequence of commands to the right.
Note: Fast-forward merges ignore messages.
Assume no merge conflicts.
Warm-up
🔥
COMP 423: Foundations of Software Engineering
git merge, ignores -m in fast-forward commits, by default
What if you want a merge commit to maintain the divergent history?
$ 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"
COMP 423: Foundations of Software Engineering
Git Log's One-line Graph View
COMP 423: Foundations of Software Engineering
Warm-up: Deleting Branches
Complete the diagrams for each of the following scenarios.
🔥
A
B
C
main*
wip
A
B
C
main*
wip
COMP 423: Foundations of Software Engineering
For both diagrams, main is HEAD and on B, wip is on C.
A
B
C
A
B
C
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
The main branch begins on B, as HEAD, and wip on C.
Start
End
A
B
C
main
wip*
A
B
C
D
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
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
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
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*
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
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.
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
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
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
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.
COMP 423: Foundations of Software Engineering
Homework
!
🌟