Installing and setting up Git
Before your first commit, install Git and tell it who you are. This takes five minutes and you only do it once per computer.
Install
| System | How |
|---|---|
| Windows | Download from git-scm.com, run the installer. The defaults are fine; choose VS Code as the default editor if you use it. You also get Git Bash, a Linux-style terminal. |
| macOS | Open Terminal and run git --version. If it isn't installed, macOS offers to install the developer tools. Or brew install git. |
| Ubuntu / Debian | sudo apt update && sudo apt install git |
| Android | The Termux app: pkg install git (yes, you can learn Git on a phone) |
Check it worked:
git --versionTell Git who you are
Every commit records an author name and email. Use the same email as your GitHub account so your commits link to your profile.
git config --global user.name "Amina Hassan"
git config --global user.email "amina@example.com"Recommended settings
git config --global init.defaultBranch main # new repos start on "main"
git config --global core.editor "code --wait" # use VS Code for commit messages
git config --global pull.rebase false # pull merges (simplest for beginners)
git config --global core.autocrlf true # Windows only: handle line endings
git config --global color.ui autoSee everything you've set:
git config --list --show-originConnecting to GitHub: HTTPS or SSH
Option 1: HTTPS (easiest). When you first git push, a browser window asks you to sign in to GitHub (Git Credential Manager on Windows/macOS). On Linux, create a personal access token on GitHub (Settings → Developer settings) and use it instead of a password.
Option 2: SSH keys (no passwords, great for servers).
ssh-keygen -t ed25519 -C "amina@example.com" # press Enter to accept defaults
cat ~/.ssh/id_ed25519.pub # copy this public keyOn GitHub: Settings → SSH and GPG keys → New SSH key, paste it. Then test:
ssh -T git@github.comUse SSH addresses like git@github.com:amina/portfolio.git when cloning.
Never share your private key (
id_ed25519without.pub). The.pubfile is the one you give out.
The three places your files live
| Area | What's there | Commands |
|---|---|---|
| Working directory | Files as you edit them | edit, save |
| Staging area | Changes chosen for the next commit | git add |
| Repository | Saved snapshots (commits) | git commit |
git status # what's changed, what's staged
git add index.html # stage one file
git add . # stage everything in this folder
git commit -m "Add contact section"
git log --oneline # history, one line per commitGit in VS Code
VS Code has Git built in: the Source Control icon on the left shows changed files, a + to stage, a message box and a ✓ to commit, and a button to push. It runs the same commands for you. Learn the commands first, then use whichever is faster.
Why every developer needs Git
Git records every version of your code, lets you undo mistakes, work on new features without breaking what works, and collaborate with other developers without emailing zip files. Almost every software company, startup and open-source project uses Git, usually with GitHub, GitLab or Bitbucket. Employers expect you to know it, and your GitHub profile often acts as a portfolio when applying for developer jobs and internships.
Your first repository from start to finish
mkdir duka-website && cd duka-website
git init # create a new repository (adds a hidden .git folder)
echo "# Duka website" > README.md
git status # README.md is "untracked"
git add README.md # stage it
git commit -m "Add README" # save a snapshot
git log --oneline # see the historyThen connect it to GitHub:
# create an empty repository on github.com first (no README), then:
git remote add origin git@github.com:yourname/duka-website.git
git branch -M main
git push -u origin main # -u remembers the link for future pushesAfter this, git push and git pull are enough.
Cloning an existing project
git clone https://github.com/someone/project.git
cd project
git log --oneline -5 # last five commitsgit clone downloads the whole history, not just the latest files, so you can work offline.
Setting up SSH keys step by step
SSH keys let you push without typing a password or token each time.
ssh-keygen -t ed25519 -C "you@example.com" # press Enter to accept the default location; add a passphrase
cat ~/.ssh/id_ed25519.pub # copy this PUBLIC key
# GitHub → Settings → SSH and GPG keys → New SSH key → paste
ssh -T git@github.com # test: "Hi yourname! You've successfully authenticated"Never share the private key (id_ed25519 without .pub). If a laptop is lost or stolen, delete its key from GitHub settings.
HTTPS with tokens
If you use HTTPS, GitHub requires a personal access token (not your account password) for pushing. Git Credential Manager (included with Git for Windows) handles sign-in through the browser and stores credentials securely.
Useful configuration
git config --global init.defaultBranch main
git config --global core.editor "code --wait" # use VS Code for commit messages
git config --global pull.rebase false # merge when pulling (simple default)
git config --global core.autocrlf true # Windows: handle line endings
git config --global alias.st status # shortcut: git st
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --list --show-origin # see all settings and where they come fromAliases save typing: git lg shows a branch graph.
Understanding status output
| Status | Meaning | Next step |
|---|---|---|
| Untracked | New file Git doesn't know about | git add file |
| Modified (not staged) | Changed since the last commit | git add file or git restore file to discard |
| Staged | Will be in the next commit | git commit or git restore --staged file to unstage |
| Clean | Nothing to commit | Keep working |
git diff # changes not yet staged
git diff --staged # changes staged for the next commit
git restore index.html # throw away unstaged changes in a file (careful!)
git restore --staged index.html # unstage but keep changesUndoing common mistakes
| Mistake | Fix |
|---|---|
| Typo in the last commit message (not pushed) | git commit --amend -m "New message" |
| Forgot a file in the last commit (not pushed) | git add file then git commit --amend --no-edit |
| Want to undo a pushed commit safely | git revert <commit> (creates a new commit that reverses it) |
| Want to go back and look at an old version | git switch --detach <commit> then git switch main to return |
| Deleted a file by accident (not committed) | git restore file |
Avoid rewriting history (amend, reset, rebase) on commits already pushed and shared with others.
Git on Windows, macOS and Linux: notes
- Windows: Git for Windows includes Git Bash, a terminal with Linux-style commands. VS Code's terminal can use it.
- macOS: run
git --version; if missing, macOS offers to install the Command Line Tools. - Linux:
sudo apt install git(Ubuntu/Debian). - Phones: you can browse repositories in the GitHub mobile app; serious work needs a computer.
GitHub profile tips for job seekers
- A clear profile photo, name, location (e.g. Nairobi, Kenya) and short bio.
- Pin your 4 to 6 best repositories.
- Every project has a README with a description, screenshots, how to run it and a live demo link.
- A profile README (a repository named the same as your username) introduces you.
- Regular, meaningful commits matter more than a green contribution graph full of trivial changes.
Practice
- Create a repository, make three commits, and view them with
git log --oneline. - Set up SSH keys and push the repository to GitHub.
- Create aliases for
statusand a graph log. - Make a change, stage it, unstage it with
git restore --staged, then discard it. - Amend the last commit to add a forgotten file.
Think about it: You committed a change, pushed it, and your teammate already pulled it. Now you realise it broke the login page. Should you use git reset or git revert, and why?Show answer
Use git revert <commit>. It adds a new commit that undoes the change, keeping history intact for everyone. git reset rewrites history; after pushing, it forces teammates to deal with conflicting histories and can lose work.
Check yourself
Which command shows your installed Git version?
Show answer
git --version
Which setting stores your name for commits? (write the config key)
Show answer
user.name
Which file do you upload to GitHub: id_ed25519 or id_ed25519.pub?
Show answer
id_ed25519.pub
Which command stages every changed file in the current folder?
Show answer
git add .
Which command shows history, one line per commit?
Show answer
git log --oneline
Which command creates a new Git repository in the current folder?
Show answer
git init
Which command safely undoes a pushed commit by creating a new commit?
Show answer
git revert
Which command fixes the message of the last commit that hasn't been pushed? (two words after git)
Show answer
commit --amend
Which command downloads a repository and its history from GitHub?
Show answer
git clone