Git Branching Reference

Git branching allows you to create independent lines of development within a repository. This reference provides commands and examples for managing branches, including creating, switching, merging, and rebasing branches, as well as resolving merge conflicts and working with remote branches.

Basic Branching Commands

Command Description
git branch List all local branches.
git branch -a List all branches, including remote branches.
git branch <branch_name> Create a new branch.
git checkout <branch_name> Switch to an existing branch.
git checkout -b <branch_name> Create and switch to a new branch.
git branch -d <branch_name> Delete a local branch (only if merged).
git branch -D <branch_name> Force delete a local branch.
git merge <branch_name> Merge a branch into the current branch.
git log --oneline --graph --decorate --all Visualize the branch structure.

Creating and Switching Branches

Create a New Branch

# Create a new branch called 'feature'
git branch feature

Create and Switch to a New Branch

# Create and switch to 'feature' branch
git checkout -b feature

Switch to an Existing Branch

# Switch to 'develop' branch
git checkout develop

Merging Branches

Fast-Forward Merge

When the current branch has not diverged from the target branch:

# Switch to 'main' branch
git checkout main

# Merge 'feature' into 'main'
git merge feature

Three-Way Merge

When branches have diverged:

# Switch to 'main' branch
git checkout main

# Merge 'feature' into 'main'
git merge feature

If there are conflicts, Git will prompt you to resolve them.

Rebasing Branches

Rebasing moves the entire branch to begin on the tip of another branch, creating a linear history.

Rebase 'feature' onto 'main'

# Switch to 'feature' branch
git checkout feature

# Rebase onto 'main'
git rebase main

Interactive Rebase

Modify commits during a rebase:

# Rebase the last 5 commits interactively
git rebase -i HEAD~5

Resolving Merge Conflicts

When merging or rebasing, conflicts may occur if changes overlap.

Steps to Resolve Conflicts

  1. Git will mark conflicting files with conflict markers.
  2. Edit the files to resolve conflicts.
  3. Add the resolved files:
    git add <file>
  4. Continue the merge or rebase:
    # For merge
    git commit
    
    # For rebase
    git rebase --continue

Deleting Branches

Delete a Local Branch

# Delete 'feature' branch (only if merged)
git branch -d feature

Force Delete a Local Branch

# Force delete 'feature' branch
git branch -D feature

Delete a Remote Branch

# Delete 'feature' branch on remote 'origin'
git push origin --delete feature

Working with Remote Branches

List Remote Branches

git branch -r

Fetch Remote Branches

git fetch

Check Out a Remote Branch Locally

# Create a local tracking branch for 'feature' from remote 'origin'
git checkout -b feature origin/feature

Push a Local Branch to Remote

# Push 'feature' branch to 'origin'
git push -u origin feature

The -u option sets the upstream tracking reference.

Branching Strategies

Git Flow

A branching model that uses separate branches for features, releases, and hotfixes.

Feature Branch Workflow

Use dedicated branches for each feature or bug fix:

# Create a feature branch
git checkout -b feature/awesome-feature

After completing the feature:

# Switch to 'develop' branch
git checkout develop

# Merge the feature branch
git merge feature/awesome-feature

# Delete the feature branch
git branch -d feature/awesome-feature

Stashing Changes

Temporarily save changes that are not ready to commit.

Save Changes to Stash

# Save changes with a message
git stash save "Work in progress"

List Stashed Changes

git stash list

Apply Stashed Changes

# Apply the most recent stash
git stash apply

# Apply a specific stash
git stash apply stash@{1}

Pop Stashed Changes

Apply and remove from stash:

git stash pop

Cherry-Picking Commits

Apply a specific commit from one branch to another.

# Switch to target branch
git checkout develop

# Cherry-pick a commit by hash
git cherry-pick <commit_hash>

Resetting and Reverting

Reset to a Previous Commit

Soft Reset: Moves HEAD to a specific commit, keeps changes staged.

git reset --soft <commit_hash>

Mixed Reset (default): Moves HEAD, unstage changes, keeps working directory.

git reset <commit_hash>

Hard Reset: Moves HEAD, discards changes in working directory and staging area.

git reset --hard <commit_hash>

Revert a Commit

Create a new commit that undoes the changes from a previous commit:

git revert <commit_hash>

Bisecting

Use binary search to find the commit that introduced a bug.

Start Bisect

# Start bisecting
git bisect start

# Mark the current commit as bad
git bisect bad

# Mark a known good commit
git bisect good <commit_hash>

Git will checkout commits between the good and bad commits for testing.

Mark Commits During Bisect

# If the commit is bad
git bisect bad

# If the commit is good
git bisect good

End Bisect

# After identifying the bad commit
git bisect reset

Tagging

Create tags to mark specific points in history, such as releases.

Create a Lightweight Tag

git tag v1.0

Create an Annotated Tag

git tag -a v1.0 -m "Version 1.0 release"

Push Tags to Remote

# Push a specific tag
git push origin v1.0

# Push all tags
git push origin --tags

Working with Submodules

Include other Git repositories within a repository.

Add a Submodule

# Add a submodule
git submodule add <repository_url> <path>

Initialize and Update Submodules

# Initialize submodules
git submodule init

# Update submodules
git submodule update

Clone a Repository with Submodules

# Clone and initialize submodules
git clone --recurse-submodules <repository_url>

Additional Resources

Return to Home