Chapter 3: Branch, Merge, Rebase, Stash and Pull Requests
Chala mitrano, aaj aapan branch, merge, rebase, stash aani GitHub pull request shikuya. Don developers ekach badge file edit kartat asa picture theva — branch mhanje separate draft. Fork, conflict line by line, cherry-pick, reset aani annotated tag. Ghabru naka: ek file, ek conflict.
What you will learn in this chapter
- Why a branch, then create, switch, list and delete
- The feature-branch loop
- Fetch vs pull vs push, once
- Fork, upstream, and a GitHub pull request
- Fast-forward vs a merge commit, then a conflict line by line
- Rebase only when the branch is not pushed; otherwise merge
- Stash, cherry-pick, and reset --soft, --mixed, --hard
- log --graph, diff, show, and an annotated tag
- Common pull-request mistakes
Concepts in this chapter
- 3.1Why branches — two developers, one shared app
- 3.2branch, switch, list, delete
- 3.3Feature-branch workflow (the daily loop)
- 3.4Fetch vs pull vs push
- 3.5Fork, upstream, and the pull request
- 3.6Merge — fast-forward and a merge commit
- 3.7A conflict, line by line
- 3.8Rebase when it is safe — and when to merge instead
- 3.9Stash — park the unfinished edit
- 3.10Cherry-pick one commit
- 3.11reset --soft, --mixed and --hard
- 3.12log --graph, diff and show
- 3.13Annotated tags
- 3.14Common pull-request mistakes
- 3.15Lab — colour, one conflict, one stash
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Apollo Hospitals, the healthcare company, working from Chennai. Dr. Meena Raman leads the team, and we look after the patient appointment app. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Apollo Hospitals is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 10
Easy List branches
Why: You must know which branches exist before you merge or delete.
What to do: List all branches, local and remote.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Easy Create and switch to a branch
Why: Feature work on its own branch keeps main stable.
What to do: Create a new branch feature/patient-appointment-cache and switch to it in one command.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Merge a feature branch
Why: Merging brings tested feature work into main.
What to do: We are on main. Merge the branch feature/patient-appointment-cache into it.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Delete a merged branch
Why: Old branches clutter the repository and confuse the team.
What to do: Delete the local branch feature/patient-appointment-cache, which is already merged.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Park unfinished work
Why: An urgent hotfix should not mix with half-done work.
What to do: Save the unfinished changes aside with the message "wip patient-appointment cache" so the working tree is clean.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Deploy only from main
Why: Feature branches are built and tested but never deployed.
What to do: Declarative pipeline, agent any, stage Deploy with when { branch 'main' } and steps sh './deploy.sh ap-south-1'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Hard Merge with a merge commit
Why: A merge commit keeps a clear record that a feature was merged.
What to do: Merge release/1.9.1 into the current branch and always create a merge commit, even if fast-forward is possible.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Hard Rebase onto main
Why: Rebasing gives a straight, easy-to-read history before a pull request.
What to do: We are on our feature branch. Replay its commits on top of the latest main.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Hard Copy one fix to a release branch
Why: A hotfix on main is also needed on the release branch, without the other commits.
What to do: We are on release/1.9.1. Apply only commit 45432fd from main here.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Hard Delete a remote branch
Why: After the pull request is merged, the remote branch is no longer needed.
What to do: Delete the branch feature/patient-appointment-cache on origin.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Ravi Sir's Cards: 33 revision, interview and comparison cards for this chapter · मराठी cards