Git basics for GitHub: essential commands

You have a GitHub repository – now what? Learn the core Git commands to clone, commit, push, pull and keep your code in sync. No prior Git experience needed.


clone

edit

commit

push

GitHub
git clone – download a repository

Copy a repository from GitHub to your local machine. You only need to do this once per project.

git clone https://github.com/username/repo.git
$ git clone https://github.com/yourname/my-first-project.git
Cloning into 'my-first-project'...
remote: Enumerating objects: 3, done.
done.

This creates a folder named my-first-project with all files and the full Git history.

After clone: cd my-first-project to enter the folder. Then you can start working.
git status – what's happening?

Shows which files are modified, staged, or untracked. Use it often – it's your Git dashboard.

git status
$ git status
On branch main
Changes not staged for commit:
  modified: index.html
Untracked files:
  style.css

🔴 Red = modified but not staged · 🟢 Green = staged (ready to commit)

git add – stage changes

Tell Git which changes you want to include in the next commit.

git add filename   or   git add . (all files)
$ git add index.html style.css
$ git status
Changes to be committed:
  new file: style.css
  modified: index.html
Tip: git add -p lets you stage parts of a file (advanced but handy).
git commit – save a snapshot

Create a permanent checkpoint with a descriptive message.

git commit -m "your message"
$ git commit -m "add style.css and update homepage"
[main 1a2b3c4] add style.css and update homepage
2 files changed, 42 insertions(+), 3 deletions(-)

✅ The commit is now saved locally. The message should be clear (e.g., "fix login bug", not "update").

Remember: commit saves to your local Git – GitHub doesn't see it yet.
git push – upload commits to GitHub

Send your local commits to the remote repository (GitHub).

git push origin main
$ git push origin main
Enumerating objects: 5, done.
To https://github.com/yourname/my-first-project.git
   a1b2c3d..e4f5g6h main -> main

Now visit your GitHub repo – your changes are live!

origin = nickname for your GitHub URL. main = branch name (could be 'master' on older repos).
git pull – get latest from GitHub

If teammates (or you from another computer) pushed changes, pull brings them to your local machine.

git pull origin main
$ git pull origin main
remote: Counting objects: 4, done.
   a1b2c3d..e4f5g6h main -> origin/main
Updating a1b2c3d..e4f5g6h
Fast-forward
 README.md | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

Always pull before you start working to avoid merge conflicts.

git log – see commit history

Shows a list of all commits with author, date, and message.

git log --oneline (compact view)
$ git log --oneline
e4f5g6h (HEAD -> main, origin/main) add style.css and update homepage
a1b2c3d initial commit with README

Typical workflow (all together)

# after cloning or pulling git pull origin main # get latest # make changes to files (edit, add, delete) git status # see what's changed git add . # stage all changes git commit -m "add new feature" git push origin main # send to GitHub

That's the daily cycle: pull → edit → add → commit → push.

Frequently Asked Questions (Git basics)

What's the difference between git pull and git fetch?
git fetch downloads new data from GitHub but does not integrate it into your working files. git pull = fetch + merge (it updates your files). For beginners, git pull is simpler.
I accidentally committed without adding everything. How do I fix?
You can amend the last commit: stage the missing files (git add .) then run git commit --amend. This updates the previous commit instead of creating a new one. Only amend if you haven't pushed yet!
Why do I need to write a commit message?
Messages describe what changed and why. They help you (and others) understand history. Think of it as a caption for your snapshot. Bad: "update". Good: "fix navbar alignment on mobile".
What does "origin" mean?
origin is a default alias (shortcut) for your remote repository URL. You can see it with git remote -v. You could rename it, but most people keep "origin".
How do I undo a git add before commit?
git reset unstages files. Example: git reset HEAD index.html removes that file from staging. If you want to unstage everything: git reset.
I see "main" and sometimes "master". Which one is correct?
GitHub now uses main as default branch name (since 2020). Older repositories may use master. Both work the same; just use the one your repo shows. Command: git push origin main or git push origin master.
What's a "merge conflict" and should I be scared?
It happens when two people change the same part of a file. Git doesn't know which change to keep. You'll see markers <<<<<<<. Don't panic – open the file, decide which part to keep, remove the markers, then git add and commit. It's a normal part of collaboration.
Do I need to run git pull every time before git push?
If you're the only person working on the repo, not always. But it's a good habit: git pull ensures you have any remote changes, avoiding rejected pushes. Many teams do pull → work → commit → pull again → push.
How do I ignore files (like node_modules) from being committed?
Create a file called .gitignore in your repo and list patterns (e.g., node_modules/, .env). Already tracked files won't be ignored – you'd need to git rm --cached them. Most templates are available when creating a repo.
I accidentally deleted a file. Can Git recover it?
Yes! If you committed it before, git checkout -- filename restores it. If you already committed the deletion, you can revert that commit. Git keeps history forever (almost).
What is the difference between GitHub and Git now that I know commands?
Git is the command-line tool you just learned. GitHub is the website that stores your git pushed repositories and adds collaboration features (Issues, PRs, Actions). You can use Git without GitHub, but GitHub relies on Git.
Previous: Create first repo Next: Branches on GitHub

You've just learned the daily Git workflow – practice by cloning a repo and making a commit!