Skip to main content

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.

Tell Git who you are (once per computer)
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:

Create a new repository
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.

Copy an existing repository onto your computer
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:

Show the current state of your 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.

Stage a specific page
git add books.html
Stage everything at once
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.

Save your staged work with a 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:

View the commit history
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.

Create a new branch
git branch dark-theme
Move onto that branch (start working in that copy)
git switch dark-theme
Or do both in one step
git switch -c dark-theme
See all branches (the * marks the one you are on)
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:

First return to main, then merge the branch into it
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:

Move your branch's pages on top of main
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.

Send your commits to the central library
git push
Fetch the newest pages from the central library
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:

View the notebook as it was at commit 08f9a11
git checkout 08f9a11

When she finds the missing page, she can restore just that file from history:

Restore a single file from an old commit
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 amgit config --global user.name "Name"
Start a new repositorygit init
Copy an existing onegit clone <url>
See what changedgit status
Stage pagesgit add .
Save with a messagegit commit -m "message"
Read the diarygit log --oneline
Make my own copygit switch -c branch-name
Combine copiesgit switch main && git merge branch-name
Straighten historygit rebase main
Send to the librarygit push
Get the latestgit pull
Travel back in timegit checkout <commit-hash>

Now that you know the toolbox, learn how the friends use it together every day in Workflows.