Git: Beginner to Advanced
Remotes and collaboration
Share changes and review team work.
Lesson 6 of 12
Reading: about 7 min
Lesson + practice: about 15 min
Difficulty: Intermediate
On this page5 sections
Share changes and review team work. Follow the animated example, then use the sections below to review each idea.
Visual walkthrough
Use the player controls or jump menu to inspect each step. The diagram changes as the example moves forward.
The player shows one focused example. The sections below explain the ideas behind each topic.
Until now everything has happened in one repository on your computer. Collaboration needs a shared copy, usually on a hosting service such as GitHub, GitLab or Bitbucket. You download your teammates' work from it and send yours to it.
In these examples the shared repository lives at https://github.com/your-team/planner.git (substitute your own). Real hosting services also print progress lines such as remote: Counting objects; they are left out here so the important lines stand out. Commit IDs will differ.
Clone and remotes
git clone copies a whole repository, every commit and branch, into a new folder and sets up a connection back to where it came from. That connection is a remote, a saved name for another repository's address. The one created by clone is called origin.
Copy a shared repository
Clone creates the planner folder. git remote -v shows the address origin refers to for downloading (fetch) and uploading (push).
git clone https://github.com/your-team/planner.git
cd planner
git remote -v$ git clone https://github.com/your-team/planner.git
Cloning into 'planner'...
done.
$ cd planner
$ git remote -v
origin https://github.com/your-team/planner.git (fetch)
origin https://github.com/your-team/planner.git (push)Local and remote-tracking branches
main is your own branch. remotes/origin/main is a remote-tracking branch: your repository's memory of where main was on the server the last time you checked. You never commit to it directly.
git branch -a
git status$ git branch -a
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleanFetch, pull, and push
Three commands move commits between your repository and the shared one:
git fetchdownloads new commits and updatesorigin/main, but leaves your ownmainand your files alone.git pullfetches and then brings the new commits into your current branch.git pushuploads your commits and moves the branch on the server.
A teammate pushed: fetch first, then look
After fetching, status reports you are one commit behind. The log shows origin/main ahead of your main (HEAD -> main). Nothing in your folder has changed yet.
git fetch
git status
git log --oneline --decorate --all$ git fetch
From https://github.com/your-team/planner
97009a9..40e637f main -> origin/main
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
$ git log --oneline --decorate --all
40e637f (origin/main, origin/HEAD) Describe the project
97009a9 (HEAD -> main) Add READMEBring the work into your branch
--ff-only tells pull to update only when it can fast-forward, so it never creates a surprise merge commit.
git pull --ff-onlyUpdating 97009a9..40e637f
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)Share your own commit
After committing, status says you are one commit ahead. git push sends it and moves main on the server.
git commit -m "Add tests"
git status
git push$ git commit -m "Add tests"
[main f4bd078] Add tests
1 file changed, 1 insertion(+)
create mode 100644 tests.txt
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git push
To https://github.com/your-team/planner.git
40e637f..f4bd078 main -> mainWhen someone pushed while you were working, your push is rejected. This protects their work: Git will not let you replace commits you have not seen.
$ git push
To https://github.com/your-team/planner.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/your-team/planner.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.Combine the two lines of work, then push
Both sides have new commits, so a fast-forward is impossible and --ff-only stops safely (it also prints a few hint lines, trimmed here). A regular pull merges the server's commit with yours (resolve any conflict as in the Merging lesson), and the push then succeeds.
git pull --ff-only
git pull --no-rebase --no-edit
git push$ git pull --ff-only
fatal: Not possible to fast-forward, aborting.
$ git pull --no-rebase --no-edit
Merge made by the 'ort' strategy.
README.md | 1 +
1 file changed, 1 insertion(+)
$ git push
To https://github.com/your-team/planner.git
3c60426..02ac8cc main -> mainUpstream branches
An upstream is the remote branch your local branch normally pulls from and pushes to. Cloned main gets origin/main automatically, which is why plain git pull and git push worked above. A branch you create yourself has no upstream until you set one.
Publish a new branch
The first plain push fails and tells you what to do. -u (short for --set-upstream) pushes the branch and remembers origin/search as its upstream, so later you can just type git push.
git switch -c search
git push
git push -u origin search$ git switch -c search
Switched to a new branch 'search'
$ git push
fatal: The current branch search has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin search
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
$ git push -u origin search
To https://github.com/your-team/planner.git
* [new branch] search -> search
branch 'search' set up to track 'origin/search'.See every branch's upstream
The upstream is shown in square brackets. When a branch is ahead or behind, the brackets say so too, for example [origin/main: ahead 1].
git branch -vv main f4bd078 [origin/main] Add tests
* search 01e5e04 [origin/search] Add searchPull requests and reviews
A pull request (GitLab calls it a merge request) is a page on the hosting service that asks to merge one branch into another. It is not a Git command; it is how a team reviews work before it reaches main.
- Create a branch, commit your work, and push it with
git push -u origin branch-name. - On the hosting site, open a pull request from your branch into
main. Describe what changed and why. - Teammates read the diff, leave comments and request changes. Automated checks such as tests usually run here too.
- Fix what was raised by committing on the same branch and pushing again. The pull request updates itself.
- Once approved, merge it on the site. Then update your own
mainwithgit switch mainandgit pull.
git status says “Your branch is up to date with 'origin/main'”, but your teammate says they pushed an hour ago. How can both be true?
Reveal answer
Status compares with your last download, not the live server. Run git fetch and status will report that you are behind.
Your push was rejected with “fetch first”. What should you do?
Reveal answer
Run git pull to bring in the commits that were pushed in the meantime, resolve any conflicts, then git push again. Do not use --force, which would delete your teammates' commits from the server.
Your progress
Reveal the check answer or reach the end to complete this lesson.