Welcome! This guide helps you master Git from zero. Through simple analogies, step-by-step examples, and under-the-hood insights, you’ll learn how to track changes, branch safely, collaborate, and resolve conflicts like a seasoned captain.

Quick Reference

Ship’s Timeline

flowchart LR
  WD[Working Directory] -- stage --> ST[Staging Area]
  ST -- commit --> LP[Local Repository]
  LP -- push --> RP[Remote Repository]
  RP -- pull --> WD

Initializing a Repository

Imagine finding an uncharted island and planting your flag. git init turns your folder into a ship’s log by creating a hidden .git folder.

git init

Under the hood, Git creates an internal database (.git) that tracks all snapshots (objects) and pointers (refs). This is your project’s time machine – no log entries without it.

Staging & Committing

Staging is packing cargo; committing is sealing the crate and dating it in your logbook.

Stage Changes (git add)

git add <file>

Puts file changes into the staging area. Git snapshots the content in its index, ready for the next commit.

Commit Snapshot (git commit)

git commit -m "Describe changes"

Records a complete snapshot of staged files, along with author, timestamp, and message. Each commit is a waypoint in your voyage.

Branching & Merging

Branches are parallel decks on your ship – experiment on one deck while keeping the main deck stable. When ready, merge decks back together.

Create & Switch Decks

git branch <name>
git switch <name>

git branch labels a commit; git switch sails you to that deck (updates working files).

Merge Decks (git merge)

git merge <branch>

Performs a three-way merge from the common ancestor. If lines overlap, Git flags a conflict for you to resolve.

Working with Remotes

Your remote repository is the fleet’s mothership. Push your commits aboard and pull updates back to stay in sync.

git remote add origin <url>
git push -u origin main
git pull origin main

Add origin to register the URL. push uploads, pull fetches and merges remote changes.

Resolving Conflicts

When two sailors rewrite the same log entry, Git flags a conflict. You’ll see markers and must choose the final text.

# Edit out markers <<<<<<< ======= >>>>>>>
git add <file>
git commit -m "Resolve conflict in <file>"

Conflict markers show both versions. Edit the file to the intended result, then stage and commit to finish the merge.

Conclusion & Next Steps

You’ve navigated Git’s core maneuvers: init, add, commit, branch, merge, push, pull, and resolve conflicts. Practice these until they’re second nature.

With these tools, you wield a time machine for your code and a fleet for collaboration. Fair winds and smooth seas on your Git voyage! ⚓