Git

Git - Basic

Page 2 of 2

Branches Basics

A is an independent line of development - a way to work on something (a new feature, an experiment) without touching the stable version of the project. The default branch is usually called main.

git branch feature-login          # create a new branch
git checkout feature-login        # switch to it
git switch feature-login          # newer, clearer equivalent to checkout
git checkout -b feature-login     # create and switch in one step

Branches are cheap and fast in Git - creating one doesn't copy any files, it just creates a new named pointer to a commit. This is why Git culture encourages branching constantly: for every feature, every bug fix, every experiment, rather than working directly on main.

Right after creating the branch, both pointers sit on the same commit:

Commit 1

Commit 2

Commit 3

main

feature-login


Merging Basics

Once work on a branch is done, you bring it back into another branch (usually main) with a .

git checkout main
git merge feature-login

If main hasn't changed since you branched off, Git does a - it just moves the main pointer forward to your latest commit, no new commit needed. If main has moved (someone else merged something), Git creates a that ties both histories together, with two parent commits instead of one.

Understanding this distinction matters later: a fast-forward merge leaves a perfectly linear history, while a merge commit visibly shows where two lines of work rejoined.

A merge commit looks like this:

mainfeature-loginC1C2C3C4Merge commit

Ignoring Files

Not everything in a project folder belongs in version control - build output, dependency folders, editor settings, secrets. A .gitignore file tells Git what to never track:

node_modules/
*.log
.env
dist/
git status   # ignored files simply won't show up as untracked

If a file is already tracked before you add it to .gitignore, ignoring it won't stop Git from tracking it - you have to explicitly remove it from tracking (git rm --cached) first. This surprises a lot of people the first time.


Undoing Simple Mistakes

Mistakes are routine, and Git has gentle ways to undo the common ones at this stage.

git restore file.txt          # discard uncommitted changes to a file, back to last commit
git restore --staged file.txt # unstage a file without discarding your edits
git commit --amend            # fix the message (or add forgotten changes) to your last commit

git restore is the newer, clearer replacement for older checkout-based undo commands, and it's worth learning first because the split between "discard my edits" and "just unstage" maps directly onto the three-states idea from earlier. Deeper undo tools - resetting whole commits, reverting pushed history - come later once you understand why they need more care.

    Welcome to OpsQuiz!

    Real scenario-based DevOps questions, hands-on practice, and clear explanations for every answer.