Git and GitHub for Beginners: The Only Commands You Need
Version control without the fear: the core Git workflow, the commands you will actually type, and how to fix common mistakes.
Git records the history of a project so you can change things without losing the ability to go back. GitHub is a website that hosts Git repositories and adds collaboration on top. Almost everything else is detail.
The mental model
Three places hold your work:
- Working directory โ the files as they are on your computer right now.
- Staging area โ the changes you have chosen to include in the next save.
- Repository โ the recorded history of saved commits.
A commit is a snapshot with a message. That is the whole idea: take snapshots often, and describe them well.
One-time setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
The daily workflow
This is the loop you will repeat forever:
git status # what has changed?
git add . # stage everything you want to save
git commit -m "Add SQL article" # save a snapshot
git push # send it to GitHub
With GitHub, start by getting the project:
git clone https://github.com/user/repo.git
Branches
A branch is a parallel line of work, so you can try something without disturbing the main version:
git switch -c feature/new-page # create and switch
git switch main # go back
When the work is ready, merge it:
git switch main
git merge feature/new-page
main working at all times, and do experiments on branches.Seeing what happened
git log --oneline # compact history
git diff # unstaged changes
git show <commit> # details of one commit
Good commit messages make this history useful. Write the why, briefly, in the imperative: "Fix date sorting on the blog index".
Fixing common mistakes
| Situation | Fix |
|---|---|
| Staged a file you did not want | git restore --staged file |
| Want to discard local edits | git restore file |
| Bad commit message, not pushed | git commit --amend -m "New message" |
| Need to undo a pushed commit | git revert <commit> (creates a safe inverse commit) |
| Work in progress, need to switch | git stash then later git stash pop |
git revert is the safe undo for shared history. Avoid rewriting commits that others may already have.
A note on merge conflicts
A conflict happens when two changes touch the same lines. Git marks the file like this:
<<<<<<< HEAD
your version
=======
their version
>>>>>>> other-branch
Edit the file to the version you actually want, delete the marker lines, then git add and commit. Conflicts are normal, not a sign you broke something.
Habits that keep you out of trouble
- Commit small and often. Large commits are hard to understand and hard to undo.
- Commit only working states on your main branch.
- Pull before you push. It prevents most avoidable conflicts.
- Never commit secrets. Passwords and API keys belong in environment variables.
- Write messages for your future self, who will have forgotten everything.
The bottom line
You need surprisingly little Git: status, add, commit, push, pull, branch, switch, merge. Learn those, and version control becomes a safety net instead of a source of anxiety.