Git pulling a branch from another repository?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To pull a branch from another repository, you do not clone everything again. The usual Git workflow is to add that repository as another remote, fetch its branches, and then either create a local branch from the fetched ref or merge, rebase, or cherry-pick from it.
Add the Other Repository as a Remote
Start by giving the other repository a remote name:
Now Git knows about the refs in that repository, such as:
You can inspect them with:
This step is better than copying patches around because it lets Git manage history normally.
Create a Local Branch from the Remote Branch
If you want to work directly on that branch locally:
This creates a new local branch named feature-x starting from the fetched remote-tracking branch.
You can now review, test, and modify the code just like any other local branch.
If you do not want the same local name, pick another one:
Merge the Branch into Your Current Work
If your goal is not to check out the branch directly, but to bring it into your current branch, fetch first and then merge:
That creates a normal merge using the branch from the other repository as input.
This is common when reviewing work from a fork or from a repository that is not your default origin.
Rebase or Cherry-Pick Instead of Merging
You are not limited to merge. Once the branch has been fetched, it behaves like any other reachable ref.
Rebase example:
Cherry-pick example:
Use these only when they match the history shape you want. Merge is usually the most straightforward option when the goal is simply "bring in that branch."
Pull Directly from a URL When You Must
Git also allows a direct fetch or pull from a repository URL without permanently adding a named remote:
That can be useful for one-off situations, but it is less convenient than a named remote if you expect to interact with the repository repeatedly.
In practice, a named remote is cleaner because you can fetch again later and Git remembers where the branch came from.
Understand the Difference Between fetch and pull
People often say "pull a branch from another repository," but in Git terms:
- '
fetchdownloads refs and objects' - '
pullis usuallyfetchplus merge or rebase into the current branch'
When the repository is not one of your normal remotes, fetch is often the safer first step because it lets you inspect what arrived before deciding how to integrate it.
That is why many experienced Git users avoid a blind pull here and prefer:
Then they decide whether to check out, merge, rebase, or cherry-pick.
Clean Up When You Are Done
If the extra remote was only temporary, remove it:
That keeps your repository configuration tidy. The local branches and commits you already created remain in your repository; removing the remote only removes the remote configuration.
Common Pitfalls
- Trying to
git pullfrom another repository before adding or fetching it as a remote. - Forgetting that
fetchalone does not change your current branch. - Merging a remote branch when you really wanted to inspect it on a local branch first.
- Using cherry-pick for an entire branch when a merge would preserve history more cleanly.
- Leaving one-off remotes around and later forgetting where they came from.
Summary
- The standard way to pull a branch from another repository is
git remote add, thengit fetch. - After fetching, you can check out a local branch from the remote-tracking branch or merge, rebase, or cherry-pick from it.
- '
fetchis usually safer than a blindpullbecause it separates download from integration.' - Named remotes are better than one-off URL fetches when you expect repeated interaction.
- Once the branch is fetched, Git treats it like any other reachable branch reference.
Related reading
- git push --force-with-lease vs. --force
- git push --force-with-lease vs. --force
- Git push --force from IntelliJ IDEA
- Git Push Error insufficient permission for adding an object to repository database
- Git Push Error insufficient permission for adding an object to repository database
- Git push error ''[remote rejected] master -> master (branch is currently checked out)''
- Git push error 'remote rejected master - master branch is currently checked out
- Git Push ERROR Repository not found
.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.