CampusFlow
DevOpsGit & GitHub

Git & GitHub

Version control is the backbone of DevOps. Learn Git fundamentals, branching strategies, pull requests, and collaborative workflows.

Core Workflow

1git init or git clone
2git add — Stage changes
3git commit — Snapshot changes
4git push — Share with team
5git pull — Sync from remote

Pull 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 repository
git add .Stage all changes
git commit -m "msg"Commit staged changes
git push origin mainPush to remote branch
git pull origin mainPull latest from remote
git branch featureCreate a new branch
git checkout -b featureCreate and switch to branch
git merge featureMerge branch into current
git rebase mainRebase current branch onto main
git log --onelineShow compact commit history
git diffShow unstaged changes
git stashTemporarily save changes

Branching & 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/`.