Sharing Work Through a Remote
So far every repository has lived in one folder used by one person. Teams share work through a remote: another repository that everyone sends commits to and gets commits from. On a real team the remote usually sits on a hosting service. In this module it is a folder on your own computer, which lets you practise every step of collaboration with no account, no network and nothing to pay for.
You will play two collaborators, Asha and Ravi, each with their own copy of the project. This practice set stays in place for the rest of the module.
The Pieces

shared.gitis a bare repository: it holds the history but has no working tree, so there are no project files to edit in it. Nobody works there; it exists only to be pushed to and fetched from. A bare repository's folder name usually ends in.git.ashaandraviare clones: complete repositories, each with its own working tree and full history, created withgit clone.- In each clone,
originis the name for the remote it was cloned from.git clonesets this up for you.
Commits travel only through the shared repository. Asha pushes her commits to it, and Ravi pulls them from it. The next lessons look at fetching, pulling and pushing one by one; this lesson uses them just enough to see work move between the clones.
Remote-Tracking Branches
A clone remembers where each branch of the remote was the last time it
checked, in a remote-tracking branch. origin/main is your clone's record
of main on origin. You do not commit on it; Git updates it when you fetch,
pull or push.
git remote -v # remotes and their locations
git branch -a # local branches and remote-tracking branches
git remote -v lists origin twice, once for fetching and once for pushing.
git branch -a shows remote-tracking branches as remotes/origin/main, and
git status compares your branch with it, for example
Your branch is up to date with 'origin/main'.
Exercise: Two Collaborators on One Computer
You will create the shared repository, clone it as Asha and as Ravi, and pass two commits between them.
Starting state: Git configured as in the configuration lesson.
-
Create the shared repository:
mkdir -p ~/git-practice/team cd ~/git-practice/team git init --bare -b main shared.git ls shared.git--baremakes a repository with no working tree.lsshows Git's own files, such asHEAD,config,objectsandrefs, and no project files. -
Clone it as Asha, and give this clone Asha's identity:
git clone shared.git asha cd asha git config user.name "Asha Rao" git config user.email "asha@example.com" git statusGit warns
You appear to have cloned an empty repository., which is expected: there are no commits yet. Thegit configcommands without--globalapply only to this clone, so commits made here are by Asha. -
Make the first commit and share it:
cat > README.md <<'EOF' # Team notes EOF git add README.md git commit -m "Add README" git push -u origin mainGit reports
* [new branch] main -> main:mainnow exists in the shared repository. The-ulinks yourmaintoorigin/mainfor later pushes and pulls; the push lesson explains it. -
Look at how the clone sees the remote:
git remote -v git branch -a git statusgit remote -vshowsoriginwith the path toshared.git.git branch -alistsmainandremotes/origin/main.git statussays your branch is up to date withorigin/main. -
Clone the shared repository again as Ravi:
cd .. git clone shared.git ravi cd ravi git config user.name "Ravi Kumar" git config user.email "ravi@example.com" git log --onelineRavi's clone already has "Add README": cloning copied the full history.
-
As Asha, make a second commit and push it:
cd ../asha cat >> README.md <<'EOF' We meet on Mondays. EOF git commit -am "Add meeting day" git push -
As Ravi, pull it:
cd ../ravi git pull git log --onelinegit pullreportsFast-forward, and Ravi's log now has "Add meeting day" on top. Asha's commit reached Ravi throughshared.git.
Success: both clones list origin pointing at shared.git, and the commit
Asha pushed appears in Ravi's clone after a pull.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
fatal: repository 'shared.git' does not exist | You ran git clone from the wrong folder | cd ~/git-practice/team, then clone again |
fatal: destination path 'asha' already exists and is not an empty directory | The clone was made before | Use the existing folder, or remove it and clone again |
src refspec main does not match any | You pushed before making a commit | Commit first, then push |
Already up to date. when Ravi pulls | Asha's push did not happen | Run git push in asha and read its output |
| Commits show the wrong author | The git config commands were run in the wrong folder | Run git config user.name inside each clone to check |
- Keep the practice set. Do not delete
~/git-practice/team: the rest of this module uses it. The last lesson of the module removes it.
Independent task: send work the other way. As Ravi, add the line Bring your laptop. to README.md, commit it and share it, then bring it into
Asha's clone. Show Asha's log, with Ravi's commit on top.
Check Your Understanding
- Why does
shared.githave no working tree? - In Asha's clone, what is
origin/main, and when does it change?
Answer and Reasoning
Independent task
cat >> README.md <<'EOF'
Bring your laptop.
EOF
git commit -am "Ask everyone to bring a laptop"
git push
cd ../asha
git pull
git log --oneline
Run the first three commands in ravi. Asha's pull fast-forwards, and her log
shows "Ask everyone to bring a laptop" on top. git log with --format or
git show would name Ravi Kumar as its author.
Understanding check
- Nobody edits files in the shared repository. It exists only to store the history that the clones push to and fetch from, so it needs no checked-out files. Each person works in their own clone instead.
- It is Asha's clone's record of where
mainwas inshared.gitthe last time her clone communicated with it. It changes only when she fetches, pulls or pushes, not when Ravi pushes: her clone does not know about Ravi's push until she asks.