【问题标题】:Using "git checkout -b" to create new branch off of public repository使用“git checkout -b”从公共存储库创建新分支
【发布时间】:2018-12-24 08:21:59
【问题描述】:

我正在尝试使用“git checkout -b”从公共存储库创建一个新分支。我在What is the difference between "git branch" and "git checkout -b"? 阅读了一些关于此的内容。 Tuong Le 提供了这个解决方案:

git checkout -b [NEW_BRANCH] [FROM_BRANCH]

他补充说if there's no FROM_BRANCH, git will use the current branch

我正在尝试找出在 [FROM_BRANCH] 中使用什么。

当我在本地计算机上使用 git remote -v 时,我得到了这个:

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

我使用 SSH 登录到我拥有该公共存储库的服务器,然后执行以下操作:

# 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

我知道,如果我只使用git checkout -b mynewbranch 而不指定[FROM_BRANCH],它将创建一个新的分支master,因为master 是当前分支。那不是我想要的。我想从我在这个公共存储库中的一个名为 mynewbranch 的分支上创建一个新分支:/home/[username]/public_html/public.git。那是一个远程存储库,当我在本地计算机上使用git branch -a 时,我将其视为remotes/origin/mynewbranch。谢谢。

更新 1:我可以在本地计算机上轻松使用它:git checkout -b mynewbranch remotes/origin/mynewbranch。但问题是我并没有尝试从我的本地计算机上执行此操作。我正在使用 SSH 访问服务器,并且从服务器(我有这个 remotes/origin/mynewbranch 的地方),我想从这个 remotes/origin/mynewbranch 创建一个新分支。当我去服务器并使用git branch -a 时,我看不到这个remotes/origin/mynewbranch。它住在哪里?如何从该remotes/origin/mynewbranch 分支所在的服务器创建一个新的remotes/origin/mynewbranch 分支?问题是当我使用SSH登录服务器并使用git branch -a时,我没有看到这个remotes/origin/mynewbranch分支。我期待看到它。

更新 2:语法是:

git checkout -b [NEW_BRANCH] [FROM_BRANCH]

我正在尝试使用这个:

git checkout -b mynewbranch origin/mynewbranch

我收到此错误:

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.

更新 3:在我的本地计算机上我这样做:

git branch -a

其中一行结果是:

remotes/origin/mynewbranch

到目前为止完美。这意味着当我在本地计算机上使用git push origin mynewbranch 时,该命令完成了将代码从本地计算机推送到公共存储库的工作。这就是为什么我在服务器上看到这个公共存储库:remotes/origin/mynewbranch。我现在要做的就是在服务器上签出我刚刚推送到这个公共存储库的代码:remotes/origin/mynewbranch。我怎么做?当我登录到我拥有公共存储库的服务器时,我使用 SSH,然后我尝试git branch -a。我看不到remotes/origin/mynewbranch。我想从公共存储库中签出该分支,而不是从我的本地计算机中签出。我该怎么做?

【问题讨论】:

  • origin/mynewbranch 用作[FROM_BRANCH]
  • @mkrieger1 我正在使用# git checkout -b mynewbranch origin/mynewbranch,我得到了这个结果:fatal: 'origin/testimonials' is not a commit and a branch 'testimonials' cannot be created from it。显然它无法识别origin
  • 另一个可能回答你问题的帖子:stackoverflow.com/questions/1783405/…
  • 你在服务器上使用那个命令吗?您需要在本地计算机上使用它。

标签: git


【解决方案1】:

如果您想创建一个名为 mynewbranch 的分支,该分支从(并跟踪)本地书签为 origin/mynewbranch 的现有远程分支开始,通常您可以直接说

git checkout mynewbranch

只要mynewbranch 不存在并且origin 是您拥有名为(repo)/mynewbranch 的远程跟踪引用的唯一存储库,这将创建本地分支并将其与跟踪引用相关联。

【讨论】:

    【解决方案2】:

    当您开始在现有的远程分支上工作时,您只需在签出分支时使用 --track 标志。 例如。

    $ git checkout --track origin/feature
    Branch feature set up to track remote branch feature from origin.
    Switched to a new branch 'feature'
    

    【讨论】:

      【解决方案3】:

      对我有用的解决方案是从我拥有公共存储库的服务器中使用以下内容:

      # git pull /home/[username]/public_html/public.git mynewbranch
      

      【讨论】:

        猜你喜欢
        • 2012-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多