Course: Intermediate Git
I. Composition (13 sentences) Working with branches is a core practice in modern version control systems, especially Git, that enables parallel development without destabilizing the main codebase. Branches act as pointers to commits and represent independent lines of development for features, fixes, or experiments. Creating a branch is quick and reversible, which encourages experimentation and reduces the risk associated with invasive changes. Switching between branches updates your working directory so you can focus on one task at a time while preserving other work. Merging integrates completed changes back into a target branch and may produce fast-forward or three-way merges depending on history. When merges conflict, tools and careful resolution are required to produce a coherent final state. After successful integration, deleting obsolete branches helps keep the repository organized and reduces cognitive load for collaborators. Naming conventions and small, focused branches improve readability and simplify reviews and merges. Regularly pulling and merging from the main branch into a feature branch reduces divergence and minimizes painful conflicts later. Pushing branches to a remote repository enables collaboration and serves as a backup for ongoing work. Pull requests and code reviews provide structured checkpoints before merging to ensure code quality and shared understanding. Practicing branch hygiene—clear names, focused scope, frequent commits—makes teams more productive and resilient. Mastery of branching and merging patterns is foundational for scalable, collaborative software development. II. Structured Lecture Sheet (semantic HTML only)Lesson
Topic: Working with Branches — Introduction to Branches
Learning Outcomes
- Understand what a branch is and why branches are used in Git.
- Create, switch, list, merge, rename, and delete branches using command-line Git.
- Recognize merge types and resolve simple merge conflicts.
- Apply best practices for branch naming, scope, and remote collaboration.
Conceptual Explanation
A branch is a movable pointer to a commit that represents an independent line of development.
Branching isolates work (features, bugfixes, experiments) from the main codebase; merging integrates those changes back.
Typical flow: create a branch, switch to it, make commits, keep it up to date with main, open a pull request or merge locally, resolve conflicts if any, then delete the branch when done.
Use short-lived, focused branches and descriptive names (e.g., feature/login, bugfix/issue-123).
| Action | Command | Purpose |
|---|---|---|
| Create branch | |
Create a new local branch pointer |
| Create & switch | |
Create and immediately switch to the new branch |
| Switch branch | |
Update working directory to branch state |
| List branches | |
Show local (and -a for all) branches |
| Merge | |
Integrate changes from another branch into current |
| Delete branch (local) | |
Remove local branch (safe/force) |
| Rename local branch | |
Rename branch |
| Count branches | |
Count local/remote branches (Unix) |
Bilingual (EN → বাংলা)
Branch শাখা — রিপোজিটরির একটি স্বাধীন ডেভেলপমেন্ট লাইন যা একটি নির্দিষ্ট কমিট পয়েন্ট করে। Merge মার্জ — একটি শাখার পরিবর্তনগুলো অন্য শাখায় একত্রিত করা। Checkout চেকআউট — নির্দিষ্ট শাখায় সুইচ করে ওয়ার্কিং ডিরেক্টরির অবস্থা পরিবর্তন করা। Delete branch শাখা মুছে ফেলা — আর প্রয়োজন না হলে স্থানীয় বা রিমোট শাখা সরিয়ে ফেলা।Important Notes
- Always check your current branch with
git branchbefore making changes. - Pull or merge from
maininto your feature branch frequently to reduce conflicts. - Use remote pushes (
git push origin <branch>) and set upstream (--set-upstream) for collaboration. - Communicate renames and deletions with your team to avoid breaking workflows.
Exercises
True / False
- Creating a branch copies files to a new directory.
Answer
False — creating a branch creates a pointer to a commit; files are not duplicated on disk until changes are made in the working tree. - A fast-forward merge creates a new merge commit.
Answer
False — fast-forward moves the branch pointer and does not create a merge commit. - You must delete a branch before merging it.
Answer
False — deletion is optional and typically done after successful merge.
Fill in the Blanks
- To create and switch to a branch in one command:
Answer
git checkout -b <branch-name> - To see all remote branches use:
Answer
git branch -r - To force-delete a local branch:
Answer
git branch -D <branch-name>
Multiple Choice (MCQ)
- Which command sets the upstream for a renamed local branch?
- a)
git push origin --delete <old> - b)
git push --set-upstream origin <new> - c)
git branch -m <old> <new>
Answer
b)git push --set-upstream origin <new> - a)
- Which strategy is recommended to minimize merge conflicts?
- a) Work in a single long-lived branch for everything
- b) Regularly merge main into feature branches
- c) Never pull from remote
Answer
b) Regularly merge main into feature branches
Matching
Match the command (A–E) with its effect (1–5):
| Command | Effect |
|---|---|
A. |
1. Delete remote branch |
B. |
2. Create and switch to branch |
C. |
3. List all branches |
D. |
4. Delete local branch (safe) |
E. |
5. Rename a local branch |
Answer
Matches: A→4, B→1, C→2, D→3, E→5
Descriptive
Explain the difference between a fast-forward merge and a three-way merge.
Answer
A fast-forward merge happens when the target branch has no new commits since the source branch split; Git simply moves the target pointer forward to the source tip without creating a merge commit. A three-way merge occurs when both branches have new commits since their common ancestor; Git creates a new merge commit that has both branch tips as parents and uses the common ancestor to compute differences.
Creative
Design a brief branching policy for a small team (3–6 developers) using feature branches and pull requests.
Answer
- All work on features/fixes must be done in short-lived feature branches named
feature/...orbugfix/.... - Feature branches should rebase or merge
mainat least daily to stay current. - Open a pull request for every branch; require one approving review and a passing CI build before merging.
- Use squash or merge commits consistently (team decision) and delete branches after merge.
- Hotfixes can be branched from
mainand merged back into bothmainanddevelopif applicable.
Activities, Vocabulary, Homework, Practice Task
Activities
- Pair exercise: create a feature branch, implement a small change, push it, and make a pull request.
Answer
Partners should confirm branch creation (git checkout -b), push (git push -u origin <branch>), and open a PR on the remote platform. - Conflict simulation: both students edit same line, then merge and resolve.
Answer
One student merges the other's branch and resolves the conflict markers, stages, and commits the resolution.
Vocabulary
BranchPointer to a commit representing a line of development. MergeOperation to combine changes from different branches. CheckoutSwitching working directory to a branch or commit. UpstreamRemote branch that local branch tracks for push/pull.Answer
Definitions above provide concise meanings; review them before practical tasks.Homework
- Read the team's chosen branching strategy (Git Flow / Trunk-Based) and write 200 words on when you'd prefer one over the other.
Answer
Submit the write-up with examples: Git Flow for scheduled releases, Trunk-Based for continuous delivery. - Create 3 branches locally, make commits on each, merge them into
mainand document commands used.Answer
Provide a short transcript of commands and outcomes (include any conflict resolutions).
Practice Task
Using a sample repo, demonstrate: clone → create branch → commit change → push → open PR → merge → delete branch. Record commands and results.
Answer
Expected command sequence (example):
git clone https://github.com/you/sample.git
cd sample
git checkout -b feature/example
# edit files
git add .
git commit -m "Add example"
git push -u origin feature/example
# open PR on GitHub, approve & merge
git checkout main
git pull origin main
git branch -d feature/example
git push origin --delete feature/example
Next lesson: Resolving merge conflicts and advanced merge strategies.