Look Before You Integrate
When a teammate pushes, their commits sit in the shared repository until you ask for them. You could pull them straight into your branch, but it is often worth looking first: what changed, who changed it, and whether it affects what you are doing.
git fetch downloads new commits from the remote without touching your work.
You inspect them, then decide how to integrate them.
What Fetch Changes
git fetch updates your remote-tracking branches, such as origin/main,
to match the remote. That is all. Your own branches, your staging area and the
files in your folder stay exactly as they were. Git reports each update:
From /home/you/git-practice/team/shared
4c1e2a0..9b7d3f1 main -> origin/main
This is illustrative output; your path and IDs will differ. The line means
origin/main moved from one commit to another.
Because fetching never changes your work, it is always safe to run.
Ahead, Behind and Diverged
git status compares your branch with its remote-tracking branch:
| Message starts with | Meaning |
|---|---|
Your branch is up to date | Same commit as your last fetch saw |
Your branch is behind | The remote has commits you do not |
Your branch is ahead | You have commits not yet pushed |
Your branch and 'origin/main' have diverged | Both have new commits |
These messages describe the remote as of your last fetch. If you have not
fetched, git status can say "up to date" while new commits are waiting in
the shared repository.
Inspecting Incoming and Outgoing Commits
Two dots between branch names select the commits on the right that are not on the left:
git log --oneline main..origin/main # incoming: on the remote, not on main
git log --oneline origin/main..main # outgoing: on main, not yet pushed
git diff main origin/main # what integrating would change in the files
Read main..origin/main as "what origin/main has that main lacks". Add
--stat to git diff for a file summary.
When you are ready, integrate. git merge origin/main brings the fetched
commits into your branch. That fetch-then-merge pair is exactly what
git pull does, which the next lesson covers.
Exercise: Inspect Incoming Work Before Integrating
Asha adds a meeting schedule. As Ravi, you will find out about it, inspect it and only then bring it in.
Starting state: Git configured as in the configuration lesson. You will rebuild the team practice set so everyone starts from the same state.
-
Rebuild the practice set. This replaces any
~/git-practice/teamfolder from the previous lesson:cd ~/git-practice rm -rf team mkdir team cd team git init --bare -b main shared.git git clone shared.git asha cd asha git config user.name "Asha Rao" git config user.email "asha@example.com" cat > README.md <<'EOF' # Team notes We meet on Mondays. Bring your laptop. EOF git add README.md git commit -m "Add team notes" git push -u origin main cd .. git clone shared.git ravi cd ravi git config user.name "Ravi Kumar" git config user.email "ravi@example.com" cd ..You end in
~/git-practice/team, with a shared repository and two clones that both have the "Add team notes" commit. -
As Asha, add a schedule and share it:
cd asha cat > schedule.md <<'EOF' # Schedule Monday: planning Wednesday: review EOF git add schedule.md git commit -m "Add meeting schedule" git push -
As Ravi, check the status:
cd ../ravi git statusIt says
Your branch is up to date with 'origin/main'.That is out of date: Ravi's clone has not asked the shared repository since cloning. -
Fetch, then check again:
git fetch git statusGit reports
main -> origin/main, and the status now saysYour branch is behind 'origin/main' by 1 commit, and can be fast-forwarded. -
Inspect the incoming work before integrating:
git log --oneline main..origin/main git diff main origin/main lsThe log names the incoming commit, "Add meeting schedule". The diff shows the new
schedule.md.lsdoes not list it yet: fetch changed nothing in your folder. -
Integrate it:
git merge origin/main ls git statusThe merge fast-forwards,
schedule.mdappears, and the status is up to date again.
Success: you named the incoming commit before integrating it, and your files did not change until the merge.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
git fetch prints nothing | Nothing new in the shared repository | Check that Asha's git push succeeded |
main..origin/main lists nothing after fetching | The commit is already in main | git log --oneline to look |
| Status still says up to date after Asha pushed | Ravi has not fetched | git fetch, then git status |
fatal: not a git repository | You are in ~/git-practice/team, not inside a clone | cd ravi or cd asha |
- Keep the practice set for the next lesson.
Independent task: now both sides have new work. Run this setup, which makes an unpushed commit in Ravi's clone and pushes a new commit from Asha's:
cat >> README.md <<'EOF'
Snacks are on Ravi this week.
EOF
git commit -am "Note who brings snacks"
cd ../asha
cat >> schedule.md <<'EOF'
Friday: demo
EOF
git commit -am "Add Friday demo"
git push
cd ../ravi
As Ravi, find out what is incoming and what is outgoing without integrating
anything. Name each commit, and say what git status reports.
Check Your Understanding
- Why is
git fetchsafe to run at any time? - Ravi's
git statussays he is up to date, yet Asha pushed ten minutes ago. How can both be true?
Answer and Reasoning
Independent task
git fetch
git status
git log --oneline main..origin/main
git log --oneline origin/main..main
git status says Ravi's branch and origin/main have diverged, with 1 and 1
different commits each. Incoming, in main..origin/main: "Add Friday demo".
Outgoing, in origin/main..main: "Note who brings snacks". The next lesson
shows how to integrate diverged work.
Understanding check
- Fetch only downloads commits and moves remote-tracking branches such as
origin/main. It never changes your branches, your staging area or the files in your folder; those change only when you integrate. git statuscompares Ravi's branch with his clone'sorigin/main, which reflects the shared repository as of his last fetch. Until he fetches again, his clone does not know about Asha's push.