Git · Lesson 2 · 30 min

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 reading git 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.

Same on both
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:

Same on both
git switch main
git merge footer

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
}
  1. 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 FILE keeps their version, and --ours keeps yours.
  2. git add style.css tells git “this conflict is fixed”.
  3. 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):

Same on both: a shared repo on your own server
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:

Same on both
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
“rejected … fetch first”

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.

Public means public

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?

2. After fixing a conflict in style.css, what are the next two commands?

3. git push is rejected with “fetch first”. What's the right move?

Finished the missions and the quiz? Mark it done to track your progress.

Next up: Linux DevOps · Ship it, automate it, starting with “Containers: build & ship your own image”.