Git for Beginners: Working Directory, Staging Area, Repository and Core Commands
Git is an essential version control tool, but many get stuck on concepts like "why do we need a staging area" and "what's the difference between commit and push." This guide starts with the three areas, gives a common command list, and walks through a complete workflow — follow along once and you will have it. All commands work in macOS and Linux terminals as well as Windows Git Bash. For more developer tools, follow the dev tools category.
1. The Three Areas: Working Directory, Staging Area, Repository
- Working directory: the folder you are editing; changes happen here first;
- Staging area (index): the "to-do list" before committing. Use
git addto place changes you want to commit — like picking what to pack; - Repository (.git): where Git actually stores history; only
git commitpermanently records the staged content.
In one sentence: add picks; commit stamps and archives.
Configure your identity first: run git config --global user.name "Your Name" and git config --global user.email "[email protected]" so commits carry an author. To ignore temporary files (caches, secrets, logs), create a .gitignore in the repository root and list the filenames. A typical loop is: edit files → git status to see changes → git add to pick → git commit to archive → git push to sync; repeating this loop is everyday development. For an existing project, git clone <repo-url> copies it locally with the full history — more complete than a manual download.
2. Common Command List
| Command | What it does |
|---|---|
git init |
Initialize a repository in a directory |
git status |
Show the current change state |
git add <file> |
Add changes to the staging area |
git commit -m "message" |
Archive the staged content |
git log |
Show commit history |
git branch |
List or create branches |
git checkout -b <branch> |
Create and switch to a branch |
git push origin <branch> |
Push local commits to remote |
git pull |
Pull the latest remote code |
These commands cover 80% of daily work: init to start, add+commit to record changes, push/pull to sync, log to review, and branch/checkout to switch lines of work. When stuck, run git status — it is Git's built-in hint for "where am I and what can I do next." To see which files a commit changed, run git show <commit>.
3. Branches and Merging
A branch is a parallel line of development. The main line is called main (formerly master). When building a feature, create a branch, finish it, then merge back into main to avoid chaos.
main: A -- B -- C
\
feature: D -- E → merge back to main
In team work, git pull before developing, and resolve conflicts manually when they occur. For common patterns, see Git workflow for website development. During a conflict, Git marks the differences between <<<<<<< HEAD and >>>>>>>; pick the content, git add, then git commit. Name branches descriptively, like feature/add-nav, fix/typo, or docs/readme, instead of test1 or asdf. After merging back to main, delete the feature branch (git branch -d <branch>) to keep the list clean.
4. A Complete Workflow Example
Deploying a static site, for example:
# 1. Initialize and make the first commit
git init
git add .
git commit -m "init: first commit"
# 2. Push to remote (create the remote repo first)
git remote add origin [email protected]:you/site.git
git push -u origin main
# 3. Develop a new feature
git checkout -b add-nav
# ...edit files...
git add .
git commit -m "feat: add navigation"
# 4. Merge back to main and push
git checkout main
git pull
git merge add-nav
git push
# 5. View history
git log --oneline
To deploy automatically, trigger CI/CD after push — see GitHub Actions CI/CD and website CI/CD pipeline.
Tip: make each commit do one thing with a clear message so rollbacks are fast. Use prefixes like feat:, fix:, and docs: to skim git log quickly. Pull before you push so you reduce conflicts when others have pushed new commits.
5. Advanced Commands Cheat Sheet
| Command | What it does |
|---|---|
git diff |
Show unstaged changes in detail |
git stash |
Temporarily shelve uncommitted changes |
git log --oneline |
View history in a compact list |
git reset --soft HEAD~1 |
Undo the last commit but keep changes |
git revert <commit> |
Undo with a new commit, without rewriting history |
git rm --cached <file> |
Stop tracking a file but keep it locally |
reset rewrites history; revert does not. For commits already pushed and shared, prefer revert. To see who changed a specific line, run git blame <file>; to hunt down a regression, git bisect does an automatic binary search. If you staged a file by mistake, git reset <file> unstages it.
- Q: What's the difference between
commitandpush:commitarchives locally;pushsyncs the archive to the remote. Commit without push, and others will not see your changes. - Q: What if I accidentally commit sensitive information:Remove the file and rotate the secret immediately. If already pushed, treat it as leaked and replace it. For safer practices, see the Git deployment workflow.
- Q: Can I fix a wrong commit message:For the last commit, use
git commit --amend -m "new message". Rewriting history on pushed branches rewrites history — use sparingly in team settings. - Q: How do I split changes into multiple commits:Use
git add <file>to stage specific files instead ofgit add .for everything, then commit each group separately — history stays cleaner. - Q: What is the difference between
git fetchandgit pull:fetchdownloads the latest remote state locally without touching your working tree;pullisfetch+merge, merging into the current branch. When you want to look before merging,fetchis safer.
Integration & Code Examples
Example 1: From clone to first push
# Clone and create a feature branch
git clone https://github.com/<user>/<repo>.git
cd <repo>
git switch -c feat/homepage-copy
# Stage precisely what you mean to commit: -p reviews each hunk
git add -p index.html
git status
git commit -m "feat(home): restructure homepage copy"
# The first push needs -u to set the upstream branch
git push -u origin feat/homepage-copy
git add -p earns its keep over git add .: even with one edited file you can still split "copy changes" from "incidental re-indentation" into two commits, which is what makes history readable.
Example 2: Inspect history and undo safely
git log --oneline --graph --decorate -n 10
git diff --staged
git restore index.html # discard working tree changes
git restore --staged index.html # unstage but keep the changes
git revert <commit> # undo pushed mistakes with a new commit
git reflog # recover a deleted branch or commit
Never rewrite already-pushed history with reset; revert adds a new inverse commit so collaborators do not have to re-clone, and reflog usually gets a lost branch back.
Which Layer reset, revert and restore Actually Touch
Each of the three areas has its own undo: restore for the working directory, restore --staged or reset <file> for the index, and only then reset and revert for commit history. Pick the wrong layer and either nothing moves or changes you meant to keep disappear.
The decision has one input: whether the commit is already pushed. Before pushing, git reset --soft HEAD~1 moves the commit back into the index with every change intact; after pushing, git revert adds a reverse commit so collaborators do not have to re-pull.
| Command | Layer it touches | Rewrites history | Boundary (basis: the command list and examples in this article) |
|---|---|---|---|
git restore <file> |
Working directory | No | Discard unstaged changes |
git restore --staged <file> |
Index | No | Unstage while keeping the edits |
git reset <file> |
Index | No | Pull a file back out of the index |
git reset --soft HEAD~1 |
Commit pointer | Yes | Local commits that were never pushed |
git revert <commit> |
Adds a reverse commit | No | Pushed commits shared with a team |
git rm --cached <file> |
Index | No | Stop tracking a file but keep it on disk |
git reflog |
Reference log | No | Recover a deleted branch or commit |
Splitting Work So One Commit Does One Thing
git add . puts "edit the copy" and "reformat the block" into the same commit, and rolling back then takes both. git add -p confirms hunk by hunk, so even a single file can produce two commits - the precondition for git log --oneline being readable.
Prefix commit messages with feat:, fix: or docs:; the prefix lets git log and git blame output be filtered by type, and it is the input Conventional Commits needs to generate a changelog.
Narrow down problems by granularity: git blame <file> shows who last touched a line, git bisect bisects to the commit that introduced a bug, and git show <commit> lists the files a commit changed.
Limitations
- This article reflects public information as of 2026-08-12; terms, prices and quotas follow the provider's current published terms.
- Third-party capability and limit statements follow their official documentation; no further commitment is implied here.
- Licensing, filing and data-residency decisions must be verified against the regulations of your operating region.
FAQ
Which should beginners learn first, CLI or GUI?
Learn the command line first. A GUI is just buttons over the same commands. About 80% of daily work is status, add, commit, push, pull and log, and conflict handling or reflog recovery still happens in a terminal. Once the three areas — working directory, staging area, repository — are clear, switch to whichever client you prefer.
What do you do after committing a secret key?
Treat it as leaked: rotate the key first, then clean history. If the commit is local only, run git rm --cached and add the file to .gitignore; once pushed, rewriting history cannot recall copies on other machines or in caches, so rotation outranks cleanup. Also check the 3 usual leftovers: CI variables, deploy scripts and image layers.
Why does a git pull leave conflict markers?
Because both sides changed the same lines, so Git keeps both versions. Everything between <<<<<<< HEAD and >>>>>>> is the pair of edits; pick line by line, git add the file, then commit — a 100-line file rarely holds more than a few hunks. To find which change introduced it, read git log -p or blame on that line instead of reverting the whole file.
Can you revert a commit that was already pushed?
Yes, with revert rather than reset. A pushed commit should be undone by git revert, which adds an inverse commit so collaborators need no re-pull; git reset --soft HEAD~1 suits local commits only, because it rewrites history and leaves other clones out of sync.
Related Providers
- GitHub: use it to push the local repository and walk a merge through a pull request when practising the workflow.
- GitLab: use it when the team needs a self-hosted repository with pipelines and images on the same instance.
- GitKraken: when conflicts pile up and the branch graph is unreadable, use it to visualise commits before a rebase or cherry-pick.
Community & Resources
Official docs and communities
- Git documentation — the manual entry point for every command
- Pro Git book — the free book from basics to internals
- git-switch manual — the canonical way to create and switch branches
- git-commit manual — messages, amend and signing options
Learning resources and tools
- Conventional Commits — a commit message convention that supports generated changelogs
- Stack Overflow: git — the most common undo, merge and conflict questions
- git/git repository — official source and release notes
Original source: 16IDC Original ↗