Show contents
Chapter 5 of 7 · 20 min
Remote repositories
By the end of this chapter, you'll be able to
- Connect a local repository to a remote repository
- Send your commits with git push and get the others' with git pull
- Copy an existing repository with git clone
- Exclude files from tracking with .gitignore
A server to meet on
A remote is another copy of the repository, usually on a platform such as GitHub, GitLab or Codeberg. It is given a short name, by convention origin. On a platform, you create an empty repository from the website, which gives you its address, for example https://github.com/awa/bakery.git.
To practise without an account or a connection, a plain folder can play the server. We start from Awa's repository, with one commit:
git init
echo "<h1>Kofi's Bakery</h1>" > index.html
git add index.html
git commit -m "Add the home page"
We create the “server” next to it: a bare repository, without a working directory, used only to store the history.
git init --bare ../server.git
git remote add origin ../server.git
git remote -v
origin ../server.git (fetch)
origin ../server.git (push)
With a platform, only the address changes: git remote add origin https://github.com/awa/bakery.git.
Send: git push
git push -u origin main
To ../server.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
-u links your main branch to the server's, once and for all: afterwards, git push and git pull are enough. Git can now compare the two:
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
On a platform, Git asks you to authenticate. Platforms no longer accept the account password for Git: you use an access token or an SSH key, following the platform's documentation. Never paste these secrets into a file of the repository.
Get: git clone and git pull
Kofi joins the project. git clone copies the whole repository, history included, and sets up origin automatically:
git clone ../server.git ../kofi
Kofi introduces himself for this repository only (without --global), adds a line and sends his commit:
cd ../kofi
git config user.name "Kofi Mensah"
git config user.email "kofi@example.com"
echo "<p>New: butter croissants</p>" >> index.html
git commit -am "Announce croissants"
git push
On her side, Awa gets this work with git pull:
git pull
From ../server
3f2a9c1..9e8d7c6 main -> origin/main
Updating 3f2a9c1..9e8d7c6
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
git log --format="%an: %s"
Kofi Mensah: Announce croissants
Awa Diallo: Add the home page
git pull does two things: git fetch (download the new commits from the server) then git merge (merge them into your branch). You can run git fetch alone to look at what changed before merging.
Both repositories have the same history.
Text description of the animation
- Kofi makes a commit. For now, it only exists on his computer.
- git push sends his new commits to the server.
- The server now has Kofi's commit. Awa does not have it yet.
- git pull downloads the new commits, then merges them into Awa's branch.
- Both repositories have the same history.
What we do not track: .gitignore
Some files have no place in the repository: downloaded dependencies, logs, generated files and above all secrets (passwords, API keys). The .gitignore file lists patterns that Git will ignore:
printf "node_modules/\n*.log\n.env\n" > .gitignore
echo "API_KEY=abc123" > .env
echo "error 500" > server.log
git status -s
?? .gitignore
.env and server.log no longer appear. *.log means every file ending in .log, and node_modules/ a whole folder. The .gitignore itself is tracked, so that the whole team benefits from it:
git add .gitignore
git commit -m "Ignore logs and secrets"
git push
Merge requests
On platforms, the team does not push directly to main. Everyone pushes their branch, then opens a merge request (pull request on GitHub, merge request on GitLab). Colleagues review the changes, comment, automated tests run, and the branch is merged only once approved. It is a chance to learn from each other and to catch mistakes before they reach production.
cat closing.txt
Closed on 1 May
Your turn
Worked exercise
Worked exercise: share a branch
Create a promo branch, add to it a file promo.txt containing “-20% on cakes on Sundays” in a commit “Add the Sunday promotion”, then send this branch to the server. Finally, display the branches the server knows.
Hint 1
-u origin followed by its name.Hint 2
git branch -r lists the remote branches your repository knows.Show the solution and common mistakes
git switch -c promo
echo "-20% on cakes on Sundays" > promo.txt
git add promo.txt
git commit -m "Add the Sunday promotion"
git push -u origin promo
$ git branch -r
origin/main
origin/promo
Depending on your Git version, an origin/HEAD -> origin/main line may also appear: it indicates the server's main branch. To check the content of the remote branch, display its last commit:
git log --format="%s" -1 origin/promo
Add the Sunday promotion
On a platform, Git also displays a link to open a merge request from promo to main directly.
Common mistakes:
- Typing
git pushwithout-u origin promo: Git answers that the branch has no equivalent on the server (has no upstream branch) and suggests the right command. - Thinking that
git commitis enough to share the work: until you push, the commit only exists on your computer.
Summary
- 1A remote repository, named origin by convention, is the team's meeting point.
- 2git push sends your commits, git pull fetches and merges the others', git clone copies a whole repository.
- 3A push is rejected when the server has commits you do not have: git pull first, then git push.
- 4.gitignore excludes dependencies, logs and secrets from tracking.
- 5In a team, you push a branch and open a merge request reviewed by the others.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.