Git: branches, merges & sharing
Git really shines when you try ideas without breaking what works, and when several people work on the same project. This lesson covers branches (parallel versions), merging them back together, what to do when two changes conflict, and sharing with push and pull.
You will learn
- Branches:
git branch,git switch -c, and readinggit log --graph - Merging, fast-forwards, and fixing a merge conflict calmly
- Remotes:
clone,push,pull, a shared repository on your own server, and GitHub
Branches: try ideas safely
A branch is a separate line of commits. main holds the version that works. Start a new branch for each idea, and if it goes badly, you just switch back to main.
git branch # list branches (* = the one you're on) git switch -c footer # create a branch and move to it …edit, add, commit… git switch main # back to main: your files change to main's version! git log --oneline --graph --all
Older tutorials use git checkout -b footer and git checkout main, which do the same thing. switch is the newer, clearer name.
Merging
When an idea is ready, bring its commits into main:
git switch main git merge footer
- Fast-forward: if main hasn't changed since you branched off, git simply moves main forward. No new commit is needed.
- Merge commit: if both sides have new commits, git combines them and makes a commit with two parents.
- Conflict: if both sides changed the same lines differently, git can't guess which one wins, so it asks you.
Merge conflicts: don't panic
CONFLICT (content): Merge conflict in style.css Automatic merge failed; fix conflicts and then commit the result.
Git puts both versions into the file, between markers:
body {
font-family: sans-serif;
<<<<<<< HEAD
background: #f5f5f0; ← your branch (main)
color: black;
=======
background: #111; ← the branch you're merging
color: #eee;
>>>>>>> dark-mode
}
- Open the file and make it look how it should: keep one side, the other, or a mix. Delete the
<<<<<<<,=======and>>>>>>>lines.
Shortcuts:git checkout --theirs FILEkeeps their version, and--ourskeeps yours. git add style.csstells git “this conflict is fixed”.git commit -m "Merge dark mode"finishes the merge.
Changed your mind? git merge --abort puts everything back as it was before the merge. git status always tells you where you are.
Remotes: sharing a repository
A remote is another copy of the repository, usually on a server or on GitHub. You push your commits to it and pull other people's commits from it. Git doesn't need GitHub. Any Linux server you can reach works, using a bare repository (one without a working folder, just the history):
sudo mkdir -p /srv/git && sudo chown $USER: /srv/git git init --bare /srv/git/site.git # the shared copy git remote add origin /srv/git/site.git # "origin" = the usual name for the main remote git push -u origin main # -u: remember this, so later it's just "git push"
A teammate gets their own copy with clone, and you keep in sync with pull:
git clone /srv/git/site.git # on this server git clone student@192.168.1.50:/srv/git/site.git # from another computer, over SSH git pull # get new commits (fetch + merge) git push # send yours
If someone pushed before you, git refuses your push. It's protecting their work, not being broken. Run git pull, fix any conflicts, then git push again. Newer git versions may ask you to choose how to pull the first time. git config --global pull.rebase false picks the simple merge behavior used here.
GitHub (and friends)
GitHub, GitLab and Codeberg host remotes on the internet, with a website around them. You make a repository on the site, then connect yours to it. The commands are identical:
git remote add origin git@github.com:YOUR-NAME/site.git git push -u origin main
The git@github.com: address uses SSH keys, the same keys as in Linux Basics, lesson 3. Add your id_ed25519.pub in GitHub's settings. The site's HTTPS addresses work too, but they need a token instead of your password.
A public repository's whole history can be downloaded by anyone. Before your first push, check git log -p for passwords or keys, and use .gitignore (lesson 1).
Try it: the Coding Club website 🌐
Your home folder has site/, the Coding Club website, with some history already. Priya made a dark-mode branch. Git is set up with your name.
Quick check
1. You switch from footer back to main, and your footer disappears from index.html. What happened?
✓ Switching branches swaps your files to that branch's version.
2. After fixing a conflict in style.css, what are the next two commands?
✓ add marks it resolved and commit finishes the merge. (restore would throw your fix away!)
3. git push is rejected with “fetch first”. What's the right move?
✓ Someone else pushed first. Get their work, then add yours on top.
Next up: Linux DevOps · Ship it, automate it, starting with “Containers: build & ship your own image”.