Git Commands — The Librarian's Toolbox
Back with our five friends — Riya, Arjun, Maya, Kabir, and Simran — who are building a college library app called Campus Library. They share one central library, and every friend keeps their own copy of the notebook. Each command below is just one small action in that library.
0 · Setup — Opening the Library Register
Before anyone writes in the notebook, the librarian needs to know who is writing. Git stamps every page with your name and email.
git config --global user.name "Riya Sharma"
git config --global user.email "riya@campuslibrary.dev"
Then, to open a new library register for a project that does not exist yet:
git init
:::tip Remember
git init happens only once — it creates the hidden .git folder, which is the librarian's register.
:::
1 · git clone — Taking Your Own Copy of the Notebook
The library already has the Campus Library notebook. Instead of writing by hand from memory, each friend simply copies the whole notebook into their own bag.
git clone https://github.com/campuslibrary/app.git
You now have a full notebook — every page, every version, every note — on your own desk.
:::tip Remember
clone is how you start working on any existing project. One command = the entire history, not just the latest page.
:::
2 · git status — Asking the Librarian What Changed
After you edit a page, the librarian can always tell you what is different compared to the notebook:
git status
On branch main
Changes not staged for commit:
modified: books.html
:::tip Remember
git status is your compass. Run it whenever you are lost — it always tells you where you are.
:::
3 · git add — Placing Pages on the Librarian's Desk
You do not glue your work into the notebook yet. First you place the pages on the desk so the librarian can see them. This is called staging.
git add books.html
git add .
:::warning Common mistake
Forgetting git add means your changes are not on the desk. git commit would save nothing new — and beginners always wonder why their edits "disappeared."
:::
4 · git commit — Gluing Pages In with a Sticky Note
Now the librarian glues your pages into the notebook and sticks a sticky note on them saying what you did. That note is your commit message.
git commit -m "Add the weekly recommended books page"
[main 7a3f9b2] Add the weekly recommended books page
1 file changed, 24 insertions(+)
:::tip Remember
git add = "here are my pages, please look" → git commit = "now glue them in forever." The commit message should say what you did and why, not just "stuff".
:::
5 · git log — Reading the Librarian's Diary
Every commit is a diary entry. The librarian can replay the entire story of the notebook:
git log --oneline
7a3f9b2 (HEAD -> main) Add the weekly recommended books page
c41d2ea Fix catalogue header color
08f9a11 Add login page
:::tip Remember
Each entry starts with a long code like 7a3f9b2. That is the commit hash — the sticky note's unique ID. You will use it for time travel later.
:::
6 · git branch and git switch — Each Friend's Own Copy
Maya wants to try a bold idea: a dark theme for the app. She does not want to risk ruining the main notebook, so the librarian gives her her own copy — a branch.
git branch dark-theme
git switch dark-theme
git switch -c dark-theme
git branch
* dark-theme
main
:::tip Remember
Think of branches as parallel versions of the notebook. main is the official one everyone reads; friends experiment safely in their own copies.
:::
7 · git merge — Combining Notebooks Page by Page
Maya's dark theme works beautifully. Now she hands her notebook to the librarian, who combines it with the main notebook page by page:
git switch main
git merge dark-theme
Updating 08f9a11..5b3e1c0
Fast-forward
styles.css | 42 ++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
If both notebooks changed the same page in different ways, the librarian pauses and asks the two friends to sit together and decide. That is a merge conflict — you will resolve one in the exercises.
:::tip Remember
git merge joins two branches. It never deletes anything — both branches' history is preserved.
:::
8 · git rebase — Replaying Pages on Top of the Newest Ones
Imagine Kabir started a branch, but meanwhile Arjun added three new pages to main. Kabir's branch is now based on old pages. The librarian offers to take Kabir's pages and replay them on top of the newest main pages, so the story reads in one straight line:
git switch kabir-feature
git rebase main
Successfully rebased and updated refs/heads/kabir-feature.
Before rebase (a fork in the story): After rebase (one straight line):
A---B kabir-feature A---B kabir-feature
/ /
...o---C---D main ...o---C---D main
:::tip Remember Rebase rewrites your commits onto newer pages. Great for keeping history clean before sharing. Never rebase pages that are already in the shared library (see Workflows). :::
9 · git push and git pull — The Central Library
Working at home, friends commit to their own notebook. But the central library is the source of truth everyone reads.
git push
git pull
:::warning Common mistake
You cannot push a branch that has no "official" copy yet — the first time needs a friend: git push -u origin my-branch. And always git pull before you push, or you may push on top of stale pages.
:::
10 · git checkout — Time Travel
Riya accidentally removed the weekly recommended books page. No panic — the librarian saved every version. She can travel back to the commit where it still existed:
git checkout 08f9a11
When she finds the missing page, she can restore just that file from history:
git checkout 08f9a11 -- books.html
:::tip Remember As long as it was committed, nothing is truly lost. That is the superpower Git gives you. :::
Cheat Sheet
| I want to… | Command |
|---|---|
| Tell Git who I am | git config --global user.name "Name" |
| Start a new repository | git init |
| Copy an existing one | git clone <url> |
| See what changed | git status |
| Stage pages | git add . |
| Save with a message | git commit -m "message" |
| Read the diary | git log --oneline |
| Make my own copy | git switch -c branch-name |
| Combine copies | git switch main && git merge branch-name |
| Straighten history | git rebase main |
| Send to the library | git push |
| Get the latest | git pull |
| Travel back in time | git checkout <commit-hash> |
Now that you know the toolbox, learn how the friends use it together every day in Workflows.