Git Branches
Understanding Branches in Git
A branch in Git represents an independent line of development in a repository. It allows teams to work on features or fixes without disturbing the primary codebase.
Why Use Branches?
Imagine working on a major project where a design update is needed. Without Git, you’d need to manually duplicate relevant files, ensure dependencies are intact, and manage multiple versions—resulting in a chaotic workflow.
With Git, this process becomes streamlined:
- Create a new branch (e.g., new-design). Modify files without affecting the primary branch.
- Encounter a critical bug? Initiate a separate branch (bug-fix), resolve the issue, and merge it into the main branch.
- Resume work on new-design, merge when ready, ensuring all updates are intact.
Branches facilitate seamless context switching, allowing independent work on multiple tasks simultaneously.
Creating a Git Branch
Suppose you’re introducing a new feature to index.html. To avoid disrupting the main repository, generate a branch:
git branch feature-images
Verify creation:
git branch
feature-images
* master
The asterisk (*) highlights the active branch, still on master. Switch to feature-images:
git checkout feature-images
Now, modifications are isolated within feature-images, safeguarding the primary branch.
Making Changes
After switching branches, edits can be made. Assume you add an image (hello_world.jpg) and update index.html:
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Greetings!</h1>
<div><img src="hello_world.jpg" alt="View from Space" style="width:100%; max-width:960px"></div>
<p>Exploring Git branching!</p>
</body>
</html>
Check the status:
git status
This will display:
- Modified files (e.g., index.html).
- Untracked files (hello_world.jpg).
Stage all changes:
git add --all
Confirm the update:
git status
Once satisfied, commit:
git commit -m "Added an image to index.html"
Your branch now holds unique modifications distinct from master.
Switching Between Branches
Switching back to master:
git checkout master
Listing directory contents reveals hello_world.jpg is absent, proving changes remain within feature-images.
Emergency Fixes
If an urgent fix is required while feature-images remains incomplete:
git checkout -b quick-fix
After resolving the issue in index.html:
git add index.html Git commit -m "Resolved urgent bug in index.html"
Merge it into master:
git checkout master git merge quick-fix
Now, master includes the fix without affecting feature-images.
Conclusion
Git’s branching mechanism fosters efficient development, enabling independent feature work, rapid bug resolution, and smooth collaboration. Merging branches integrates modifications seamlessly, ensuring projects evolve without unnecessary disruptions.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand GIT Tutorial visually:
What You'll Learn:
- 📌 Git branches tutorial. How to use Git branches.
- 📌 Git Crash Course #11 - Understanding Branches