Git Fundamentals
Git records every change to your project so you can see history, work with others and undo mistakes.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 29, 2026
Related tutorials
Learning objectives#
- Create a repository and make commits
- Understand working tree, staging area and history
- Ignore files with .gitignore
- Undo common mistakes safely
Explanation#
A repository stores the full history. Files move from the working tree (your edits) to the staging area (git add) to a commit (git commit).
A commit is a snapshot with an author, message and a unique hash.
.gitignore lists files Git should never track: node_modules, bin/obj, .env and secrets.
Undo: git restore (discard working changes), git restore --staged (unstage), git commit --amend (fix the last commit before pushing), git revert (undo a pushed commit safely).
Code example: Your first repository#
git init shop-api && cd shop-api
echo "# Shop API" > README.md
printf "bin/\nobj/\nnode_modules/\n.env\n" > .gitignore
git status # untracked: README.md, .gitignore
git add README.md .gitignore
git commit -m "Initial commit: README and .gitignore"
git log --oneline
git diff # shows unstaged changes (none now)
[main (root-commit) 3f2a9c1] Initial commit: README and .gitignore 2 files changed, 5 insertions(+) 3f2a9c1 Initial commit: README and .gitignore
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- git init creates the hidden .git folder.
- .gitignore keeps build output and secrets out of history.
- git add stages files; git commit records the snapshot.
- git log shows history with short hashes.
Real-world use case#
Every lesson project on this site starts with git init — your history becomes your safety net and your portfolio.
Common mistakes#
Removing a file later does not remove it from history. Add .env to .gitignore first; rotate any leaked secret.
Small commits with clear messages make history useful.
Best practices#
- Commit small, focused changes
- Write messages in the imperative (“Add login page”)
- Create .gitignore before the first commit
- Use git revert for pushed commits
Remember this#
Working tree → git add → staging → git commit → history.
.gitignore before secrets ever exist.
Pushed? Use revert, not reset.
Practice exercise#
Your task: Unstage a file you added by mistake, keeping your edits.
Show solution
git restore --staged appsettings.Development.json
Mini quiz#
Which command moves changes into the staging area?
Want more? Practise all DevOps MCQs · DevOps interview questions
My notes#
Saved in this browser.