Skip to article content
FrontendXLesson 01 / 16

Git version control fundamentals

Move from a laptop shell to a safe, observable delivery pipeline: version control, Linux processes and permissions, container images, networking, configuration and secrets, deployment, health checks, observability, rollback, reproducibility and security hardening.

beginner · about 50 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Describe the commit graph and why a branch is only a movable pointer.
  • Create, switch and merge branches without losing work.
  • Choose between reset, revert and restore when undoing a change.

Overview

#

Git stores snapshots of your project as a directed graph of commits. Each commit points to its parent, so history is a chain you can walk backwards. A branch is nothing more than a movable label that points at one commit. Understanding that one idea removes most Git confusion.

The three areas

Work moves through three areas: the working tree (your files), the staging area or index (what the next commit will contain), and the repository (committed history). git add copies changes into the index; git commit records the index as a new commit.

snippet.bash
git status                 # what changed in each area
git add src/app.js         # stage one file
git add -p                 # stage selected hunks
git commit -m "Add retry"

Branches and merges

A branch is a pointer. Creating one is instant and copies nothing.

snippet.bash
git switch -c fix/retry-timeout   # create and switch
git commit -am "Cap retries at 3"
git switch main
git merge fix/retry-timeout       # fast-forward or merge commit

When both branches changed the same lines, Git reports a conflict and marks the file. You edit the file to the version you want, git add it, and git commit the merge.

Undoing work

  • git restore <file> discards working-tree changes for a file.
  • git restore --staged <file> unstages while keeping the edit.
  • git revert <commit> creates a new commit that reverses an old one; safe on shared branches.
  • git reset --soft HEAD~1 moves the branch back but keeps changes staged.
  • git reset --hard HEAD~1 moves the branch back and discards changes; never use it on commits you already pushed.

Common pitfalls

  • Committing secrets or large binaries; once pushed they stay in history even after deletion.
  • Running reset --hard on a shared branch, which rewrites history other people already pulled.
  • Treating git pull as always safe; it can create merge commits or conflicts you did not expect.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    You committed to main by mistake and have not pushed. Which command keeps the change but moves it off main so you can put it on a branch?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.bash
    git log --oneline -1   # shows the accidental commit on main
    
    Choose the best answer
  2. Exercise 2Implement

    Write the commands to create a branch named chore/pin-node, commit a change to .nvmrc, and push it with an upstream tracking branch.

    starter.bash
    # create branch, stage .nvmrc, commit, push with upstream
    

    Reveal the reference answer
    reference.bash
    git switch -c chore/pin-node
    git add .nvmrc
    git commit -m "Pin Node version"
    git push -u origin chore/pin-node

    Authored expected output: A new remote branch chore/pin-node tracking the local branch, with one commit.

    switch -c creates and checks out the branch. add and commit record the change. push -u sets the upstream so later git push and git pull work without arguments.

  3. Exercise 3Debug

    A teammate says their push was rejected with a non-fast-forward error after someone else pushed to the same branch. What happened, and what is the safe fix?

    starter.text
    ! [rejected] main -> main (non-fast-forward)
    

    Reveal the reference answer
    reference.text
    The remote has commits the local branch does not. Fetch and integrate them, then push: git pull --rebase origin main, resolve any conflicts, then git push.

    Authored expected output: Local branch rebased on top of the remote commits, then pushed cleanly.

    A non-fast-forward rejection means the remote moved ahead. Rebasing (or merging) brings those commits in so the push becomes a fast-forward. Force-pushing would overwrite the teammate's work and is the wrong fix on a shared branch.

Common misconceptions

#
  • A branch is a full copy of the project: it is a pointer to a commit, so creating branches is cheap.
  • reset and revert are the same: reset moves your branch and can rewrite history, revert adds a new reversing commit and is safe to share.

Follow-up practice

#
  • devops-linux-shell

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.