On the local machine I work in a branch, for example localbranch. In this thread, I finished some function. And so! Done! Now I want to go to the develop branch, and smadzhit it, but since I added only the localbranch branch to the local machine ( git clone --single-branch -b localBranch *url* ) and now I want to go to the develop branch . But I have cloned only one branch, and when I switch to git, it will give me that there is no such wreath (it is understandable, it lies on a remote repo) .

What am I doing in this case. I delete the project I clone develop, after I do merge localBranch.

And, I need that would not have something to remove. And, you just need to add a branch to an existing local repository.

Essentially git clone --single-branch -b develop *url* but in the same folder.

I tried to load the master branch assuming that all existing branches are downloaded from it, but alas not. Should there be a way to add a branch without deleting the existing one?

  • 3
    It seems to me that you want either git branch -u upstream/foo foo or git checkout -b <branch> . But I am very much knocked down by the fact that with one hand you say that you locally do not have a branch that is on the server (develop is on the server, but not locally), then with the other hand you endure "into an already existing locally" with the other hand. Let us figure it out in yourself, put the question in order, and you will not shout with a bolt — this does not help understanding a lot. Do you see for yourself that one part of the question contradicts another? - AK
  • @AK Thank you! Also the right things !!! But not that! - EugeneTM
  • Then probably you need to more thoroughly explain the questions. Advice: instead of bold, you could attach git status and git branch -a - I assure you, it would be much more understandable. - AK
  • @AK highlighted the key points. And, git status and git branch -a in my opinion is not clear. I wrote that the branch that is needed not on the local repo means it would not exist in the git branch -a either. It would seem to me all confused only. Yes, and the answer was given (reshu)! Just what you need. - EugeneTM

4 answers 4

To begin with, assume that all changes are saved by a commit. Those. git status shows that "nothing to commit, working tree clean".

In such cases, I do this:

 git pull 

after which I get the origin / develop branch, then

 git checkout develop 

As a result, I turn to the local branch develop.

In case of a very old git, which may not be able to switch to a branch from origin (and in some versions of git pull I downloaded only the current branch), I used to do this before:

 git fetch origin develop git checkout FETCH_HEAD -b develop 
  • gives -> git checkout -b FETCH_HEAD master fatal: Cannot update paths and switch to branch 'FETCH_HEAD' at the same time. Did you intend to checkout 'master' which can not be resolved as commit? git checkout -b FETCH_HEAD master fatal: Cannot update paths and switch to branch 'FETCH_HEAD' at the same time. Did you intend to checkout 'master' which can not be resolved as commit? a, git fetch origin develop works - EugeneTM
  • I am sorry, I didn’t put it there first. Try the corrected version. - reshu
  • Thank you so much!!! Works!!! Exactly what I needed !!! - EugeneTM

your problems are caused by the use (rather rarely used in practice) of the - --single-branch option of the clone command.

This leads to the fact that the local storage is configured to receive from the remote only one specified branch .

in this situation, to get another branch (for example, develop ) from the remote repository (by default, the name origin ), you can use the following command, for example (shown and example output):

 $ git fetch origin develop:develop From url-хранилища * [new branch] develop -> develop 

as a result, a local develop branch is created, connected (“tracked”) with the same branch in the remote repository. you can switch to it (with the checkout command), and fully work (perform pull / merge / push , etc., etc.)


syntaxis used for refspec (see $ man git-fetch )

 $ git fetch хранилище ветка1:ветка2 

means in this case that:

  • ветка1 will be received from the specified storage (its local name will be хранилище/ветка1
  • local ветка2 will be created (if absent)
  • ветка2 will be swazed (“tracked”) with хранилище/ветка1
  • Thank! reshu has already answered! Almost the same, but with two teams! This way is more convenient! I'll try! If possible, explain a bit the develop: develop line. - EugeneTM
  • @EugeneTM, branches names - from where and where. - Qwertiy
  • @Qwertiy of develop remote: in develop locate? Do I understand correctly? - EugeneTM
  • @EugeneTM, supplemented the answer - aleksandr barakin
  • @alexander barakin Thank you! Extremely useful information! - EugeneTM

Do git clone - get, the entire repository then create the branch that you need, apply the changes and then git merge . Either that, or I did not understand you.

  • git clone will not work. For example, I do this way git checkout develop, and git tells me that such a branch does not exist. But she is !!! In the Remout repository. Actually, this is the question of how to add it to an already existing local repository, without re-creating the project folder with the desired branch. - EugeneTM
  • one
    All git clone works, but suddenly for some reason he won't have ... - Enikeyshchik
  • @ Enikeyschik I meant that not clone itself will not work, but in my case. Since it will create the same folder inside an existing one. Oh, I don't need this. - EugeneTM
  • git clone needs to be done at the very beginning. Then there will be all the branches. And now no one bothers to clone a separate branch without deleting the existing one. - Enikeyschik

It looks like you do not quite understand the concept of git. You can have as many branches as you like without deleting anything, and switch between them as you like. First you need to make git commit all your local changes to this your localbranch . Then make git checkout the develop branch, then git pull (to pick up new changes in develop from the remote server) and only after that git merge localbranch .

  • This is not what I need! I do now. for example, I do this with git clone --single-branch -b localBranch *url* doing a function there, done. Now I want to go to the develop branch. But I have cloned only one branch and when I switch to git, it will give me that there is no such wreath. - EugeneTM
  • What am I doing in this case. I delete the project I clone develop, after I do merge localBranch. And, I need that would not have something to remove. And, you just need to add a branch to an existing repository. - EugeneTM
  • 2
    You do some kind of game. Take the orign / develop branch from the remote to your local repository. - kot-da-vinci
  • the game here does not even smell! I don’t tell you what you would do, and if you don’t know the answer, you don’t need to advise! - EugeneTM