Skip to content
Happy Programming Guide
Start learning
Coding Tools

Git and GitHub Explained for Beginners

What Git actually does, the seven commands that cover most daily work, how GitHub fits in, and how to fix the mistakes beginners make most.

Git saves snapshots of your project so you can go back to any of them. GitHub is a website that stores those snapshots online and lets other people see them.

They are two different things, and the confusion between them causes a lot of early frustration.

Why bother#

Without Git, saving a version means project-final.py, then project-final-2.py, then project-final-ACTUALLY.py. With Git you keep one folder and a full history you can move through.

It also means you can try a risky change and throw it away cleanly, and it is how nearly every team shares code.

Setting up once#

Terminal
git --version

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

The seven commands#

Terminal
git init                    # start tracking this folder
git status                  # what has changed? — use constantly
git add .                   # stage the changes you want to save
git commit -m "Add login"   # save a snapshot with a message
git log --oneline           # see the history
git push                    # send commits to GitHub
git pull                    # bring down other people's commits

git status is the one to run most. It tells you what state you are in and usually suggests the next command.

The everyday cycle#

  1. Change some files
  2. git status — check what changed
  3. git add . — stage it
  4. git commit -m "message" — save it
  5. git push — share it

Commit when you finish a small piece of work, not once a week. “Add validation to the signup form” is a good commit; “stuff” covering three days is not.

Getting it onto GitHub#

  1. Create an empty repository on GitHub — no README, so it does not conflict
  2. Copy the commands it shows you, which look like this:
Terminal
git remote add origin https://github.com/you/project.git
git branch -M main
git push -u origin main

The -u only matters the first time; after that git push is enough.

Branches#

A branch is a parallel version of your project. You try something on a branch, and your main version stays untouched.

Terminal
git switch -c new-feature    # create and move to a branch
git switch main              # back to the main version
git merge new-feature        # bring the work in
git branch -d new-feature    # tidy up

Working alone you can live on main for a while. Branches become essential the moment someone else is involved, or when you want to experiment safely.

.gitignore#

Some things should never be committed. Create a file named .gitignore:

Output
__pycache__/
venv/
node_modules/
.env
.DS_Store
*.log

.env matters most — it usually holds passwords and API keys. Once a secret is pushed to a public repository, treat it as compromised and replace it, even after deleting the file. The history keeps it.

Undoing things#

Terminal
git restore file.py                 # discard unsaved changes to a file
git restore --staged file.py        # unstage, keep the changes
git commit --amend -m "Better msg"  # fix the last commit message
git revert abc123                   # undo a commit with a new commit
git log --oneline                   # find the commit id you need

Be careful with git reset --hard — it deletes uncommitted work with no undo. git revert is the safe option for anything already pushed.

Cloning someone else’s project#

Terminal
git clone https://github.com/someone/project.git
cd project

Questions people ask#

Do I need to learn the command line?

VS Code’s Source Control panel handles the common operations with buttons. Learning the commands is still worth it — every guide and error message you will ever read uses them.

What is a pull request?

A GitHub feature, not a Git one: a request to merge your branch into another, with a page for discussion and review. It is how teams and open-source projects accept changes.

What if I get a merge conflict?

Git marks the clashing sections in the file with arrow markers. Edit the file to how it should read, delete the markers, then add and commit. It looks alarming and is genuinely routine.

Is GitHub the only option?

No — GitLab and Bitbucket do the same job. Git itself works identically with all of them.

Where to go next#

Next lessonBrowser DevTools for beginners

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *