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?
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.
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.
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.
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.
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
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.
$ 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
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.
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.
Method without adding remote.
git checkout --orphan fork_branch git reset --hard git pull <url of fork> <branch>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With