Git and GitHub · Part 3 · GitHub and remote repositories
Push, pull and fetch in depth
When two people push
With one person, push and pull just work. With two, their work crosses: a push gets rejected, branches diverge, and someone is tempted to force-push. This lesson covers those everyday situations, using a local “server” like the one in Lesson 16, because it makes two people easy to simulate. Everything works the same with GitHub.
A two-person lab
cd ~/Desktop
mkdir sync-lab
cd sync-lab
git init --bare server.git
git clone server.git nithin
git clone server.git teammateGit warns You appear to have cloned an empty repository, which is fine. Nithin makes the first commit, and the teammate pulls it:
cd nithin
echo "line 1" > app.txt
git add app.txt
git commit -m "Add app"
git push -u origin main
cd ../teammate
git pullgit push
A push sends new commits to the remote, moves the remote's branch forward, and updates the local origin/main to match.
git push # the current branch, to the branch it tracks
git push origin main # the same, said in full
git push -u origin feature/x # a new branch's first push, setting up trackingTyping -u origin branch-name for every new branch gets old. Since Git 2.37, one setting does it on the first push:
git config --global push.autoSetupRemote trueAfter that, plain git push works on a brand-new branch too.
The rejected push
The teammate pushes first:
echo "teammate line" >> app.txt
git commit -am "Teammate change"
git pushNithin commits too, without pulling, and pushes:
echo "nithin line" > notes.txt
git add notes.txt
git commit -m "Add notes"
git pushTo /Users/you/Desktop/sync-lab/server.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '/Users/you/Desktop/sync-lab/server.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.Git is protecting the teammate's work. The server has A then T; the laptop has A then N. Accepting the push would move the server's main to N and throw T away. So a push is only allowed when it's a fast-forward for the server: the new commits build on everything the server already has.
The fix is to bring the teammate's work in first, then push again:
git pull
git pushHow git pull combines the two is the subject of the git pull section.
Force-pushing safely
After rebasing or squashing a branch that's already pushed (Lessons 13 and 14), its history has changed, and a normal push is rejected. Replacing the remote's history takes a force-push, and there are two kinds:
| Command | Tells the server |
|---|---|
git push --force | Match my laptop, no matter what. Anything a teammate pushed meanwhile is deleted. |
git push --force-with-lease | Replace it, but only if it's still where I last saw it. Otherwise, refuse. |
1.Both people get a branch
git pull
git switch -c feature/profile
echo "v1" > profile.txt
git add profile.txt
git commit -m "Add profile"
git pushgit fetch
git switch feature/profile
echo "teammate fix" >> profile.txt
git commit -am "Teammate fix to profile"
git pushThere's no local feature/profile in the teammate's copy, so git switch creates one that tracks origin/feature/profile.
2.Rewrite and force-push
Nithin, without fetching, amends his commit and force-pushes:
git commit --amend -m "Add profile page"
git push --force-with-lease! [rejected] feature/profile -> feature/profile (stale info)
error: failed to push some refs to '/Users/you/Desktop/sync-lab/server.git'Stale info means the server changed since Nithin's last look, so the push was refused and the teammate's fix is safe. Plain --force would have deleted it, so don't run that here.
| Situation | Force-push? |
|---|---|
| Your own feature branch after a rebase or squash | Yes, with --force-with-lease |
| Your own branch with an open pull request | Yes, and tell the reviewers |
main, develop or any shared branch | Never |
| A branch other people push to | Only after agreeing it first |
An alias makes the safe version the easy one to type:
git config --global alias.pushf "push --force-with-lease --force-if-includes"
git pushfgit fetch
git fetch downloads new commits and branches and updates the origin/* bookmarks. It never touches local branches or files, so it's always safe to run. (git fetch --all fetches from every remote, useful with forks.)
Its real use is looking before bringing changes in. The teammate pushes an update:
git switch main
git pull
echo "important update" >> app.txt
git commit -am "Important update"
git pushgit switch main
git fetch
git statusOn branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.| Question | Command |
|---|---|
| What does the remote have that I don't? | git log main..origin/main --oneline |
| What do I have that the remote doesn't? | git log origin/main..main --oneline |
| What changed in the code? | git diff main origin/main |
A..B means “commits in B that aren't in A”. Here the first command shows cf1e177 Important update and the second shows nothing. Once it looks right, bring it in with git merge origin/main (or git rebase origin/main). That pair is exactly what git pull does in one step.
Cleaning up deleted branches
When a branch is deleted on GitHub, usually after its pull request is merged, the local origin/ bookmark for it stays. Over months, dozens pile up:
git branch -rorigin/HEAD -> origin/main
origin/feature/profile
origin/mainorigin/feature/profile was already deleted on the server. --prune removes bookmarks like it:
git fetch --prune- [deleted] (none) -> origin/feature/profileTo prune on every fetch and pull: git config --global fetch.prune true. Then git branch -vv marks local branches whose remote is gone:
feature/profile 77f98ec [origin/feature/profile: gone] Add profile page
* main cf1e177 [origin/main] Important updateDelete those with git branch -d feature/profile. If the pull request was squash-merged, Git refuses with not fully merged, because the branch's own commits never reached main. Once GitHub shows it as merged, -D deletes it anyway.
To delete a branch on the server from the terminal: git push origin --delete feature/profile. On GitHub, the Delete branch button after merging a pull request is more common.
git pull
git pull is git fetch followed by a way of combining. If there are no new local commits, there's nothing to combine: Git fast-forwards, the same with every strategy. The decision comes when both sides have new commits:
Diverged: N is Nithin's new commit, T is the teammate's.
| Strategy | Setting | Result |
|---|---|---|
Merge (git pull --no-rebase) | pull.rebase false | A merge commit joins N and T. Never rewrites anything, but a busy team gets dozens of “Merge branch 'main' of …” commits a day. |
Rebase (git pull --rebase) | pull.rebase true | N is replayed on top of T as N′. A straight line, and safe, because only unpushed local commits are rewritten. |
Fast-forward only (git pull --ff-only) | pull.ff only | Works only when simply behind. On a divergence it stops, and the choice is made by hand. |
After git pull --no-rebase: a merge commit M with two parents.
After git pull --rebase: one straight line.
With no setting at all, a diverged pull stops and asks for one:
hint: You have divergent branches and need to specify how to reconcile them.
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
fatal: Need to specify how to reconcile divergent branches.Lesson 2 set pull.rebase false to avoid that, and Lesson 13 offered true. The rebase setting, plus one more, is what many developers use:
git config --global pull.rebase true
git config --global rebase.autoStash trueWithout rebase.autoStash, a pull with uncommitted changes refuses: error: cannot pull with rebase: You have unstaged changes. With it, Git stashes them, rebases and brings them back (Lesson 15). If a team prefers merges, follow the team.
Try the rebase pull
echo "teammate A" >> app.txt
git commit -am "Teammate A"
git pushecho "nithin B" > b.txt
git add b.txt
git commit -m "Nithin B"
git pull --rebase
git log --oneline --graph* 34f1016 (HEAD -> main) Nithin B
* fd13746 (origin/main) Teammate A
* cf1e177 Important updateA straight line, ready for git push.
Conflicts during a pull
A pull that hits a conflict is fixed the same way as any other. Only the last command depends on the strategy:
| Rebase pull | Merge pull | |
|---|---|---|
| Fix | Edit the file, delete the markers | Edit the file, delete the markers |
| Mark as fixed | git add app.txt | git add app.txt |
| Finish | git rebase --continue | git commit |
| Back out | git rebase --abort | git merge --abort |
During a rebase pull, the sides are swapped (Lesson 13): “current” (HEAD) is what came from the server, and “incoming” is the local commit.
A teammate's branch
The teammate pushed feature/search and asked for a look:
git fetch
git branch -a* main
remotes/origin/HEAD -> origin/main
remotes/origin/feature/search
remotes/origin/maingit switch feature/searchSwitched to a new branch 'feature/search'
branch 'feature/search' set up to track 'origin/feature/search'.Git finds origin/feature/search and makes a local branch that tracks it, so git pull and git push work on it. To link a branch by hand: git branch -u origin/feature/search.
A normal day
# Morning: get the latest
git switch main
git pull
# Start a feature, commit often
git switch -c feature/filters
git commit -am "Add price filter"
git commit -am "Add bedroom filter"
# Back it up (fine even when unfinished, on your own branch)
git push
# main moved on: bring the branch up to date
git fetch
git rebase origin/main
git push --force-with-lease
# After the pull request is merged
git switch main
git pull
git branch -d feature/filtersRecommended settings
git config --global push.autoSetupRemote true
git config --global pull.rebase true
git config --global rebase.autoStash true
git config --global fetch.prune true
git config --global alias.pushf "push --force-with-lease --force-if-includes"git config --global --list shows them all.
Quick reference
| Task | Command |
|---|---|
| Push the current branch | git push |
| First push of a new branch | git push -u origin branch |
| Push was rejected | git pull, then git push |
| Force-push your own branch | git push --force-with-lease |
| Delete a remote branch | git push origin --delete branch |
| Download without changing anything | git fetch |
| What's new on the remote | git log main..origin/main --oneline |
| What isn't pushed yet | git log origin/main..main --oneline |
| Compare code with the remote | git diff main origin/main |
| Clean up deleted remote branches | git fetch --prune |
| Find branches deleted on the server | git branch -vv |
| Pull with a straight history | git pull --rebase |
| Pull only if it fast-forwards | git pull --ff-only |
| Work on a teammate's branch | git fetch, then git switch branch |