The first push
Ana has one commit. On GitHub she creates an empty my-project. git remote add registers the remote under the alias origin. git push -u origin main uploads it; main now tracks origin/main.
Ana has one commit. On GitHub she creates an empty my-project.
git remote add registers the remote under the alias origin.
git push -u origin main uploads it; main now tracks origin/main.
Ana has one commit. On GitHub she creates an empty my-project. git remote add registers the remote under the alias origin. git push -u origin main uploads it; main now tracks origin/main.
Bob uses git clone: it downloads the repository with all its history.
The clone already has origin; its main already tracks origin/main.
Bob uses git clone: it downloads the repository with all its history. The clone already has origin; its main already tracks origin/main.
Bob adds tasks.txt, commits and pushes: GitHub moves forward.
Ana runs git status, and it says she is up to date.
It compares with origin/main: the snapshot from her last talk with GitHub.
Bob adds tasks.txt, commits and pushes: GitHub moves forward. Ana runs git status, and it says she is up to date. It compares with origin/main: the snapshot from her last talk with GitHub.
git fetch downloads the new commit and only moves origin/main.
main and the files do not change. git status now says: behind by 1 commit.
git fetch downloads the new commit and only moves origin/main. main and the files do not change. git status now says: behind by 1 commit.
git pull runs a git fetch, then merges origin/main into main.
Ana had no commits of her own: a fast-forward, main just moves ahead.
Then she adds a task, commits and pushes it with git push.
git pull runs a git fetch, then merges origin/main into main. Ana had no commits of her own: a fast-forward, main just moves ahead. Then she adds a task, commits and pushes it with git push.
Meanwhile, Bob changes the README without bringing in that work.
GitHub rejects his push: the remote has work that Bob does not have.
A push only moves the remote forward: accepting it would erase Ana's commit.
Meanwhile, Bob changes the README without bringing in that work. GitHub rejects his push: the remote has work that Bob does not have. A push only moves the remote forward: accepting it would erase Ana's commit.
Bob sets pull.rebase false: when branches diverge, Git merges.
git pull brings in Ana's commit and creates a merge commit with two parents.
Now Bob's main contains everything on the remote.
Bob sets pull.rebase false: when branches diverge, Git merges. git pull brings in Ana's commit and creates a merge commit with two parents. Now Bob's main contains everything on the remote.
Now his push is a fast-forward and GitHub accepts it: nothing is lost.
push uploads, fetch only looks, pull integrates.
If a push is rejected: git pull first, then git push.
Now his push is a fast-forward and GitHub accepts it: nothing is lost. push uploads, fetch only looks, pull integrates. If a push is rejected: git pull first, then git push.