What Is Git?
Git is a distributed version control system. It records snapshots of your project over time, lets many people work together, and lets you go back to any earlier version. In DevOps, everything is stored in Git: application code, Dockerfiles, Terraform files, Kubernetes YAML and pipeline definitions.
Git is not GitHub: Git is the tool on your computer. GitHub, GitLab and Bitbucket are websites that host Git repositories and add pull requests, issues and CI.
Install and First-Time Setup
bash
git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase false # merge on pull (a clear default)
git config --list # check settings
The Three Areas
Understanding these three places removes most Git confusion:
text
Working directory --git add--> Staging area (index) --git commit--> Repository (history)
(files you edit) (what goes into the next commit) (saved snapshots)
Your First Repository
bash
mkdir shop && cd shop
git init # creates the hidden .git folder
echo "# Shop" > README.md
git status # README.md is untracked
git add README.md # stage it
git commit -m "Add README" # save a snapshot
git log --oneline # see history
Result
git status shows what changed, what is staged and what is untracked. Run it often. It also tells you the next command to use.
A Normal Daily Cycle
bash
# 1. Edit files
echo "print('hello')" > app.py
# 2. See what changed
git status
git diff # unstaged changes
# 3. Stage chosen files (or parts of files)
git add app.py
git add -p # choose hunks interactively
git diff --staged # what will be committed
# 4. Commit with a clear message
git commit -m "feat: add hello app"
# 5. Look at history
git log --oneline --graph --decorate --all
git show HEAD # details of the last commit
Good Commit Messages
- Commit small, related changes together
- First line: short summary in the imperative, under about 72 characters (
Fix login timeout)
- Optional body: explain why, not only what
- Many teams use Conventional Commits:
feat:, fix:, docs:, chore:, refactor:, test:
- Never commit secrets, passwords or large binaries
Ignoring Files
Create a .gitignore file listing things Git must not track: dependencies, build output, local settings and secrets.
text
# .gitignore
node_modules/
__pycache__/
*.log
.env
*.tfstate
*.tfstate.backup
.terraform/
dist/
.DS_Store
If a secret was committed: Deleting it in a later commit does not remove it from history. Rotate (change) the secret immediately, then clean history with git filter-repo or ask your platform team.
Cloning and Remotes
bash
git clone https://github.com/example/shop.git
git remote -v # list remotes
git remote add origin git@github.com:me/shop.git
git push -u origin main # first push, set upstream
git pull # fetch and merge new commits
git fetch # download only, no merge
Undoing Things Safely
| Situation | Command | Safe on shared branches? |
| Discard edits in a file (not staged) | git restore file.txt | Yes (loses the edits) |
| Unstage a file | git restore --staged file.txt | Yes |
| Fix the last commit message or add a file | git commit --amend | Only if not pushed |
| Undo a commit by adding a new opposite commit | git revert <commit> | Yes, preferred |
| Move branch back and keep changes staged | git reset --soft HEAD~1 | Only if not pushed |
| Move branch back and keep changes unstaged | git reset --mixed HEAD~1 | Only if not pushed |
| Move branch back and delete changes | git reset --hard HEAD~1 | Dangerous |
| Find a lost commit | git reflog | Yes (read only) |
bash
git revert 3f2a1bc # creates a new commit that undoes 3f2a1bc
git reflog # history of where HEAD pointed, even after reset
git reset --hard 3f2a1bc # go back to a commit found in reflog
Safety net: Almost nothing committed is truly lost for a while. git reflog can usually bring it back.
Stash: Save Work for Later
bash
git stash push -m "wip: login form" # hide changes, clean working tree
git stash list
git stash pop # bring the latest stash back
git stash apply stash@{1} # apply but keep it in the list
Tags: Mark Releases
bash
git tag v1.0.0 # lightweight tag
git tag -a v1.0.1 -m "Release 1.0.1" # annotated tag (preferred)
git push origin v1.0.1 # tags are not pushed automatically
git tag --list
Handy Inspection Commands
bash
git log -p app.py # history of one file with changes
git log --author="alice" --since="2 weeks ago"
git blame app.py # who changed each line
git diff main..feature # differences between branches
git grep "TODO" # search tracked files
git bisect start # binary search for the commit that introduced a bug
Practice Tasks
- Create a repo, make three commits, and view them with
git log --oneline --graph
- Add a
.gitignore and prove that an ignored file does not show in git status
- Make a bad commit and undo it with
git revert
- Use
git stash to switch tasks and come back
- Push your repo to GitHub using an SSH key
Conclusion
Git tracks snapshots through three areas: working directory, staging area and repository. Learn add, commit, log, diff, restore and revert well, and use reflog as your safety net.
Quick Summary: add -> commit; status/diff/log; .gitignore; revert for shared history; stash; tags; reflog.