DevOpsGit & GitHub
Git & GitHub
Version control is the backbone of DevOps. Learn Git fundamentals, branching strategies, pull requests, and collaborative workflows.
Core Workflow
1
git init or git clone2
git add — Stage changes3
git commit — Snapshot changes4
git push — Share with team5
git pull — Sync from remotePull Request Lifecycle
1Create feature branch from main
2Make commits, push to remote
3Open Pull Request on GitHub
4Code review + CI checks pass
5Merge (squash/rebase/merge commit)
Essential Commands
git initInitialize a new repositorygit add .Stage all changesgit commit -m "msg"Commit staged changesgit push origin mainPush to remote branchgit pull origin mainPull latest from remotegit branch featureCreate a new branchgit checkout -b featureCreate and switch to branchgit merge featureMerge branch into currentgit rebase mainRebase current branch onto maingit log --onelineShow compact commit historygit diffShow unstaged changesgit stashTemporarily save changesBranching & Merging
Branches isolate work. The default branch is main (formerly master).
- Feature branch:
git checkout -b feat/login - Merge:
git checkout main && git merge feat/login - Rebase:
git rebase main(on feature branch) - Squash:
git merge --squash feat/login
Git Workflows
GitFlow
Strict branching model with dedicated branches for features, releases, and hotfixes. Suitable for scheduled releases.
Branches: main, develop, feature/, release/, hotfix/
Trunk-Based
Developers commit directly to main or short-lived branches (hours/days). Relies on feature flags.
Branches: main (short-lived feature branches)
GitHub Flow
Simple workflow: branch from main, open PR, merge back. Every merge deploys to production.
Branches: main, feature branches, PRs
GitLab Flow
Environment branches track deployments. Merge requests promote code between environments.
Branches: main, environment branches (staging/production), feature branches
GitFlow Diagram
main: ─── M ─── M ─── M ─── M ─── M
\ /
develop: ── D ── D ── D ── D ── D ── D
\ /
feature: F ── F ── F main develop feature
Interview Questions
Q1: What is the difference between git merge and git rebase?
`git merge` creates a new merge commit that preserves the history of both branches. `git rebase` rewrites history by replaying commits from the current branch onto the target, creating a linear history. Merge is safer for shared branches; rebase is cleaner for feature branches before merging upstream.
Q2: How do you resolve a merge conflict?
Run `git merge` — Git marks conflicted files. Open each file, find the conflict markers (<<<<<<, ======, >>>>>>), edit to keep the desired changes, remove markers. Stage the resolved files with `git add` and continue with `git merge --continue` or `git commit`.
Q3: What is the difference between `git reset` and `git revert`?
`git reset` moves the HEAD and branch pointer backward, potentially discarding commits. It alters history and should not be used on shared branches. `git revert` creates a new commit that undoes a previous commit — it is safe for shared branches because it does not rewrite history.
Q4: Explain Git hooks and give examples.
Git hooks are scripts that run automatically at specific points in the Git workflow. Examples: `pre-commit` (lint/format check before commit), `pre-push` (run tests before push), `commit-msg` (validate commit message format), `post-receive` (trigger deployment on server). Hooks are stored in `.git/hooks/`.