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
git init
– Initialize a new repository (creates a hidden .git
folder).
git clone <url>
– Copy a remote repo locally (sets up origin, pulls history).
git status
– See modified, staged, and untracked files.
git add <file>
/ git add .
– Stage changes for the next commit.
git commit -m "Message"
– Create a snapshot with a descriptive message.
git log
– View commit history. Add --oneline
or --graph
for compact view.
git branch <name>
/ git switch -c <name>
– Create (and switch to) a new branch.
git switch <name>
– Switch between branches.
git merge <branch>
– Merge another branch into the current one.
git remote add origin <url>
– Link a remote repo named "origin".
git push -u origin <branch>
– Push commits to the remote and set upstream.
git pull origin <branch>
– Fetch and merge remote changes into local branch.
git stash
– Temporarily store uncommitted changes.
git reset HEAD~1
– Undo the last commit (keep changes staged or unstaged).
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
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.
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.
- Make small, focused commits with clear messages.
- Create branches for every feature or fix.
- Pull often before you push to avoid surprises.
- Embrace conflicts – resolving them builds understanding.
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! ⚓