← Back to the catalog

Git and GitHub for Beginners: Commits, Branches, Pull Requests

Track a project with Git from your first commit: stage changes, write clear commit messages, work on branches and resolve a simple merge conflict. Then push your project to GitHub, sign in with a token or browser login, and open, review and merge your first pull request.

Language
English
Lessons
6
Minutes
155

Price: · The subscription includes the whole catalog at no extra cost.

Course outline

  1. Tracking a project with commits

    1. Your first commits: working directory, staging, commit (25′)
    2. Reading your history and writing good commit messages (25′)
  2. Branches and merging

    1. Branches: try things without breaking main (30′)
    2. A simple merge conflict, resolved by hand (25′)
  3. GitHub: push and pull request

    1. Pushing your project to GitHub (25′)
    2. Opening and reviewing a pull request (25′)

Free first lesson

Your first commits: working directory, staging, commit

Three places your work lives

You have Git on your laptop and a terminal you use a little — that is all this lesson needs. Picture a project folder on your machine, say recipes, with a file notes.txt in it. When you edit that file, the change happens in the working directory: the ordinary folder you see, with no help from Git. Git records nothing until you ask.

Saving a change means moving it twice. git add notes.txt puts a copy of the file, exactly as it is at that moment, into the staging area — a shelf where you gather what the next save will contain. The shelf can hold several files at once: stage two or three, and one commit saves them together. Then git commit wraps everything on that shelf into a commit: a permanent, named snapshot stored in Git's history. In the flow diagram, follow the file's trip from folder to shelf to snapshot.

Why does Git make you stage files first, instead of saving everything at once? Watch for the answer as you run the steps below.

Your first commit, step by step

Open your terminal in a small project folder — create recipes on your desktop if you want a fresh one. Git only ever needs this setup once per folder.

  1. Run git init. This creates a hidden .git folder inside recipes and turns it into a repository. You never touch that folder by hand; it is where Git keeps the whole history.
  2. Run git status. It says you are on branch main and that there is nothing to commit — no commits exist yet. You may see master instead of main depending on your Git version and settings. This course (and GitHub) uses main, so if you see master, run git branch -m main right after your first commit (step 6); git config --global init.defaultBranch main makes new repositories start on main.
  3. Create notes.txt with a line or two in it. This change is in the working directory only.
  4. Run git status again. Under "Untracked files" it lists notes.txt: Git sees it, but it is not staged.
  5. Run git add notes.txt, then git status. The file now sits under "Changes to be committed" — it is on the shelf.
  6. Run git commit -m "Add cooking notes". The -m flag carries the message describing the save. Git answers with a line like 1 file changed, 2 insertions(+): that is your snapshot being written into history. If this is your first commit on this laptop, Git may stop with Author identity unknown: run git config --global user.name "Your Name" and git config --global user.email "you@example.com" once, then repeat the commit. That name and email are stored in every commit and become public if you push to a public GitHub repository; GitHub offers a private no-reply address under Settings → Emails.
  7. Run git status once more: a clean tree. The change is now a snapshot in history.

The answer to the why question: staging is a choosing step. If you had edited two files but wanted them in two separate saves, you would stage and commit one, then stage and commit the other. The shelf decides what each save contains — nothing else does.

Second save — you write the commands

Now a second save on the same project. You open notes.txt, add a new line — "try less salt" — and save the file. That is the setup; it matches step 3 above, and the change is back in the working directory.

Your turn: which two commands record this change, and what does git status say between them? Write the commands down before you run anything.

Run your two commands, with git status in between, and check that the result matches what you wrote.

What trips people up at first

  • Skipping git add. Git answers "no changes added to commit": a commit records the shelf, not the folder.
  • Editing after staging. git add copies the file at that moment; newer edits stay unstaged until you re-run it.
  • git add . blindly stages every change at once, so one commit can no longer hold one idea.

What to keep

  • A change stays in the working directory until git add copies it to the staging area.
  • git commit turns what is staged into a permanent snapshot in history.
  • git status names the area each file is in at every step.

Walk one change through, and name the areas

Pick a project folder of yours and walk one change through the three areas, running git status after each command. Write down what it reports before git add, after it, and after the commit.

working directory
Your ordinary project folder, where you edit files. Git records nothing there until you ask.
staging area
The holding area where git add gathers the exact changes the next commit will contain.
commit
A permanent, named snapshot of everything staged, stored in Git's history with its message.
repository
A folder tracked by Git, created by running git init once inside it.