Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git pulling a branch from another repository?

I have a local git repository which is a clone of a repository on github. Someone has forked the repository and made changes in a new branch on a new repository. I want to move this new branch into my repository (locally to work on it first before merging with the master).

I tried creating a new branch and then pulling from the forked repository but it complains because the new branch is a copy of the master branch as well as the local file changes, so it says

error: Your local changes to the following files would be overwritten by merge.

So how can I pull the branch in the other repository into a new branch on my local repository?

I hope that makes sense. If not, this is my repository: https://github.com/MatthewLM/cbitcoin

As you can see, someone has created a new repository with the branch "linuxBuild": https://github.com/austonst/cbitcoin/tree/linuxBuild

I want that branch on my local repository for MatthewLM/cbitcoin.

How can I do this?

like image 398
Matthew Mitchell Avatar asked Jan 17 '13 16:01

Matthew Mitchell


People also ask

How do I pull a branch from another repo?

You can do this by first stashing your local changes and than pulling that branch. Afterward you can apply your stash. This will create the local branch fork_branch with the same history like <branch> in the fork, i.e. fork_branch will branch off of your master where <branch> does in the fork.

Can you git pull a specific branch?

You can clone a specific branch from a Git repository using the git clone –single-branch –branch command. This command retrieves all the files and metadata associated with one branch. To retrieve other branches, you'll need to fetch them later on.

How do I copy a branch from one Github repository to another?

Simply add the new remote (Organization) to your old repository (master). Once you did it simply push the branch A to the new (organization) repository. Now you should have the new branch A in your new repository. The point is to add new remote and to push the branch to your new repository.

How do I pull a remote branch?

just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

How to pull a particular branch from different Repo in Git?

In order to pull a particular branch from different repo you can use the below git command. git pull <git_url> <branch> --allow-unrelated-histories

What is a git repository?

A git repository contains one or more branches to manage the code efficiently. Sometimes, the git users need to work with the multiple branches simultaneously and require to switch from one branch to another branch with the changes made in the previous branch to the current branch before commit.

How do I pull the origin of a git repository?

$ git pull origin master The command should fetch content from the set remote repository into the local repo. If you have not set remote repo or unsure, use this command to check what the current repo is set in Git Bash by this command: $ git remote –v

How to see all local and remote Git branches?

So, we do not have the new branch on our local Git. But we know it is available on GitHub. So we can use the -a option to see all local and remote branches: git branch -a * master remotes/origin/html-skeleton remotes/origin/master Note: branch -r is for remote branches only.


2 Answers

You need to make sure that git status shows that no unstaged changes exist in your local repository.
You can do this by first stashing your local changes and than pulling that branch. Afterward you can apply your stash.


If you want to re-create the branch structure of the fork in your local repository, you can do the following:

git remote add fork <url of fork> git fetch fork git checkout -b fork_branch fork/<branch> 

This will create the local branch fork_branch with the same history like <branch> in the fork, i.e. fork_branch will branch off of your master where <branch> does in the fork. Additionally, your local branch will now track that branch in the fork, so you can easily pull in new changes committed in the fork.

I think you still need to make sure beforehand that your working copy doesn't contain any changes.

like image 96
Daniel Hilgarth Avatar answered Sep 16 '22 15:09

Daniel Hilgarth


Method without adding remote.

git checkout --orphan fork_branch git reset --hard git pull <url of fork> <branch> 
like image 43
Alex78191 Avatar answered Sep 18 '22 15:09

Alex78191