github branching

Git Branching

Navigating Version Control Like a Pro

Prerequisites

Before diving into Git branching, make sure you have:

  1. Basic Computer Setup
  • A computer with internet connection
  • Basic command-line/terminal knowledge
  • Text editor or IDE (Visual Studio Code, Sublime Text, etc.)

2. Software Installation

  • Git installed on your computer
  • Windows: Download from git-scm.com
  • Mac: Use Homebrew or git-scm.com installer
  • Linux: Use package manager (apt-get, yum)

3. GitHub account (free)

Initial Git Configuration

# Set your name
git config --global user.name "Your Name"

# Set your email
git config --global user.email "youremail@example.com"

What is a Branch?

A branch in Git is essentially a lightweight movable pointer to a specific commit in your repository. Think of it like a separate line of development that allows you to:

  • Work on new features without affecting the main project
  • Experiment with code changes safely
  • Collaborate with others without disrupting the main codebase

Why Use Branches?

Isolation of Work

  • You can develop a new feature or fix a bug in isolation
  • Keeps your main branch (typically main or master) clean and stable
  • Allows multiple people to work on different features simultaneously

Typical Branch Workflow:

  1. Create a New Branch
#1 Create and switch to a new branch
git checkout -b feature-new-design

#2 Or do it in two steps
git branch feature-new-design
git checkout feature-new-design
  1. Make Changes
  • Work on your code in this branch
  • Commit your changes as you progress

3. Push the Branch to GitHub

# First time pushing the branch
git push -u origin feature-new-design
  1. Create a Pull Request
  • Go to GitHub website
  • Select your branch
  • Click “Create Pull Request”
  • This allows review and discussion before merging

What Happens with a Single Branch?

If you maintain only one branch and publish it via GitHub Pages:

  • All your changes directly impact the live site
  • No way to test or preview changes safely
  • Risky for development
  • No separation between stable and experimental code

Recommended Workflow:

  1. Keep main branch as your stable, production-ready code
  2. Create feature branches for new developments
  3. Use pull requests to merge changes after review
  4. GitHub Pages can be configured to serve from specific branches (like main or gh-pages)

Example Scenario:

# Start on main branch
git checkout main

# Create a new feature branch
git checkout -b feature-navbar

# Make changes
git add .
git commit -m "Add responsive navbar"

# Push branch to GitHub
git push -u origin feature-navbar

# Create pull request on GitHub website
# After review and approval, merge into main

Pro Tips:

  • Always pull latest changes before creating a branch
  • Use descriptive branch names
  • Keep branches focused on single features or fixes
  • Delete branches after merging to keep repository clean

Practical Exercise

# Create a Empty Folder named "Demo"
# Open this Folder in VS Code & Navigate to your Terminal

# Initialize your Empty Folder 
git init

# Check Status
git status

# Clone a repository
git clone https://github.com/yourusername/your-project
# Eg: git clone https://github.com/REPHEL-0101/hvac.git
# Copy this URL from Code Menu -> HTTPS  tab

# Change your Directory
cd hvac

# Create a new branch
git checkout -b about-modify

# Verify whether the branches are created
git branch

# Check Status again
git status

# Make changes

# Check Status again
git status

# Commit changes by adding your current folder (. means everything)
git add .

# give a message to your commit like "Implement about-modify"
git commit -m "Implement about-modify"

# -m means message

# Check Status again
git status

# All the changes are commited / saved locally
# Now its time to push it to Github also
git push

# fatal error will be prompted
# Because new branch is created in our machine not in github
# Push branch to GitHub
git push --set-upstream origin new_branch

Common Errors and How to Avoid Them

1. Merge Conflicts

What is it?

  • Occurs when same part of file is changed differently in two branches
  • Git can’t automatically decide which change to keep

Prevention Tips:

  • Communicate with team members
  • Pull changes frequently
  • Use git pull before creating new branches
  • Resolve conflicts manually when they occur

2. Pushing to Wrong Branch

Potential Mistakes:

  • Accidentally pushing feature work to main branch
  • Overwriting production code

Safety Measures:

  • Always check current branch before pushing
# Check current branch
git branch

# Switch to correct branch
git checkout feature-branch

3. Orphaned Branches

Problem:

  • Creating branches but never merging or deleting them
  • Repository becomes cluttered

Best Practices:

  • Delete branches after merging
# Delete local branch
git branch -d branch-name

# Delete remote branch
git push origin --delete branch-name

4. Large File Tracking Issues

Challenge:

  • Accidentally committing large files
  • Bloating repository size

Prevention:

  • Use .gitignore file
  • Install Git Large File Storage (LFS)
# Sample .gitignore
node_modules/
*.log
large_videos/

Recommended Learning Path

  1. Learn Basic Git Commands
  • git init
  • git clone
  • git add
  • git commit
  • git push
  • git pull

2. Practice Branching Workflow

  • Create feature branches
  • Make small, focused changes
  • Use pull requests
  • Review and merge code

3. Tools to Learn

  • GitHub Desktop (for visual learners)
  • VS Code Git integration
  • Git CLI (command line)

Warning Signs: When to Be Careful

  • Uncommitted changes when switching branches
  • Multiple people working on same branch
  • Not pulling latest changes regularly
  • Ignoring code review comments

Bonus Tips

  • Use meaningful branch names
  • Keep branches short-lived
  • Commit often, push regularly
  • Learn to use Git GUI tools
  • Practice, practice, practice!

Recommended Resources

  • Official Git Documentation
  • GitHub Learning Lab

Branch out & code fearlessly

Leave a Reply

Your email address will not be published. Required fields are marked *