DevOps Peach

DevOps & CI/CD · Git & GitHub · Lesson 3 of 4

Common Git Commands

Create and switch branches with git switch -c (or checkout -b), push new branches with push -u, update with pull --rebase, view history with log --oneline --graph, park work with stash and mark releases with tags.

Practice interactively →

Group Commands Notes
Start git init -b main, git clone URL, git remote add origin URL clone only the first time
Record git add, git commit -m, git diff, git log --oneline --graph
Branch git branch (list), git switch -c feature/login Old way git checkout -b; git checkout BRANCH only switches, it does not create
Sync git push -u origin feature/login, git pull --rebase -u = --set-upstream; afterwards a plain git push
Combine git merge feature/login, git rebase main Do not rebase shared branches
Other git stash / git stash pop, git tag v1.0.0, git push origin v1.0.0 Tags are not pushed automatically
gitGraph
  commit id: "init"
  commit id: "readme"
  branch feature/login
  checkout feature/login
  commit id: "login form"
  commit id: "validation"
  checkout main
  commit id: "hotfix"
  merge feature/login
  commit id: "v1.0.0" tag: "v1.0.0"

Traps: mixing up pull (fetch down) and push (send up); running git pull origin master when the default branch is already main.

Command Meaning
git switch -c feature/login Create and switch to a branch
git push -u origin feature/login Push a new branch, set upstream
git pull --rebase Update with a straight history

Undoing things safely

Situation Command Effect
Fix the last commit message or add a forgotten file git commit --amend Rewrites the last commit; only before pushing
Undo a pushed commit git revert <sha> New commit that reverses it; history stays intact
Move a branch back, keep changes staged git reset --soft HEAD~1 Uncommits, files stay staged
Throw away local commits and changes git reset --hard origin/main Destructive; check git status first
Recover a "lost" commit git reflog then git switch -c rescue <sha> The reflog remembers where HEAD has been

Rule of thumb: on shared branches use revert; rewrite history (amend, reset, rebase) only on commits nobody else has pulled.

Resolving a merge conflict

  1. git merge feature/login (or a rebase) stops with CONFLICT and lists the files.
  2. Open each file; Git marks the two versions between <<<<<<<, =======, and >>>>>>>.
  3. Edit to the correct final content and delete the markers.
  4. git add the files, then git commit (or git rebase --continue).

git merge --abort or git rebase --abort returns to the state before you started.

Useful everyday tools

Writing good commits

Keep each commit focused on one change, write a short imperative subject ("Add login form validation"), and explain why in the body when it is not obvious. Many teams follow Conventional Commits (feat:, fix:, docs:), which tools use to generate changelogs and version numbers.

Practice interactively →