How to pull from a local branch into another one?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
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:
To always create a merge commit (even for fast-forwards):
Rebase onto a Local Branch
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.
Merge vs Rebase
| Aspect | git merge | git rebase |
| History | Non-linear (merge commits) | Linear |
| Original commits | Preserved | Rewritten (new hashes) |
| Conflicts | Resolved once | Resolved per commit |
| Safe for shared branches | Yes | No (rewrites history) |
Using git pull with a Local Branch
Technically, git pull can pull from a local branch, but this is unusual:
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:
Keeping Feature Branch Up to Date
A common workflow is regularly merging main into your feature branch:
Rebase Workflow
Handling Merge Conflicts
Using a Merge Tool
Updating Multiple Branches
Common Pitfalls
- Using
git pullinstead ofgit merge:git pullis designed for fetching from remote repositories. For local branch-to-branch operations, usegit mergeorgit rebasedirectly.git pull . branchworks 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 mergemay refuse to run or create a messy merge. Commit or stash your changes before merging:git stash, thengit merge, thengit stash pop. - Not resolving all conflicts: After fixing conflicts, you must
git addevery 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 rebaseinstead for a cleaner history, or squash-merge the feature branch when it is complete:git merge --squash feature-branch.
Summary
- Use
git merge source-branchto bring changes from a local branch into the current branch - Use
git rebase source-branchfor a linear history without merge commits git pullis for remotes — usegit mergefor local branch operations- Use
git cherry-pickto copy specific commits instead of merging everything - Resolve merge conflicts by editing files, staging with
git add, and completing withgit commit - Only rebase branches that have not been pushed or shared with others
Related reading
- How to pull remote branch from somebody else's repo
- How to push a docker image to a private repository
- How to push different local Git branches to Heroku/master
- How to re-commit a past commit if someone overwrote my commit
- How to read the output from git diff?
- How to rebase local branch onto remote master
- How to rebase local branch onto remote master
- How to recover stashed uncommitted changes
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.