0

I am quite new in git and I am trying to figure out the following situation. There is a repo in bitbucket containing the master code and some branches where a couple of colleagues are working. After having clone that repo into a public local folder F (that is somehow available to everybody) now and again I want to pull the master in the public folder F, but I only want to pull the master. I want to avoid that in F the users can navigate to the different branches where code is still not ready for production.

obviously I tried simply git pull but this downloaded everything including the branches in the remote origin.

After going over the modifiers of git pull in https://git-scm.com/docs/git-pull I can not figure out which one I have to use.

JFerro
  • 3,203
  • 7
  • 35
  • 88
  • Does this answer your question? [How do I clone a single branch in Git?](https://stackoverflow.com/questions/1778088/how-do-i-clone-a-single-branch-in-git) – mkrieger1 Jun 21 '21 at 22:28
  • I slighltly modify the question. The cloning is not a problem because it is a one single action at the very beginning. The difficulty starts for me when wanting to pull to a folder ONLY the master and not any other activity (branches) available in the repo – JFerro Jun 21 '21 at 22:59

2 Answers2

1

You're (commonly) confusing what git pull does. You can think of git pull as a cross between git fetch and git merge. Specifically, git pull without any arguments typically does:

  1. git fetch origin
  2. git merge origin/main

The first one gets all the data, including remote branch labels, from the remote repository but does not merge it into your main branch. So, yes all the remote branch names show up locally if you look at the output of git branch -r. But, they don't change the contents of your local main branch. Only the second command (git merge) does that and it only affects the one branch you merge in. It would require multiple merges to merge in any other branches.

So: the upshot is that git pull is only merging in one upstream branch.

(note: all the above names make assumptions about your remote names, and branch names)

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69
  • perfect. OK, so in order to achieve what I want I need to AVOID that in the fetching part the branches are fetched. I only want to fetch master. I guess I fave to go then git fetch origin master and then go with the second comand. I am going to try it – JFerro Jun 21 '21 at 23:02
  • Yes, the `git fetch` command accepts a `` argument after the remote, so you should be able to specify what you want there. See https://git-scm.com/docs/git-fetch – joanis Jun 21 '21 at 23:28
1

You can do a single-branch clone:

git clone --single-branch --branch master <theURL>

Now this local repo doesn't have any remote branches except origin/master, and doing a git fetch won't automatically download any of them. People listing branches won't see them. It is possible to force a download of another branch — no power on earth can prevent that unless you want to take steps at the server side — but at least it won't be obvious that they exist.

However, having said all that, I think your goal might be mistaken. We work on a project with dozens of branches all of which belong to other companies working on their own versions of the code; only one branch belongs to us. We stay out of everyone else's branches by (get this) staying out of everyone else's branches. Git is cooperative; just tell your people to cooperate.

matt
  • 515,959
  • 87
  • 875
  • 1,141