0

In Bitbucket, I created a team and then a project. I created a (main) repository with only one branch - master, and then I added a team member, who forked the repository.

After that, I pushed a new branch dev to the repository but my team member does not have the dev branch in his own forked repository.

We also do not know how to create an upstream remote branch that will connect their local/forked repository to the main repository because the link for cloning the repo has my name on it and will require me to enter my password on their computer before it is added as an upstream remote.

How can my team member get the dev branch to his own repo in Bitbucket? How can my team member create an upstream remote connected to the main repository in Bitbucket?

This question is specific to Bitbucket, not Github and it regards working with teams in Bitbucket.

orimdominic
  • 995
  • 10
  • 23

2 Answers2

1
git fetch

is the tool for this. (doc)

It gets the new branch refs from the remote, then updates the remote-tracking branches, on your local repo. These branches are not to be confused with your local branches. They are images of the remote branches, for all purposes of diff, etc.

Then, after the fetch, if said coworker wants to work on the branch, and eventually commit on it, he'll have to create a local counterpart with a simple

git checkout <branchName>

With a recent enough git version, this will then automatically create the logical link between these two, setting your config to pull and push from/to its counterpart.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • Thanks. I know how to do this in Github but not in Bitbucket as I am new. My team member is trying to access the dev branch on the upstream (main) repository, not his own remote repository. He has tried your solution with git fetch, but after git branch, he did not see the dev branch – orimdominic Jun 20 '19 at 17:13
  • Yes, because `git branch` only lists local branches. He would see it with `git branch -r`, but as I suggested, he can just `git checkout ` and afterwards it'll appear with a simple `git branch` – Romain Valeri Jun 20 '19 at 17:16
  • We just refreshed now but the dev branch is not on his forked repository. On running `git branch -r` we saw only the branches he created @RomainValeri – orimdominic Jun 20 '19 at 17:20
  • @sudo_kaizen If so, something's not right, something you didn't mention here and is left out of the picture. He *should* see the new branch after a fetch from a repo which has it. Maybe double-check if he's fetching from the proper remote, not another one? (`git fetch --all` would be the sledgehammer approach here, all remotes are fetched) – Romain Valeri Jun 20 '19 at 17:22
  • Thank you very much for your continuous support. My team member forked the main repo and cloned his forked repo to his local, so his origin is his forked repo. I have updated the question to explain the issue more clearly. COuld you please check it out? Thanks again! @RomainValeri – orimdominic Jun 20 '19 at 17:28
  • @sudo_kaizen OK, I get it. He's succesfully asking his fork : "hey, new branches?" to which his fork answers : "nope." (and rightfully so). He has to set *your* own github repo (the origin of the fork itself) as 'upstream' or something, and fetch from it. Do you know [how to](https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emaddem) set a remote? – Romain Valeri Jun 20 '19 at 17:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195296/discussion-between-sudo-kaizen-and-romainvaleri). – orimdominic Jun 20 '19 at 17:34
0

The best way is locally also connect to the main repo (let's call that upstream. The fork online is already a remote called origin)

You could add the main repo as follows

git remote add upstream yourGitRepoUrl

If you check the remotes, you will 2 remotes (origin and upstream, both push and pull)

git remote -v

Now you could do

git fetch upstream

And

git checkout dev

Note, If your fork already have a dev and you need only the one from upstream, you could do

git checkout dev2 upstream/dev

This will create a new branch local, tracked to upstream's dev

Julian
  • 33,915
  • 22
  • 119
  • 174
  • Thanks, but are you talking about Bitbucket or Github? This works for Github, but in Bitbucket, there a private repos. The `git remote add upstream yourGitRepoUrl` line doesn't work. The `yourGitRepoUrl` requests for the password of the owner of the `yourGitRepoUrl` repo before upstream can be fetched. The question is specific to Bitbucket – orimdominic Jun 23 '19 at 06:19
  • If the repo is private, then you will always need a login (e.g. password ). That's the same for bitbucket and GitHub. Another way is that you push the dev branch to the other fork? Or email the repo https://stackoverflow.com/questions/2545765/how-can-i-email-someone-a-git-repository#2545784 – Julian Jun 23 '19 at 08:55
  • Yes, one would always need a login to access a private repo. The repo is the team's repo, and members of the team should have access to it when they have been give access rights. I gave my team member access rights when I shared the repo and added him. He was able to fork the repo but he cannot access the upstream (main) from which he forked. What I consider most important now is how a team member can access the upstream through a url from his local @Julian Thanks irrespective. – orimdominic Jun 23 '19 at 15:35