DEV Community

Muhammad Khizr Khan
Muhammad Khizr Khan

Posted on AI-assisted

DevOps from Zero #4: Git and GitHub, beyond add, commit and push

I'm Muhammad Khizr Khan, a computer science student and full-stack/DevOps developer, learning DevOps in public one class at a time. This is Class 04.

Every CI/CD pipeline, every automatic deploy and every code review starts in the same place: a Git push. So this class goes beyond add, commit and push.

Git vs GitHub

  • Git is a program on your computer that saves the history of a folder. It works offline.
  • GitHub (or GitLab, Bitbucket) is a website that keeps a shared copy, plus pull requests, issues and CI/CD.

final_v2_REAL_final.zip is not a version control system. Git is.

First-time setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git init                  # new repo in this folder
git clone <url>           # copy an existing repo + its history
Enter fullscreen mode Exit fullscreen mode

The hidden .git/ folder is the repository. Never edit or delete it by hand.

The three areas

Area What lives there
Working directory The files you are editing
Staging area Changes chosen for the next commit (git add)
Repository Saved snapshots, called commits (git commit)

The daily loop

git status                # what changed?
git diff                  # exact lines, not staged yet
git add file.txt          # or: git add .
git diff --staged         # what will be committed
git commit -m "Fix broken link on pricing page"
git log --oneline --graph --all
Enter fullscreen mode Exit fullscreen mode

Small commits, one idea each, with a message that says what and why.

.gitignore, and what to do if you commit a secret

.env
*.pem
node_modules/
venv/
dist/
*.log
Enter fullscreen mode Exit fullscreen mode

Committed a key or password by mistake? Deleting the file is not enough: it's still in the history. Rotate it first (create a new key, revoke the old one), then clean the history.

Branches: a parallel timeline for every feature

git switch -c feature/x   # create a branch and move to it
git branch                # list; * marks the current one
git switch main           # go back
git branch -d feature/x   # delete after merging
Enter fullscreen mode Exit fullscreen mode

main should always work. New features and fixes happen on branches.

Merging, and what <<<<<<< HEAD actually means

git switch main
git merge feature/x
# CONFLICT? Open the file and you'll see:
<<<<<<< HEAD        # main's version
=======             # --- divider ---
>>>>>>> feature/x   # the branch's version
Enter fullscreen mode Exit fullscreen mode

Keep the right code, delete the three marker lines, then git add and git commit. To cancel the merge: git merge --abort.

  • Rebase (git rebase main) replays your branch on top of main for a straight history. Only rebase branches nobody else uses.
  • Stash: git stash parks unfinished work, git stash pop brings it back.

The undo menu

Command What it does
git restore f Drop unstaged edits to a file
git restore --staged f Unstage, keep the edits
git commit --amend Fix the last commit (before pushing)
git reset --soft HEAD~1 Undo the commit, changes stay staged
git reset --mixed HEAD~1 Undo the commit, changes stay unstaged
git reset --hard HEAD~1 Undo the commit and delete the changes
git revert <id> A new commit that cancels an old one (safe after pushing)

Remotes: push, pull vs fetch, SSH keys

ssh-keygen -t ed25519 -C "you@example.com"   # then add the .pub key on GitHub
git remote add origin git@github.com:user/app.git
git push -u origin main
git fetch                 # download only
git pull                  # fetch + merge
Enter fullscreen mode Exit fullscreen mode

Pull requests and team rules

  • The flow: branch → commit → push → open a PR → review + CI tests → merge → deploy.
  • Branch protection: nobody pushes straight to main. A PR and green tests are required.
  • Trunk-based development (short branches, merged daily) is common today; GitFlow (develop and release branches) shows up in older teams.
  • Semantic versions: 1.4.2 = major . minor . patch. Mark releases with git tag v1.4.2.

🧪 Lab (45 minutes)

Create a merge conflict on purpose, then fix it:

  1. Make a repo with a README.md and commit it.
  2. On a branch, change line 1 and commit.
  3. Back on main, change the same line differently and commit.
  4. Merge the branch, resolve the conflict by hand, and commit.

It's the fastest way to stop being scared of conflicts.

Next class

YAML & JSON: the language every DevOps tool speaks.


I'm Muhammad Khizr Khan, a full-stack & DevOps developer and the founder of Axelink Systems. More of my work is on my portfolio, LinkedIn and GitHub. Questions about Git? Ask in the comments.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

This is a solid class — and the undo menu is the part I would bookmark, because that is the table you reach for at 1 a.m. when something is already wrong. Two things stood out: "rotate it first, then clean the history" is the right order for a committed secret and most tutorials get it backwards, and the lab that makes you create a merge conflict on purpose is the fastest way to stop fearing them. One question: when a secret lands on a branch other people have already pulled, do you rewrite the history and force-push, or rotate the secret and accept the old commit stays in the log — and what decides it for you?