Git is a version control system: it records every change to your code so you can go back, compare versions and work with a team without overwriting each other's work. GitHub (like GitLab or Bitbucket) hosts Git repositories online and adds pull requests, code review and CI/CD.
Every developer, tester and DevOps engineer uses Git daily.
One-time setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainThe three areas
| Area | Meaning |
|---|---|
| Working directory | Files you are editing |
| Staging area | Changes selected for the next commit (git add) |
| Repository | Saved history of commits (git commit) |
Your first repository
mkdir my-project && cd my-project
git init
echo "# My Project" > README.md
git status
git add README.md
git commit -m "Add README"
git log --onelineWrite commit messages that say what changed and why, in the imperative: "Add login validation", not "changes".
Daily commands
git status # what changed?
git diff # exact line changes not yet staged
git add . # stage everything in this folder
git commit -m "Fix fee calculation for discounts"
git pull # get the latest from the remote
git push # send your commits to the remoteBranches: work without breaking main
git switch -c feature/login-page # create and move to a new branch
# ...edit files, add, commit...
git push -u origin feature/login-pageThen open a Pull Request (PR) on GitHub. Teammates review the code, CI runs the tests, and when everything passes the branch is merged into main.
Keep branches small and short-lived. A PR with 200 changed lines gets a careful review; one with 3,000 lines gets a rubber stamp.
Merging and conflicts
git switch main
git pull
git merge feature/login-pageA merge conflict happens when two branches changed the same lines. Git marks the file:
<<<<<<< HEAD
const fee = 40000;
=======
const fee = 42000;
>>>>>>> feature/fee-updateEdit the file to keep the correct version, remove the markers, then:
git add pricing.js
git commitConnect to GitHub
git remote add origin https://github.com/your-name/my-project.git
git push -u origin mainUndoing mistakes safely
| Situation | Command |
|---|---|
| Discard unstaged changes in a file | git restore file.txt |
| Unstage a file | git restore --staged file.txt |
| Fix the last commit message (not yet pushed) | git commit --amend -m "Better message" |
| Undo a pushed commit safely | git revert <commit-id> |
| Save work temporarily | git stash, later git stash pop |
Avoid git push --force on shared branches; it rewrites history for everyone. If you must, use --force-with-lease.
.gitignore: never commit secrets
Create a .gitignore file so passwords, keys and build output never enter the repository:
node_modules/
.env
.env.local
*.log
target/If a secret is ever pushed, consider it leaked: rotate the key immediately, even after deleting the commit.
Interview questions
- `git fetch` vs `git pull`?
fetchdownloads changes without merging;pull=fetch+ merge (or rebase). - `merge` vs `rebase`? Merge keeps history as it happened with a merge commit; rebase replays your commits on top of the target for a linear history.
- `revert` vs `reset`?
revertadds a new commit that undoes a change (safe for shared branches);resetmoves the branch pointer and can discard history.
Next steps
Create a GitHub repository for your projects and practise the branch and PR flow daily. Then automate builds with Docker and CI/CD in the DevOps + AI course.
