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
Tracking a project with commits
- Your first commits: working directory, staging, commit (25′)
- Reading your history and writing good commit messages (25′)
Branches and merging
- Branches: try things without breaking main (30′)
- A simple merge conflict, resolved by hand (25′)
GitHub: push and pull request
- Pushing your project to GitHub (25′)
- 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.
- Run
git init. This creates a hidden.gitfolder insiderecipesand turns it into a repository. You never touch that folder by hand; it is where Git keeps the whole history. - Run
git status. It says you are on branchmainand that there is nothing to commit — no commits exist yet. You may seemasterinstead ofmaindepending on your Git version and settings. This course (and GitHub) usesmain, so if you seemaster, rungit branch -m mainright after your first commit (step 6);git config --global init.defaultBranch mainmakes new repositories start onmain. - Create
notes.txtwith a line or two in it. This change is in the working directory only. - Run
git statusagain. Under "Untracked files" it listsnotes.txt: Git sees it, but it is not staged. - Run
git add notes.txt, thengit status. The file now sits under "Changes to be committed" — it is on the shelf. - Run
git commit -m "Add cooking notes". The-mflag carries the message describing the save. Git answers with a line like1 file changed, 2 insertions(+): that is your snapshot being written into history. If this is your first commit on this laptop, Git may stop withAuthor identity unknown: rungit config --global user.name "Your Name"andgit 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. - Run
git statusonce 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 addcopies 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 addcopies it to the staging area. git committurns what is staged into a permanent snapshot in history.git statusnames 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.