Git Basics: Commands Every Developer Should Know
Short answer: Git tracks changes to your files as a series of snapshots called commits. The daily loop is: change files, git add to choose what to save, git commit to save it, and git push to share it. This page walks through each essential command.
One-time setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
Start a repository
git init # turn the current folder into a repo
git clone https://github.com/user/project.git # copy an existing repo
The basic workflow
git status # what changed?
git add index.html # stage one file
git add . # stage everything in the folder
git commit -m "Add homepage" # save a snapshot with a message
git log --oneline # see history
Git has three areas: your working directory (files you edit), the staging area (what will go into the next commit) and the repository (saved commits). git add moves changes to staging and git commit saves them.
Seeing changes
git diff # unstaged changes
git diff --staged # staged changes
git show <commit> # one commit in detail
Branches
git branch # list branches
git switch -c feature/login # create and switch to a new branch
git switch main # go back
git merge feature/login # merge it into the current branch
git branch -d feature/login # delete after merging
Branches let you work on a feature without touching the stable code. See branching strategies and merge vs rebase.
Working with a remote (GitHub, GitLab)
git remote add origin https://github.com/user/project.git
git push -u origin main # first push, sets upstream
git push # later pushes
git pull # fetch and merge remote changes
git fetch # download without merging
Undoing things
| Goal | Command |
|---|---|
| Discard changes in a file | git restore file.txt |
| Unstage a file | git restore --staged file.txt |
| Fix the last commit message | git commit --amend (only if not pushed) |
| Undo a pushed commit safely | git revert <commit> |
| Move back and drop commits | git reset --hard <commit> (destructive) |
| Save work temporarily | git stash; see Git stash |
Ignoring files
Create a .gitignore file for things that should not be tracked, such as dependencies and secrets:
node_modules/
.env
dist/
.DS_Store
Never commit passwords or API keys. If you do, rotate them, because they stay in history.
Good commit habits
- Commit small, related changes.
- Write messages in the imperative: "Fix login redirect", not "fixed stuff".
- Pull before you push to reduce conflicts. If one happens, follow how to resolve merge conflicts.
New to Git? Read what Git is and how it works, and keep the Git commands cheat sheet handy.
FAQs
What is the difference between Git and GitHub?
Git is the version control tool on your computer. GitHub is a website that hosts Git repositories and adds collaboration features.
What is the difference between git fetch and git pull?
fetch downloads remote changes without changing your files. pull does a fetch and then merges.
How do I see who changed a line?
Use git blame file.txt, or the history view in your editor.
What does HEAD mean?
HEAD points to the commit (usually the tip of the current branch) you are working on.
