git
version control
branches
pull
git-commands

How to pull from a local branch into another one?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To incorporate changes from one local branch into another, use git merge or git rebase — not git pull (which is for fetching from remotes). Switch to the target branch and run git merge source-branch to merge, or git rebase source-branch to replay your commits on top of the source. The term "pull from a local branch" is a common misnomer; the correct operations are merge and rebase.

Merge a Local Branch

bash
1# You are on feature-branch and want changes from main
2
3# Step 1: Switch to the target branch
4git checkout feature-branch
5
6# Step 2: Merge the source branch into it
7git merge main

This creates a merge commit that combines the histories of both branches. The feature branch now has all of main's changes.

Fast-Forward Merge

If feature-branch has no new commits since it diverged from main, Git performs a fast-forward merge:

bash
git checkout feature-branch
git merge main
# "Fast-forward" — no merge commit created, just moves the pointer

To always create a merge commit (even for fast-forwards):

bash
git merge --no-ff main

Rebase onto a Local Branch

bash
1# You are on feature-branch and want to replay your commits on top of main
2
3git checkout feature-branch
4git rebase main

Rebase replays each commit from feature-branch on top of main's latest commit, creating a linear history. After rebasing, your feature branch appears as if it was started from the latest main.

bash
1# Before rebase:
2# main:    A - B - C
3# feature: A - B - D - E
4
5# After rebase:
6# main:    A - B - C
7# feature: A - B - C - D' - E'

Merge vs Rebase

bash
1# Merge: preserves branch history, creates a merge commit
2git checkout feature
3git merge main
4# History: A - B - D - E - M (merge commit)
5#               \       /
6#                C ----
7
8# Rebase: linear history, no merge commit
9git checkout feature
10git rebase main
11# History: A - B - C - D' - E'
Aspectgit mergegit rebase
HistoryNon-linear (merge commits)Linear
Original commitsPreservedRewritten (new hashes)
ConflictsResolved onceResolved per commit
Safe for shared branchesYesNo (rewrites history)

Using git pull with a Local Branch

Technically, git pull can pull from a local branch, but this is unusual:

bash
1# This fetches and merges from a local branch (rarely used)
2git pull . main
3
4# Equivalent to:
5git fetch . main
6git merge FETCH_HEAD

The . tells Git to use the local repository as the remote. This is a valid command but confusing — use git merge directly instead.

Cherry-Pick Specific Commits

To bring only specific commits from another branch:

bash
1# Copy a single commit to your current branch
2git cherry-pick abc1234
3
4# Copy a range of commits
5git cherry-pick abc1234..def5678
6
7# Copy multiple specific commits
8git cherry-pick abc1234 def5678 ghi9012

Keeping Feature Branch Up to Date

A common workflow is regularly merging main into your feature branch:

bash
1# On feature-branch, pull latest from remote main and merge
2git fetch origin
3git merge origin/main
4
5# Or if main is already up to date locally
6git merge main

Rebase Workflow

bash
1# Fetch latest main from remote
2git fetch origin
3
4# Rebase feature branch on top of latest main
5git checkout feature-branch
6git rebase origin/main
7
8# If conflicts arise during rebase:
9# 1. Fix conflicts in the files
10# 2. Stage the resolved files
11git add resolved-file.txt
12# 3. Continue the rebase
13git rebase --continue
14
15# Or abort the rebase entirely
16git rebase --abort

Handling Merge Conflicts

bash
1git checkout feature-branch
2git merge main
3
4# If conflicts:
5# Auto-merging file.txt
6# CONFLICT (content): Merge conflict in file.txt
7
8# 1. Open conflicted files and resolve
9# Look for <<<<<<< ======= >>>>>>> markers
10
11# 2. Stage resolved files
12git add file.txt
13
14# 3. Complete the merge
15git commit
16# Git opens editor with default merge commit message

Using a Merge Tool

bash
1# Configure a merge tool
2git config --global merge.tool vimdiff
3
4# Launch it during conflicts
5git mergetool

Updating Multiple Branches

bash
1# Update main, then merge into feature
2git checkout main
3git pull origin main
4
5git checkout feature-branch
6git merge main
7
8# Shortcut without switching branches (Git 2.5+)
9git fetch origin main:main  # Update local main from remote
10git merge main               # Merge into current branch

Common Pitfalls

  • Using git pull instead of git merge: git pull is designed for fetching from remote repositories. For local branch-to-branch operations, use git merge or git rebase directly. git pull . branch works but is confusing and non-standard.
  • Rebasing shared branches: Never rebase a branch that other people are working on. Rebase rewrites commit hashes, and others who based work on the original commits will encounter conflicts. Only rebase local, unpushed branches.
  • Forgetting to commit before merging: If you have uncommitted changes, git merge may refuse to run or create a messy merge. Commit or stash your changes before merging: git stash, then git merge, then git stash pop.
  • Not resolving all conflicts: After fixing conflicts, you must git add every resolved file before committing. If you forget a file, the merge commit includes conflict markers (<<<<<<<) in the file.
  • Merge commits polluting history: Frequently merging main into a feature branch creates many merge commits. Consider using git rebase instead for a cleaner history, or squash-merge the feature branch when it is complete: git merge --squash feature-branch.

Summary

  • Use git merge source-branch to bring changes from a local branch into the current branch
  • Use git rebase source-branch for a linear history without merge commits
  • git pull is for remotes — use git merge for local branch operations
  • Use git cherry-pick to copy specific commits instead of merging everything
  • Resolve merge conflicts by editing files, staging with git add, and completing with git commit
  • Only rebase branches that have not been pushed or shared with others

Course illustration
Course illustration

All Rights Reserved.