Intermediate Git¶
Introduction to Git worked on a single line of history, on a single machine. This part adds the two features that make Git a collaboration tool: branches, to develop several things in parallel and combine them, and remote repositories, to share a project between machines and people. Both are the foundation of everything GitHub does.
Learning path¶
Follow the pages in order: each one builds on the previous one and ends with hands-on labs.
| Topic | Page | Main commands |
|---|---|---|
| Branches | Working with branches | git branch, git switch, git switch -c |
| Comparing and managing branches | git diff main dev, git branch -m, git branch -d / -D |
|
| Merging branches | git merge, git branch --merged |
|
| Resolving merge conflicts | git merge, git add, git commit, git merge --abort |
|
| Remotes | Remote repositories | git clone, git remote -v, git remote add |
| Fetching and pulling | git fetch, git merge origin/main, git pull |
|
| Pushing to a remote | git push, git push -u |
The big picture¶
Branches live inside one repository; remotes connect several copies of that repository. A typical day combines both:
flowchart LR
subgraph remote["Remote repository (server, GitHub...)"]
rmain[("main")]
end
subgraph local["Local repository"]
lmain["main"]
feat["feature branch"]
end
rmain -- "git clone / git fetch / git pull" --> lmain
lmain -- "git switch -c" --> feat
feat -- "git merge" --> lmain
lmain -- "git push" --> rmain
- Get the latest version of the project from the remote (
git clonethe first time,git pullafterwards). - Create a branch for the task, and commit on it.
- Merge the branch back into
main, resolving conflicts if any. - Send the result to the remote with
git push.
The example project¶
The examples continue the homelab project: the notes and hardware inventory of a few home servers, maintained by Sam Rivera. The repository starts again from a single commit, so that every page can show its branches clearly:
| File | Content |
|---|---|
README.md |
A short description and a to-do list |
inventory.csv |
One line per machine: hostname,ip,role |
services.md |
Which service runs on which machine |
From the remotes pages onwards, the project is shared between Sam's desktop, Sam's laptop, and a small Git server on the network, which holds the shared copy at /srv/git/homelab.git.
All outputs shown in these notes come from real Git sessions on this project.
Practice environment¶
The labs use the same setup and format as before: Linux, Git installed, and a practice folder ~/git-practice that you can delete at any time. Each lab sets a name and email inside its practice repositories only, so your global configuration is left untouched. The remote labs do not need a GitHub account: a remote can be any repository Git can reach, including a folder on the same disk. In the remote labs, you often play two people in turn, each with their own clone: switch directories to switch roles.
Output language and Git version
The outputs shown are in English, from Git 2.55. Run a command with LC_ALL=C in front of it to get English output on a system in another language. Older versions of Git may word some messages slightly differently, and git switch requires Git 2.23 or later (see the older git checkout syntax).
Command summary¶
Branches¶
| Command | Purpose |
|---|---|
git branch |
List local branches; * marks the current one |
git branch <name> |
Create a branch, without switching to it |
git switch <name> |
Switch to an existing branch |
git switch -c <name> |
Create a branch and switch to it |
git diff main monitoring |
Compare the latest commits of two branches |
git branch -m <old> <new> |
Rename a branch |
git branch -d <name> |
Delete a branch that has been merged |
git branch -D <name> |
Force-delete a branch, even if it has not been merged |
git merge <branch> |
Merge <branch> into the current branch |
git branch --merged |
List the branches already merged into the current one |
git merge --abort |
Cancel a merge that stopped on conflicts |
Remotes¶
| Command | Purpose |
|---|---|
git clone <url-or-path> [directory] |
Copy a repository, and remember the original as origin |
git remote |
List the remotes of the repository |
git remote -v |
Same, with their URLs |
git remote add <name> <url> |
Add a remote |
git fetch origin |
Download new commits and branches from origin, without changing your branches |
git merge origin/main |
Merge the fetched main into the current branch |
git pull origin main |
Fetch main from origin and merge it into the current branch |
git push origin main |
Send local main commits to origin |
git push -u origin hotfix |
Create hotfix on origin and set it as the upstream of the local branch |
What comes next¶
With branches and remotes in place, the next step is Introduction to GitHub: hosting repositories online, and collaborating through forks, issues, and pull requests.