I'm working on a tutorial and the tutorial actually provided a git repository, which one was to fork and work with. When I forked the repository, I only forked the master branch, as I assumed that's the only one I need.
The next assignment is to check out a speficic branch from that repository. And Im not quite sure how to do that.
I know that I can simply create a new repo and fork the whole thing again but that would throw me back to the start and somewhat disrupt the flow.
So what I tried is to add the source repo (provided by tutorial) as a remote (next to my forked repo), check out the branch, push it to my own forked repo and rebase it accordingly.
This didnt work.
I did add the source repo as remote and when I run git remote
it is listed there.
However, none of the branches are. When I use git branch
all I see is my own master branch.
I also tried `git fetch -t source/branch but that didnt work either.
I saw this answer and it's basically what I tried but when I try to checkout I get this error:
# German
# fatal: 'source/branch' ist kein Commit und es kann kein Branch 'branch' aus diesem erstellt werden.
# English
# fatal: 'source/branch' is not a commit and branch 'branch' could not be created
Is there any way to do this properly without starting from scratch by forking the source repo agian? Could you explain to me why my approach didn't work?
CodePudding user response:
When I use
git branch
all I see is my own master branch.
Correct, because git branch
lists only local branches that you have created,1 and you have not created any other local branches. But when you say git branch --all
you see all the branches. You can then check any of them out locally.
I'll demonstrate (follow along on your machine if you like). I go to https://github.com/BLeeEZ/amperfy (chosen at random!) and make a fork where, making the same mistake you made, I choose to take only the master
branch. I then clone my fork to my own computer, using the instructions that GitHub gives me:
% git clone [email protected]:mattneub/amperfy.git
% cd amperfy
Now, still pretending to be you, I realize that I needed all the branches; so I decide to add the original as a second remote. It is usual to call this upstream
(not source
) so I will do that:
% git remote add upstream https://github.com/BLeeEZ/amperfy
Now I have to fetch
in order to get the branches from upstream
(perhaps this is the step you forgot? You do not mention it anywhere):
% git fetch --all
Okay, so when I say merely git branch
I see only my own master
branch, just as you say:
% git branch
* master
But when I say git branch --all
, there they all are!
% git branch --all
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/upstream/HysteriaPlayer
remotes/upstream/develop
remotes/upstream/gapless
remotes/upstream/gaplessStreamingKit
remotes/upstream/master
remotes/upstream/miniPlayerNextButton
So you see, I do have all the branches from upstream
, which is my second remote corresponding to your source
.
Finally, let's say I want to check out one of these branches locally. I'll check out gapless
:
% git switch gapless
Presto, I have created a local gapless
branch corresponding to upstream/gapless
. I could now, if desired, work with this branch and ultimately push it to my own fork:
% git push origin gapless
1 You might say: So where did master
come from? I never "created" it. Well, yes you did. When you git clone
, Git kindly saves you a step by checking out the remote's default branch as a local branch, thus creating it for you. You can actually suppress that step when you clone (by saying -n
), thus ending up with no local branches at all. That can actually be a very useful thing to do, and I do it quite often.
CodePudding user response:
I've documented how to work with remote branches originated from PR's while using the Forking Strategy
and how to commit your local changes back to the original fork branch which also updates the PR.
This requires you have write
permission on the remote repo. Otherwise, you can only send a PR to the original author.
I'm using BitBucket server.
https://blog.jeremyfiel.com/bitbucket-pull-pr-and-commit-local-changes-remote-forks