Git series: 1. Commits and the object model · 2. Remotes, tags and backups · 3. Branching, merge, rebase and PRs
Part 1 was about what happens on your own machine: Git’s three areas, the object model and how to write commits. This part takes the repo off your machine: pushing to GitHub, cloning onto another machine, knowing exactly what is not backed up, then numbering versions with Semantic Versioning and tags. Read part 1 first, especially the section on commit conventions, because the versioning scheme at the end builds on it.
At a glance:
flowchart LR
W[Working directory] -->|commit| L[Local repo]
L -->|push| R["Remote (GitHub)"]
R -->|"fetch / pull"| L
R -->|"clone (new machine)"| L
ST["Stash, unpushed branches, ignored files"] -.->|local only| W
L -->|annotated tag| TG["Tag: SemVer MAJOR.MINOR.PATCH"]
TG -->|"push tags explicitly"| R
1. GitHub and remotes
Key idea: a remote is a copy of the repo on another machine (usually GitHub), so your code doesn’t die with your laptop and others can see it. Add it with git remote add, send with git push, receive with git fetch/git pull.
GitHub is a platform for hosting and sharing Git repos (others: GitLab, Bitbucket, Gitea).
# 1. Create an empty repo on GitHub (web UI, or: gh repo create booking-service --private)
# 2. Add the remote
git remote add origin git@github.com:<user>/booking-service.git
git remote -v
# 3. Push the branch; -u lets you just type "git push" next time
git push -u origin main
# Get other people's changes
git fetch origin # download only, your branches are untouched
git pull --rebase origin main
git pull is git fetch followed by git merge (or rebase if pull.rebase=true). Seniors often fetch first to see what’s new, then decide how to integrate it.
Clone: getting a repo onto a new machine
Key idea: git clone <url> makes a full copy of the repo (history included) on a machine that doesn’t have it. Clone once, then just fetch and pull.
The GitHub slide has two pictures: the first machine pushes its history to GitHub, then a different laptop downloads that same history with git clone.
git clone git@github.com:<user>/booking-service.git # creates booking-service/
git clone git@github.com:<user>/booking-service.git my-dir # pick a different folder name
git clone -b develop git@github.com:<user>/booking-service.git # check out develop straight away
| Command | Use it when | What it does |
|---|---|---|
git clone <url> |
The machine doesn’t have the repo yet | Creates a new repo, once |
git fetch |
You already have the repo | Downloads new commits, updates only origin/* |
git pull |
You already have the repo | fetch + integrate into the current branch |
Re-cloning every time you want updates wastes time and throws away any unpushed branches and stashes in the old copy.
What git clone does under the hood, and shallow clones in CI
git clone does several things at once:
- Creates the folder and runs
git init. - Adds a remote called
originpointing at the URL. - Downloads all objects and history.
- Creates remote-tracking branches (
origin/main,origin/develop, …), a record of where the remote was at the last fetch. - Creates a local
mainthat tracksorigin/mainand checks it out.
Other branches have no local copy yet, but git switch develop creates a local branch tracking origin/develop for you.
2. A daily habit: commit, push, and Git as a backup
Key idea: every time you finish a lesson (or a piece of work), commit and push it right away. You get practice and an off-machine copy of your code at the same time.
- Practice.
add,commitandpushevery day turn into reflexes. Whether you join as an intern or a hire, people assume you already know Git. - An off-machine copy. Laptop broken, stolen, disk dead:
git cloneon another machine and carry on.
One-time setup on a new machine
Key idea: set your name, email, default branch, global gitignore and SSH key once per machine.
git config --global user.name "Nguyen Van A"
git config --global user.email "a@example.com" # match your GitHub email so commits are attributed to you
git config --global init.defaultBranch main
git config --global pull.rebase true # pull = fetch + rebase for personal branches
git config --global core.excludesFile ~/.gitignore_global # OS/IDE junk, not the project's business
ssh-keygen -t ed25519 -C "a@example.com" # create an SSH key, paste ~/.ssh/id_ed25519.pub into GitHub
ssh -T git@github.com # check the connection
- The project’s
.gitignore: what the project generates (target/,build/,.env). - The global gitignore: your machine’s junk (
.DS_Store,.idea/,*.iml), so you don’t repeat it in every repo.
Git is a backup, but only for what’s committed and pushed
Key idea: “I pushed it to Git, so it’s backed up” is only partly true. The five things below do not live on the remote.
| What | Why it isn’t backed up | What to do |
|---|---|---|
Uncommitted changes, files never git add-ed |
They only exist in the working directory | Commit early, even “wip” commits on your own branch |
| Commits not yet pushed | They only exist in your local repo | git status -sb shows [ahead 2]; push at the end of every session |
| Local branches never pushed | git push won’t push a branch with no upstream |
git push -u origin <branch> as soon as you create it |
git stash |
Stashes only exist locally | Don’t use the stash as long-term storage; commit to a branch |
Ignored files (.env, a local DB, large test data) |
Deliberately kept out of Git | Secrets go in a password or secret manager; data gets a script that regenerates it |
git status -sb # ## main...origin/main [ahead 2] -> 2 commits not pushed yet
git log --oneline origin/main..main # exactly the commits the remote doesn't have
git branch -vv # which branch tracks which remote, and which has no upstream
For company code, “it’s on GitHub” isn’t a backup policy. Remotes get lost too: a repo deleted by mistake, a locked account, someone force-pushing over history. Organisations usually turn on branch protection (no force pushes to main) and mirror repos somewhere else on a schedule.
Organisation-level backups: mirrors and git bundle
git clone --mirror git@github.com:acme/booking-service.git # a full copy of every ref
git -C booking-service.git remote update --prune # refresh it on a schedule (cron)
git bundle create booking-2026-11-17.bundle --all # pack the whole repo into one file
git clone booking-2026-11-17.bundle restored # restore from the bundle
Two more notes:
- Don’t commit large or generated files (
.jar,target/, videos, database dumps). Git keeps every version forever, so the repo grows and everyone who clones downloads all of it. For large binaries that genuinely need versioning, use Git LFS. - “wip” commits on your own branch are fine. The goal is not to lose work; clean them up with an interactive rebase before you open a pull request (see part 3).
3. Version numbers (Semantic Versioning) and tags
Key idea: SemVer reads a MAJOR.MINOR.PATCH version as a promise about compatibility. Each release is marked with a tag: a fixed pointer to one commit.
The slide shows a release timeline: a few 0.9.x betas, then 1.0.0, patches 1.0.1 to 1.0.3, 1.1, 1.2.x, and finally a jump to 2.0.0.
| Part | Bump it when | Example |
|---|---|---|
MAJOR |
You make a breaking change (change or remove an API, change a data format) | 1.4.2 → 2.0.0 |
MINOR |
You add a feature that stays backward compatible | 1.4.2 → 1.5.0 |
PATCH |
You only fix bugs, with no API change | 1.4.2 → 1.4.3 |
- Bumping a higher part resets the lower ones to zero (
1.4.2→2.0.0, not2.4.2). 0.y.zmeans unstable: the API can change at any time, so the0.9“beta” on the slide promises nothing yet.1.0.0is the first public API commitment.- Pre-release suffixes (
2.0.0-rc.1,1.0.0-beta.2) sort before the matching final release.
This ties back to Conventional Commits from part 1: fix → PATCH, feat → MINOR, ! or BREAKING CHANGE: → MAJOR. Tools like semantic-release read the commit history and pick the next number for you. In a pipeline, the Git tag v1.4.0 becomes the image’s SemVer tag and triggers the release: see CI/CD for a Spring Boot backend.
Unlike a branch, a tag doesn’t move when new commits arrive.
git tag -a v1.2.0 -m "Release 1.2.0: hotel search by price" # annotated tag
git tag # list tags
git show v1.2.0 # tagger, date, message, and the tagged commit
git push origin v1.2.0 # push a single tag
git push --follow-tags # push commits plus the annotated tags pointing at them
git checkout v1.2.0 # look at exactly what shipped (detached HEAD)
git describe # v1.2.0-3-g8a6504e: three commits past the nearest tag
Exercises
Exercise 1: A learning repo with a “finish a lesson, push it” habit
Task (paraphrased): Create a GitHub (or GitLab) repo for your course code. After each lesson, commit that lesson’s code and push it to the remote. Two goals: practise Git commands every day, and always have a copy of the code off your machine.
Solution to Exercise 1: create the repo, push every lesson, rehearse a restore
mkdir java-core-notes && cd java-core-notes
git init
cat > .gitignore <<'EOF'
target/
out/
*.class
.env
EOF
echo "# Java Core - code per lesson" > README.md
git add .gitignore README.md
git commit -m "chore: init repository"
gh repo create java-core-notes --private --source=. --remote=origin --push
# --- After each lesson ---
mkdir -p lesson15-hello-world # write HelloWorld.java in here
git add lesson15-hello-world
git commit -m "feat(lesson15): print hello world and compile with javac"
git push
# --- End of the day: make sure nothing lives only on this machine ---
git status -sb # no [ahead N], no stray files
git log --oneline origin/main..main # empty means everything is pushed
# --- "Lost laptop" drill: clone elsewhere and run it ---
git clone git@github.com:<user>/java-core-notes.git /tmp/restore-test
java /tmp/restore-test/lesson15-hello-world/HelloWorld.java
- The
.gitignoreexists from the very first commit, so.classfiles and.envnever sneak in. - One commit per lesson with a Conventional Commits message (
feat(lesson15): ...) turnsgit log --onelineinto a progress log. - The restore drill matters more than it looks: a backup you’ve never restored may not actually work.
What a senior would add:
- An alias to run before shutting the laptop:
git config --global alias.unpushed "log --branches --not --remotes --oneline"— lists unpushed commits on every branch, not just the current one. - A pre-commit hook that scans for secrets (gitleaks) so
.envnever gets pushed. git push --all originwhen the repo has many exercise branches.
Remember
- Explain how
clone,fetchandpulldiffer and when to use each. - Name the five things that are not backed up even after “I pushed to GitHub”, and the commands that show what hasn’t gone out.
- Know why
.gitignoredoesn’t untrack a committed file and what to do after committing a secret. - Read a SemVer number and map
fix/feat/!to the part that gets bumped. - Know to use annotated tags for releases, how to push them, and why you never move a published tag.
Interview questions
Junior
git fetch vs git pull?
fetch only downloads; pull downloads and then integrates into the current branch.
fetch updates the remote-tracking branches (origin/main) without touching your branches or files. pull is fetch plus a merge (or a rebase with --rebase).
.gitignore already lists .env, yet Git still reports changes to .env. Why, and how do you fix it?
.gitignore only applies to untracked files; .env was committed at some point, so Git keeps tracking it.
Remove it from the index with git rm --cached .env and commit; the file stays on disk. If .env holds secrets, they’re already in the history, so rotate them right away.
How is git clone different from git pull?
clone is a one-off for a machine without the repo; pull is for a repo you already have, to get new commits.
clone creates the folder, adds the origin remote, downloads the whole history and checks out the default branch. pull fetches the remote’s new commits and integrates them into your current branch.
How do you read version 2.3.1 under Semantic Versioning?
MAJOR.MINOR.PATCH: 2 is the major (breaking changes), 3 the minor (new backward-compatible features), 1 the patch (bug fixes only).
Moving to 2.4.0 gives you new features and is safe to upgrade; moving to 3.0.0 means reading the changelog, because something breaks. A 0.x version is considered unstable.
Mid
Is pushing to GitHub every day enough of a backup?
Only for what’s committed and pushed.
- Uncommitted changes, unpushed commits, local branches without an upstream, stashes and ignored files all stay on your machine.
git status -sbandgit log --branches --not --remotesshow what hasn’t gone out. - For an organisation’s code the remote can be lost too (a deleted repo, a force push over history): you want branch protection, scheduled mirrors or
git bundlecopies elsewhere, and an occasional real restore test.
What’s the difference between annotated and lightweight tags, and which should a release use?
Releases use annotated tags: their own object with a tagger, date, message and optional GPG signature; a lightweight tag is just a ref pointing at a commit.
Annotated tags give you traceability, and git describe and git push --follow-tags only use them by default. Remember: a plain git push doesn’t send tags, and never move a published tag to another commit; cut a new patch release instead.
Senior
How would you automate releasing versions from commit history, and what tends to go wrong?
Enforce Conventional Commits in CI, then let semantic-release or release-please compute the version, generate the changelog and create an annotated tag on the main branch.
The tool reads the commits since the last tag, works out the next SemVer version, generates the changelog, creates an annotated tag and builds the artifact from exactly that commit. commitlint in CI enforces the convention. What goes wrong:
- The CI job uses a shallow clone and can’t see previous tags (it needs
fetch-depth: 0). - Squash merges lose each commit’s type if PR titles don’t follow the convention.
- Someone deletes and recreates a tag, so one version points at two commits (turn on protected tags, treat tags as immutable).
- A breaking change isn’t marked with
!, so it only bumps the minor.
Useful phrases for interviews
- “I commit and push at the end of every session, so nothing important lives only on my laptop.”
- “Adding a file to .gitignore doesn’t untrack it; you have to remove it from the index with git rm –cached.”
- “Pushing to GitHub isn’t a complete backup: stashes, unpushed branches and ignored files stay local.”
- “On a new machine I clone once, and from then on I just fetch and pull.”
- “We follow semantic versioning, so a breaking change means a new major version, and every release is an annotated tag.”
- “Our CI uses a shallow clone for speed, but the release job fetches the full history so version detection works.”
Notes based on the Fundamentals of Backend course (Ronin Engineer) and the Hỏi Dân IT Java Core material, plus my own research, rewritten in my own words.