Git and GitHub

Backend Engineering

Git and GitHub

Git Branching and Workflow

Git Branching and Workflow

Branching is a key feature that makes Git a powerful tool for version control. It allows multiple developers to work on different features or bug fixes simultaneously without interfering with each other's work. This section will cover the fundamentals of Git branching and the common workflows used in development.

Introducing Branching

In Git, a branch represents an independent line of development. Creating a new branch allows you to work on a feature or bug fix in isolation from the main codebase. Once your work is complete, you can merge the branch back into the main branch.

Example:

# Create a new branch named "feature-branch"
git branch feature-branch

# Switch to the new branch
git checkout feature-branch

The Main/Master Branch

The main or master branch is the default branch in most Git repositories. It is the primary branch where the source code of HEAD always reflects a production-ready state.

Example:

# Create a branch named "main"
git branch main

# Rename the default branch from "master" to "main"
git branch -m master main

Overview of HEAD

HEAD is a pointer that always points to the latest commit in the current branch. When you switch branches, HEAD points to the latest commit in the new branch.

Example:

# Show where HEAD is pointing
git log --oneline --decorate

# Move HEAD to the previous commit
git checkout HEAD^

Viewing Branches with Git Branch

Using the git branch command, you can view all the branches in your repository. It lists both local and remote branches.

Example:

# List all local branches
git branch

# List all remote branches
git branch -r

# List all branches (local and remote)
git branch -a

Creating and Switching Branches

Creating a new branch and switching between branches are common tasks in Git. This allows you to work on multiple features or fixes simultaneously.

Example:

# Create and switch to a new branch
git checkout -b new-branch

# Switch to an existing branch
git checkout existing-branch

Git Checkout and Git Switch

The git checkout command is used to switch branches or restore working tree files. The newer git switch command is more intuitive and specifically designed for this purpose.

Example:

# Using git checkout to switch branches
git checkout branch-name

# Using git switch to switch branches
git switch branch-name

# Using git switch to create and switch to a new branch
git switch -c new-branch

Deleting and Renaming Branches

Managing branches includes deleting no longer needed branches and renaming branches for clarity.

Example:

# Delete a local branch
git branch -d branch-name

# Force delete a branch (if it has unmerged changes)
git branch -D branch-name

# Rename a branch
git branch -m old-branch-name new-branch-name

How Git Stores HEAD & Branches

Git stores branch information in the .git directory. The HEAD file contains a reference to the current branch, and each branch is a file in the .git/refs/heads directory.

Example:

# View the contents of the HEAD file
cat .git/HEAD

# View the references of branches
ls .git/refs/heads/

Branching Workflow

Feature Branch Workflow

Each new feature is developed in a dedicated branch in the feature workflow. This isolates the feature work from the main branch, ensuring that the main branch remains stable.

  1. Create a Feature Branch:

    git checkout -b feature-branch
    
  2. Work on the Feature:

    • Make changes and commit them to the feature branch.

    git add .
    git commit -m "Implement feature"
    
  3. Merge the Feature Branch into Main:

    • Once the feature is complete, merge it into the main branch.

    git checkout main
    git merge feature-branch
    

Git Flow Workflow

Git Flow is a well-defined branching model that defines a strict branching structure. It uses different branches for feature development, releases, and hotfixes.

  1. Feature Branches:

    git checkout -b feature/feature-name
    
  2. Release Branches:

    git checkout -b release/release-name
    
  3. Hotfix Branches:

    git checkout -b hotfix/hotfix-name
    

Example Workflow

  1. Start a New Feature:

    git checkout -b feature/login
    
  2. Finish the Feature:

    git checkout main
    git merge feature/login
    
  3. Create a Release Branch:

    git checkout -b release/v1.0
    
  4. Fix a Bug on Main Branch:

    git checkout -b hotfix/critical-bug
    
    # Fix the bug and commit changes
    git checkout main
    git merge hotfix/critical-bug
    

By mastering branching and workflows in Git, you can efficiently manage parallel development, ensure code stability, and streamline collaboration. These practices are essential for maintaining a well-organized and productive development environment.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board