McTaba Labs logo
By Bonaventure Ogeto|

Git and GitHub for Beginners: The First Ten Commands

Git is a version control system that tracks every change you make to your code, letting you undo mistakes, work on features without breaking the main project, and collaborate with other developers. GitHub is a website that hosts your Git repositories online. You need ten commands to get started: init, add, commit, status, log, branch, checkout, merge, push, and pull.

Why you need Git (even for solo projects)

Without Git, developers used to do this: project-final.zip, project-final-v2.zip, project-ACTUAL-final.zip, project-final-USE-THIS-ONE.zip. If you have ever had a folder like that, you already understand the problem Git solves.

Git keeps a complete history of every change you make. You can:

  • See exactly what changed, when, and why.
  • Undo a change that broke something, even weeks later.
  • Work on a new feature without touching the stable version.
  • Collaborate with other developers without overwriting each other's work.

Every developer job requires Git. Every open-source project uses Git. There is no way around it, so let us get started.

Installing Git and setting up your identity

Check if Git is already installed:

git --version
# git version 2.43.0 (or similar)

If not installed, get it:

# Ubuntu/Debian
sudo apt install git

# macOS (comes with Xcode Command Line Tools)
xcode-select --install

# Windows: download from https://git-scm.com/download/win

Set your name and email. These appear in every commit you make:

git config --global user.name "Wanjiku Mwangi"
git config --global user.email "wanjiku@example.com"

Use the same email you will use for your GitHub account so your commits link to your profile.

Commands 1 to 5: working locally

Let us build a small project to learn each command. We will create a simple to-do list app.

1. git init: Create a new Git repository.

mkdir todo-app
cd todo-app
git init
# Initialized empty Git repository in /home/wanjiku/todo-app/.git/

This creates a hidden .git folder that stores all of Git's tracking data. Do not delete it.

2. git status: See what has changed.

# Create a file first
echo "# Todo App" > README.md

git status
# Untracked files:
#   README.md

Git tells you README.md exists but is not being tracked yet.

3. git add: Stage files for the next commit.

# Add a specific file
git add README.md

# Or add everything at once
git add .

Staging is like putting items in your shopping basket before you check out. You are telling Git "include this in the next commit."

4. git commit: Save a snapshot of your staged changes.

git commit -m "Add README with project title"
# [main (root-commit) a1b2c3d] Add README with project title
#  1 file changed, 1 insertion(+)

The -m flag lets you write the commit message inline. A good commit message says what you did and why. "Fix login bug" is good. "Update code" is not.

5. git log: See the history of commits.

git log --oneline
# a1b2c3d Add README with project title

Each commit has a unique hash (like a1b2c3d), the author, the date, and the message. This is your project's timeline.

Commands 6 to 8: branching and merging

Branches let you work on a feature without touching the main code. Think of it like writing in a separate notebook, and only copying your notes to the main notebook once you are happy with them.

6. git branch: Create a new branch.

# List existing branches
git branch
# * main

# Create a new branch
git branch add-tasks

7. git checkout: Switch to a different branch.

git checkout add-tasks
# Switched to branch 'add-tasks'

# Shortcut: create and switch in one command
git checkout -b add-tasks

Now make some changes on this branch:

# Create a tasks file
echo "Buy groceries" > tasks.txt
git add tasks.txt
git commit -m "Add first task"

8. git merge: Bring changes from one branch into another.

# Switch back to main
git checkout main

# Merge the add-tasks branch into main
git merge add-tasks
# Updating a1b2c3d..e4f5g6h
# Fast-forward
#  tasks.txt | 1 +
#  1 file changed, 1 insertion(+)

The changes from add-tasks are now in main. This is the core workflow: branch off, make changes, merge back.

Commands 9 to 10: working with GitHub

So far everything has been on your computer. GitHub lets you store your code online, back it up, and share it with others.

First, create a repository on GitHub (github.com, click "New repository"). Then connect your local project to it:

# Add GitHub as a remote (do this once)
git remote add origin https://github.com/wanjiku/todo-app.git

9. git push: Upload your commits to GitHub.

git push -u origin main
# Enumerating objects: 6, done.
# Counting objects: 100% (6/6), done.
# Writing objects: 100% (6/6), 450 bytes | 450.00 KiB/s, done.
# Branch 'main' set up to track remote branch 'main' from 'origin'.

The -u flag sets the default remote branch. After this, you can just run git push without the extra arguments.

10. git pull: Download changes from GitHub to your computer.

git pull
# Already up to date.
# (or it downloads new commits if someone else pushed changes)

Use git pull before starting work each day to make sure you have the latest code, especially when working with a team.

The daily workflow in practice

Here is the workflow you will follow for almost every feature you build:

# 1. Pull the latest changes
git pull

# 2. Create a branch for your feature
git checkout -b feature/user-profile

# 3. Write code, make changes
# ... edit files ...

# 4. Check what changed
git status
git diff

# 5. Stage and commit
git add .
git commit -m "Add user profile page with avatar upload"

# 6. Push your branch to GitHub
git push -u origin feature/user-profile

# 7. Open a Pull Request on GitHub for review
# 8. After approval, merge into main

This is the workflow used at companies of every size, from two-person startups to large engineering teams. The tools and review process may vary, but these ten commands stay the same.

Common mistakes beginners make:

  • Committing node_modules or .env files. Add a .gitignore file to exclude them.
  • Writing vague commit messages like "fix stuff" or "update." Be specific.
  • Working directly on main instead of creating a branch. Always branch.
  • Forgetting to pull before pushing. This causes merge conflicts that are easily avoided.

Frequently Asked Questions

What is the difference between Git and GitHub?
Git is the version control software that runs on your computer and tracks changes to your files. GitHub is a website that hosts Git repositories online and adds collaboration features like pull requests, issues, and project boards. You can use Git without GitHub (using GitLab, Bitbucket, or no remote at all), but most developers use both together.
What should I put in my .gitignore file?
At minimum: node_modules/, .env files (they contain secrets), build output folders (dist/, .next/), and OS files (.DS_Store on Mac). GitHub provides .gitignore templates for most languages when you create a new repository.
How do I fix a merge conflict?
A merge conflict happens when two branches change the same line of a file. Git marks the conflict in the file with arrows (<<<, ===, >>>). Open the file, decide which version to keep (or combine both), remove the conflict markers, then stage and commit. It looks scary the first time, but it becomes routine.
Should I commit after every small change?
Commit when you complete a logical unit of work: a function, a bug fix, a component. Each commit should leave the code in a working state. Committing too rarely makes it hard to undo specific changes. Committing after every single line is noisy and unhelpful.

Ready to build real-world apps?

Join the McTaba Labs full-stack marathon. Ship 8 production apps with M-Pesa, USSD, and WhatsApp integrations, and get career support until placement.

See Programs