0

I am trying to use "git checkout -b" to create a new branch off of a public repository. I was reading something about this at What is the difference between "git branch" and "git checkout -b"?. Tuong Le provided this solution:

git checkout -b [NEW_BRANCH] [FROM_BRANCH]

He added that if there's no FROM_BRANCH, git will use the current branch.

I am trying to figure out what to use in [FROM_BRANCH].

When from my local computer I use git remote -v, I get this:

origin  ssh://[username]@[domain]/~[username]/public_html/public.git (fetch)
origin  ssh://[username]@[domain]/~[username]/public_html/public.git (push)

I use SSH to login to the server where I have that public repository and I do this:

# git remote -v
origin  /home/[Unix user]/public_html/app (fetch)
origin  /home/[Unix user]/public_html/app (push)
# pwd
/home/[Unix user]/public_html/[path to source code]
# git branch
  D
  newheader
  responsivenavigation
  bluesidebar
* master
  reports

I know that if I simply use git checkout -b mynewbranch without specifying [FROM_BRANCH] it is going to create a new branch off of master because master is the current branch. That is not what I want. I want to create a new branch off of a branch called mynewbranch that I have in this public repository: /home/[username]/public_html/public.git. That is a remote repository that when I use git branch -a in my local computer, I see it as remotes/origin/mynewbranch. Thank you.

UPDATE 1: I could easily use this from my local computer: git checkout -b mynewbranch remotes/origin/mynewbranch. But the thing is that I am not trying to do this from my local computer. I am using SSH to access the server, and from the server, which is where I have this remotes/origin/mynewbranch, I want to create a new branch off of this remotes/origin/mynewbranch. When I go to the server and use git branch -a, I do not see this remotes/origin/mynewbranch. Where does it reside? How can I create a new branch off of remotes/origin/mynewbranch from the server where this remotes/origin/mynewbranch branch resides? The problem is that when I use SSH to log in to the server and use git branch -a, I do not see this remotes/origin/mynewbranch branch. I was expecting to see it.

UPDATE 2: The syntax is:

git checkout -b [NEW_BRANCH] [FROM_BRANCH]

I am trying to use this:

git checkout -b mynewbranch origin/mynewbranch

I get this error:

error: pathspec 'mynewbranch' did not match any file(s) known to git.
error: pathspec 'origin/mynewbranch' did not match any file(s) known to git.

UPDATE 3: From my local computer I do this:

git branch -a

One of the lines of the results is:

remotes/origin/mynewbranch

Perfect so far. This means that when from my local computer I used git push origin mynewbranch, the command did its job to push the code from my local computer to the public repository. That is why I see this public repository on the server: remotes/origin/mynewbranch. All I want to do now is to checkout on the server the code that I just pushed to this public repository: remotes/origin/mynewbranch. How do I do that? When I log in to the server where I have the public repository, I use SSH and then I try git branch -a. I cannot see remotes/origin/mynewbranch. I want to checkout that branch from the public repository, not from my local computer. How do I do that?

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103
  • 1
    Use `origin/mynewbranch` as `[FROM_BRANCH]`. – mkrieger1 Jul 16 '18 at 18:06
  • 1
    Possible duplicate of [difference between origin/branch\_name and branch\_name?](https://stackoverflow.com/questions/26125162/difference-between-origin-branch-name-and-branch-name) – mkrieger1 Jul 16 '18 at 18:11
  • @mkrieger1 I am using `# git checkout -b mynewbranch origin/mynewbranch` and I get this result: `fatal: 'origin/testimonials' is not a commit and a branch 'testimonials' cannot be created from it`. Apparently it is not recognizing `origin`. – Jaime Montoya Jul 16 '18 at 18:13
  • Another post that possibly answers your question: https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch – mkrieger1 Jul 16 '18 at 18:15
  • Are you using that command on the server? You need to use it on your local computer. – mkrieger1 Jul 16 '18 at 18:16
  • @mkrieger1 I am trying to use `git checkout -b [NEW_BRANCH] [FROM_BRANCH]` on the server, yes, in order to create a new branch on the server. But you are saying that I need to use it on my local computer. – Jaime Montoya Jul 16 '18 at 18:20
  • Didn't you say that the branch already exists on the server? I guess then I don't understand what your situation is and what you are trying to do. – mkrieger1 Jul 16 '18 at 18:22
  • Possible duplicate of [How do I check out a remote Git branch?](https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch) – phd Jul 16 '18 at 19:38
  • @mkrieger1 The branch already exists on the server because I did this from my local computer: `git push origin mynewbranch`. I can confirm that the branch already exists on the server because when from my local computer I use `git branch -a`, I see this as one of the branches: `remotes/origin/mynewbranch`. But when on the server I use `git branch`, I do not see the `mynewbranch` listed. – Jaime Montoya Jul 16 '18 at 20:32

3 Answers3

0

If you want to create a branch called mynewbranch that starts at (and tracks) an existing remote branch locally bookmarked as origin/mynewbranch, usually you can just say

git checkout mynewbranch

As long as mynewbranch doesn't exist and origin is the only repository for which you have a remote tracking ref named (repo)/mynewbranch, this will create the local branch and associate it with the tracking ref.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
0

When you are starting to work on an existing remote branch, you need to simply use the --track flag while checking out the branch. eg.

$ git checkout --track origin/feature
Branch feature set up to track remote branch feature from origin.
Switched to a new branch 'feature'
priteshbaviskar
  • 2,139
  • 2
  • 20
  • 27
0

The solution that worked for me was to use the following from the server where I have the public repository:

# git pull /home/[username]/public_html/public.git mynewbranch
Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103