DevOps · Git

Git Fundamentals

Git records every change to your project so you can see history, work with others and undo mistakes.

Beginner 5 min Lesson 3 of 30
Before you start:Introduction to DevOps
0/30 in DevOps
On this page · 5 min · Beginner

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#

ShellYour first repository Illustrative
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)
Expected output
[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#

  1. git init creates the hidden .git folder.
  2. .gitignore keeps build output and secrets out of history.
  3. git add stages files; git commit records the snapshot.
  4. 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#

Committing .env or passwords

Removing a file later does not remove it from history. Add .env to .gitignore first; rotate any leaked secret.

Giant “update” commits

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
Shell Illustrative
git restore --staged appsettings.Development.json

Mini quiz#

Git Fundamentals quizMultiple choice Question 1 of 1

Which command moves changes into the staging area?

Score so far: 0/0

Want more? Practise all DevOps MCQs · DevOps interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.