Git Basics: Commit, Branch, Push
7 pages · ~5 min✓ Reviewed
Start here. Next up: Git Internals: The Object Model.
What Git Is and the Three Areas
1. What Git Is
- Distributed VCS: every clone holds the full history
commit,log,diffwork offline- A commit = immutable snapshot of staged files
- It stores message, author, parent pointer, SHA-1 hash
2. The Three Areas
- Working treefiles on disk
- Staging (index)next commit
- Repository.git history
| Command | Copies |
|---|---|
git add | working tree → staging |
git commit | staging → repository |
git restore <file> | staging → working tree |
git restore --staged <file> | last commit (HEAD) → staging |
3. File States
- 1Untrackednew file
- 2Modifiededited on disk
- 3Stagedafter add
- 4Committedafter commit
Which state?
git status tells you the state of every file. Run it before every commit.
4. Interview Point
- Why does Git work offline? Every clone has the full history
- Staging area? The snapshot the next commit will contain
Core Workflow: init, clone, add, commit
1. Start a Repo
git initcreates a .git folder in the current directorygit clone <url>copies the full repo and sets remote origin
2. Stage, Then Record
- Editworking tree
- git addto staging
- git committo repository
| Command | Takes | New files |
|---|---|---|
git add <file> | one file | yes |
git add . | all under current dir | yes |
git commit -m "msg" | staged snapshot only | if staged |
git commit -am "msg" |
Inspecting State: status, log, diff
1. Status first
git statusshows the branch, staged, unstaged and untracked files- Run it before every commit
- Not tracked yet: untracked. Edited but not added: unstaged
2. diff: which two areas
| Command | Compares |
|---|---|
git diff | working tree vs staging |
git diff --staged | staging vs last commit |
3. Reading history
--oneline --graph --all: compact history, every branchlog -p <file>: the patches that changed one fileshow <hash>: one commit, message and patch
Branches and Merging
1. Branch = Pointer
- A branch is a movable pointer to a commit
- HEAD points to the branch you are on
- A new commit moves the current branch forward
- C1
- C2
- C3main
- HEADpoints to main
2. Branch Commands
| Command | Does |
|---|---|
git switch -c <name> | create and switch |
git branch | list branches |
git branch -d <name> | delete a merged one |
3. Merge and Conflicts
Remotes, Push/Pull and .gitignore
1. Remotes and Publishing
- A remote is a named URL, usually
origin git remote -vlists themgit push -u origin <branch>publishes and sets the upstream- After that, plain
git pushworks
bash
git remote -v git push -u origin feat/login git push
-u only the first time
2. Fetch, Pull, Rejected Push
| fetch | pull | |
|---|---|---|
| Downloads commits | yes | yes |
| Changes your files | no | yes, merges |
| Equals | git fetch | fetch + merge (--rebase: rebase) |
Git Basics Cheat Sheet
1. Start, Save, History
- Working treeedit files
- Staginggit add
- Repositorygit commit
| Goal | Command |
|---|---|
| Start | git init · git clone <url> |
| Identity | git config --global user.name / user.email |
| State | git status |
| Stage, save | git add <file> · git commit -m "msg" |
| Diff | git diff · git diff --staged |
| History | git log --oneline --graph · git show <hash> |
Check yourself
You run git add app.py, edit app.py again, then git commit -m "x". What does git status show for app.py?
- The commit recorded the staged version, which is the first edit
- The second edit was never staged, so app.py shows as modified
- Nothing is staged any more
You add .env to .gitignore, but git status still reports .env as modified. Why?
.gitignoreonly skips untracked files; .env is already tracked- Run
git rm --cached .env, then commit - Old commits still hold the secret
git push is rejected. What happened and what do you do?
- The remote has commits you lack
git pull, resolve any conflicts, then push again- Avoid
--forceon shared branches: it overwrites others' work