I've got a project hosted on GitHub which somebody has forked. On their fork, they've created a new branch "foo" and made some changes. How do I pull their "foo" into a new branch also named "foo" in my repo?
I understand they could submit a pull request to me, but I'd like to initiate this process myself.
Assume the following:
Go into the directory for your project. Add a connection to your friend's version of the github repository, if you haven't already. Pull his/her changes. Push them back to your github repository.
The content of the multiple remote repositories can be pulled to the local drive by using the command, `git pull origin` or `git pull upstream`.
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.
Add a new remote (say, other ) in your own repo. Pull other/<branch> changes into your local branch (say, add-other-changes ). Push to your own forked repo ( origin/add-other-changes ). Now, when're you done with add-other-changes branch, create a Pull request & merge it with origin/master .
git remote add coworker git://path/to/coworkers/repo.git git fetch coworker git checkout --track coworker/foo
This will setup a local branch foo
, tracking the remote branch coworker/foo
. So when your co-worker has made some changes, you can easily pull them:
git checkout foo git pull
Response to comments:
Cool :) And if I'd like to make my own changes to that branch, should I create a second local branch "bar" from "foo" and work there instead of directly on my "foo"?
You don't need to create a new branch, even though I recommend it. You might as well commit directly to foo
and have your co-worker pull your branch. But that branch already exists and your branch foo
need to be setup as an upstream branch to it:
git branch --set-upstream foo colin/foo
assuming colin
is your repository (a remote to your co-workers repository) defined in similar way:
git remote add colin git://path/to/colins/repo.git
No, you don't need to add them as a remote. That would be clumbersome and a pain to do each time.
git fetch [email protected]:theirusername/reponame.git theirbranch:ournameforbranch
This creates a local branch named ournameforbranch
which is exactly the same as what theirbranch
was for them. For the question example, the last argument would be foo:foo
.
Note :ournameforbranch
part can be further left off if thinking up a name that doesn't conflict with one of your own branches is bothersome. In that case, a reference called FETCH_HEAD
is available. You can git log FETCH_HEAD
to see their commits then do things like cherry-picked
to cherry pick their commits.
Oftentimes, you want to fix something of theirs and push it right back. That's possible too:
git fetch [email protected]:theirusername/reponame.git theirbranch git checkout FETCH_HEAD # fix fix fix git push [email protected]:theirusername/reponame.git HEAD:theirbranch
If working in detached state worries you, by all means create a branch using :ournameforbranch
and replace FETCH_HEAD
and HEAD
above with ournameforbranch
.
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