Git Basics Reference
March 9, 2026 · View on GitHub
A practical reference for the Git commands you will use in this project. Bookmark this page.
Core concepts
| Concept | What it means |
|---|---|
| Repository (repo) | A folder tracked by Git — contains your project and its full history |
| Commit | A saved snapshot of your files at a point in time |
| Branch | An independent line of development — like a parallel version of the project |
| Remote | A copy of the repository hosted somewhere else (e.g. GitHub) |
| Fork | Your own copy of someone else's repository on GitHub |
| Pull Request (PR) | A proposal to merge your branch into another branch |
| Merge | Combining changes from one branch into another |
| Rebase | Replaying your commits on top of another branch (cleaner history than merge) |
Setting up Git
Configure your name and email (once, on any new computer):
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Check your configuration:
git config --list
Starting with a repository
Clone a repository to your local machine:
git clone https://github.com/USERNAME/REPO.git
Initialize a new repository in the current folder:
git init
Working with remotes
View your remote connections:
git remote -v
Add a new remote:
git remote add upstream https://github.com/ORIGINAL/REPO.git
Fetch changes from a remote (does not merge):
git fetch upstream
Pull changes from a remote and merge:
git pull upstream main
Push your branch to your remote:
git push origin your-branch-name
Branches
List all local branches:
git branch
List all branches including remote:
git branch -a
Create and switch to a new branch:
git checkout -b branch-name
Switch to an existing branch:
git checkout branch-name
Delete a branch (after it has been merged):
git branch -d branch-name
Making changes
Check the status of your working directory:
git status
See what has changed (unstaged):
git diff
See what is staged and ready to commit:
git diff --staged
Stage specific files:
git add filename.html
git add folder/
Stage all changed files:
git add .
Unstage a file (keep changes, just remove from staging):
git restore --staged filename.html
Discard changes in a file (cannot be undone):
git restore filename.html
Committing
Commit staged changes:
git commit -m "type: short description"
Amend the most recent commit message (before pushing):
git commit --amend -m "type: corrected description"
View commit history:
git log
View a compact commit history:
git log --oneline
Rebasing
Rebase brings your branch up to date with another branch by replaying your commits on top of it.
First, fetch the latest changes:
git fetch upstream
Then rebase your branch onto main:
git rebase upstream/main
If there are conflicts, Git will pause and tell you which files conflict. Open them, resolve the conflict markers, then:
git add resolved-file.html
git rebase --continue
To abort a rebase in progress:
git rebase --abort
After rebasing, force-push your branch (required because history changed):
git push origin your-branch-name --force-with-lease
Resolving merge conflicts
A conflict looks like this in a file:
<<<<<<< HEAD
your version of the line
=======
the incoming version of the line
>>>>>>> upstream/main
To resolve:
- Open the conflicted file in your editor
- Decide which version to keep (or combine them)
- Delete the conflict markers (
<<<<<<<,=======,>>>>>>>) - Save the file
- Stage it:
git add filename - Continue the merge or rebase
Useful shortcuts
Undo the last commit (keep changes staged):
git reset --soft HEAD~1
Stash uncommitted changes (to work on something else temporarily):
git stash
Restore stashed changes:
git stash pop
See the full history with branches visualized:
git log --oneline --graph --all
Common mistakes and fixes
Committed to main by accident
git checkout -b my-branch # create a new branch at current state
git checkout main # go back to main
git reset --hard origin/main # reset main to match remote
git checkout my-branch # continue work on the new branch
Pushed the wrong thing
Do not force-push to main. Contact a maintainer.
Forgot to pull before starting work
git fetch upstream
git rebase upstream/main
This replays your commits on top of the latest main.