The Git Master Handbook: From Setup to Real-World Use
π 1. Initial Setup β One-Time Only
π Scenario: You’re using Git for the first time on your machine.
Set your user details (required for commits):
git config --global user.name "Alice Dev"
git config --global user.email "alice@example.com"
Optional settings:
git config --global core.editor "code --wait" # Use VS Code as Git editor
git config --global pull.rebase false # Pull uses merge by default
git config --global init.defaultBranch main # Default branch is main, not master
π₯ 2. Cloning a Repository
π Scenario: Youβre joining a team or starting a project from GitHub or Azure DevOps.
git clone https://github.com/org/project.git
cd project
π§ Analogy: Cloning is like downloading a working copy of a shared Google Doc where Git tracks every change.
πΏ 3. Branching β Working in Isolation
π Scenario: Youβre working on a new feature or fixing a bug.
Always start from the latest main:
git checkout main
git pull origin main
git checkout -b feature/signup-form
feature/signup-form
is your personal workbench.- You can commit freely without affecting the main product.
π§ Analogy: You forked a Google Doc to make your own edits before merging back.
π 4. Status, Add, Commit β Making and Saving Changes
Youβve made changes. Whatβs going on?
git status
Youβll see:
- β Tracked files β Git knows these; changes will be recorded.
- β οΈ Untracked files β Git has no record of them yet.
- π Modified files β You changed something but havenβt committed yet.
Stage changes (prepare them for commit):
git add file.txt # Stage one file
git add . # Stage all changes
Commit:
git commit -m "Add signup validation"
π§ Analogy: Think of add
as selecting slides for a presentation, and commit
as saving the version of that presentation.
π§³ 5. Stashing β Save Without Committing
π Scenario: Youβre halfway through work and need to switch tasks or rebase.
Basic stash:
git stash -m "WIP: signup flow"
Stash including untracked files:
git stash -u -m "WIP: with new files"
Get back stashed changes:
git stash pop # Pops and applies top stash
git stash list # View all stashes
git stash drop stash@{0} # Delete a specific stash
π§ Analogy: Itβs like saving a draft email without sending it.
π 6. Fetch, Merge, Rebase β Sync with Main
π Scenario: Youβve been working for days. The main
branch is updated by others.
π git fetch
git fetch origin
- Updates your local view of
origin/main
- Doesnβt change your working branch
π§ Analogy: Refreshing your inbox without replying yet.
π git merge origin/main
git merge origin/main
- Combines your branch with the latest main.
- Keeps history with a merge commit.
π§ When to use:
- Shared branches (safe)
- You want to preserve all commit history
π§ Analogy: Combining two versions of a doc with a sticky note saying “merged here”.
π git rebase origin/main
git rebase origin/main
- Replays your work on top of latest main
- Makes history look linear
π§ When to use:
- Clean up before opening a Pull Request
- You havenβt pushed yet
π§ Analogy: You move your edits to a new copy of the doc as if you started fresh.
βοΈ 7. Merge & Rebase Conflicts
π Scenario: You and a teammate changed the same file or line.
Git will say:
CONFLICT (content): Merge conflict in file.py
Youβll see conflict markers like:
<<<<<<< HEAD
Your code
=======
Their code
>>>>>>> origin/main
Fix the file, then:
git add file.py
git rebase --continue # or git merge --continue
π 8. Pushing and Pull Requests
π Scenario: You’re done with your feature and want it reviewed.
git push origin feature/signup-form
Then go to GitHub/Azure and open a Pull Request (PR) into main
.
π§ Analogy: Submitting your edited draft for peer review.
β 9. Cleaning Up
After your PR is merged:
git checkout main
git pull origin main
git branch -d feature/signup-form # local
git push origin --delete feature/signup-form # remote
π§° 10. Advanced but Useful Commands
Command | Description |
---|---|
git log --oneline --graph |
View commit history graph |
git diff |
Show unstaged changes |
git diff --staged |
Show staged changes |
git reset file.txt |
Unstage a file |
git reset --hard |
Reset everything to last commit (β οΈ destroys changes) |
git clean -fd |
Delete all untracked files & folders (β οΈ irreversible) |
git cherry-pick <commit> |
Apply a single commit to your branch |
π§ͺ Real-World Scenarios
π Scenario 1: Update your branch with latest main (safe way)
git stash -u -m "WIP before update"
git fetch origin
git rebase origin/main
git stash pop
If you hit a conflict, resolve it and continue with:
git add conflicted-file
git rebase --continue
π§ Scenario 2: You want to stop working and pick up later
git stash -u -m "Login form in progress"
# go to another branch or pull updates
# come back
git stash pop
π Scenario 3: Starting a new feature
git checkout main
git pull origin main
git checkout -b feature/payment-method
π Scenario 4: Fix a critical bug on production
git checkout -b hotfix/critical-crash
# fix it
git add .
git commit -m "Fix crash"
git push origin hotfix/critical-crash
Open a PR into main
or release
and get it deployed.
π€ Scenario 5: You Made Changes, Took a Break, and Came Back
π Situation:
You were working on a feature. You made some changes locally but didnβt commit or push. You went on a weekend or short leave. Now you’re back.
Other teammates may have updated the main
branch while you were away. You need to sync your branch with the latest main
before continuing.
π§ First Steps When You’re Back:
1. Check if you have uncommitted changes:
git status
- If yes: π§³ stash your work to avoid losing it
git stash -u -m "WIP before syncing"
2. Fetch the latest changes from the remote:
git fetch origin
3. Rebase your branch on top of the updated main
:
git rebase origin/main
This applies your commits on top of the new changes from
main
4. Bring back your stashed changes (if any):
git stash pop
π Example Flow:
git status # You see uncommitted changes
git stash -u -m "WIP: login fix"
git fetch origin # Get the latest main
git rebase origin/main # Replay your branch on top
git stash pop # Restore your work
π§ Why this matters:
- Keeps your branch up to date
- Reduces conflicts later during PR
- Prevents pushing outdated code
- Makes your PR easier to review
β Pro Tip: Always fetch and rebase when returning from a break. Itβs a small habit that saves hours of merge pain later.
π§ Git Mental Model Cheatsheet
You want to⦠| Do this |
---|---|
Save in-progress changes | git stash |
Create a new branch | git checkout -b new-branch |
See what changed | git status / git diff |
Save changes permanently | git add + git commit |
Sync with remote | git fetch + rebase or merge |
Submit code for review | git push + open PR |
Clean working directory | git clean -fd (careful!) |
Start over | git reset --hard (β οΈ risky) |
π 11. Renaming, Deleting, Reverting, and Resetting
These are essential for fixing mistakes, cleaning your repo, or handling files during refactors.
βοΈ Rename a File
git mv oldname.py newname.py
git commit -m "Rename oldname.py to newname.py"
π§ Analogy: Like moving a file in Google Drive β Git sees it’s the same content, just with a new name.
ποΈ Delete a File
git rm obsolete.js
git commit -m "Remove unused script"
- Removes file from version history from this commit onward
- File is still in past commits β Git never forgets unless forced
π Undo with git revert
(Safe Method)
git revert <commit-hash>
β Safest way to undo a commit β Works even after you’ve pushed
- Creates a new commit that undoes the changes
- Preserves history
π§ Analogy: You’re adding a new note saying βActually, undo what I just did.β
π₯ Reset with git reset
(Dangerous But Powerful)
π‘ Soft Reset (keep changes, undo commit)
git reset --soft HEAD~1
- Last commit is removed
- Changes stay staged
π‘ Mixed Reset (default, keep changes unstaged)
git reset HEAD~1
π΄ Hard Reset (dangerous!)
git reset --hard HEAD~1
- Removes last commit
- Deletes any unstaged changes
- β οΈ Use only if you’re 100% sure
π§ Analogy: Like tearing out a page and throwing it away β no record unless someone copied it.
π§ͺ Scenario: You Made a Bad Commit
Use revert
if you already pushed:
git revert 7a8b9c1
Use reset
if it’s local only and you want to start over:
git reset --hard HEAD~1
π‘ Bonus: Viewing Commit History
git log --oneline --graph --decorate --all
- Visualize commit history like a tree
- Helps understand merges, reverts, and branch relationships
π Summary: Undo Commands
Task | Command | Safe to Use After Push? | Keeps History? |
---|---|---|---|
Rename file | git mv old new |
β Yes | β Yes |
Delete file | git rm file |
β Yes | β Yes |
Revert a commit | git revert <hash> |
β Yes | β Yes |
Undo commit (soft) | git reset --soft HEAD~1 |
β No | β No |
Reset and delete changes | git reset --hard HEAD~1 |
β No | β No |
β Final Pro Tips
- Use
rebase
before PRs for clean history - Use
merge
on shared branches - Always
stash
if you have uncommitted changes before pulling - Avoid
--force
unless you understand rebase - Small, frequent commits > giant ones
- Use
revert
if you already pushed the commit - Use
reset
only for local rewrites - Avoid
reset --hard
unless youβve stashed or backed up work - Git doesn’t lose data until you tell it to β but
reset --hard
will destroy uncommitted edits