【问题标题】:How to add local repo to a new remote branch如何将本地仓库添加到新的远程分支
【发布时间】:2017-07-13 21:19:00
【问题描述】:

在这里需要一些帮助:理想情况下,我会创建一个新的 repo, 但我的团队中没有 Bitbucket 权限,而这样做的高级工程师正在为一对夫妇度假更多周。

我的仓库中有一个包含遗留代码的主分支 - 我不想更改它,太多关键系统从那里拉出。

我在我的网络上找到了部署服务器,其中包含许多我想要在版本控制下的有用脚本。

我的解决方案是从 master 创建一个新分支,在该新分支上使用来自部署服务器的脚本 - 与 master 分支完全不同的代码。 (这是高级工程师之前做过的。)

我已经初始化了一个本地 git repo。

我已经添加了远程来源:

git remote add origin https://my.username@bitbucket.site.com/scm/dev.repo.git

我已经通过 bitbucket 的站点添加了一个分支,我将调用 deployment-scripts

我在本地创建了分支deployment-scripts。 (但我在master中签出。)

我害怕做 git fetch 并覆盖关键脚本和所有内容。(或者 fetch 不这样做?)我希望做点什么就像git push origin deployment-scripts 一样,并且有一个不错的新分支,脚本可以在其中运行。

这是正确的路径吗?现在我该怎么做?

【问题讨论】:

    标签: git bitbucket git-remote


    【解决方案1】:

    git fetch 不会覆盖任何内容

    git fetch 只是更新(并创建)本地存储库中对远程存储库的引用。在您的情况下,这将创建一个名为 origin/deployment-scripts 的引用。初始获取将如下所示:

    $ git fetch
    From https://my.username@bitbucket.site.com/scm/dev.repo.git
     * [new branch]      deployment-scripts        -> origin/deployment-scripts
    

    这意味着信息是从给定的 url 中获取的,其中创建了一个名为 deployment-scripts 的新分支,在您的本地存储库中为此创建了一个名为 origin/deployment-scripts 的引用。

    未来的git fetch 可能与此类似:

    $ git fetch
    remote: Counting objects: 3, done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From https://my.username@bitbucket.site.com/scm/dev.repo.git
       fa09a74..03350ff  deployment-scripts        -> origin/deployment-scripts
    

    输出的前三行包含有关获取数据的一些统计信息,接下来是您获取数据的 URL,然后您可以看到分支部署脚本自上次以来已从 fa09a74 更改为 03350ff fetch 和本地引用分别更新。

    您的git push 可能不起作用

    我假设您在 bitbucket 中创建的分支 deployment-scripts 是从某个其他分支分支出来的,因此包含该分支的提交(至少在 Bitbucket 服务器中,无法创建空分支)。在这种情况下,您的推送将失败,因为远程分支和本地分支都具有另一个没有的更改:

    $ git push origin deployment-scripts
    To https://my.username@bitbucket.site.com/scm/dev.repo.git
     ! [rejected]        deployment-scripts -> deployment-scripts (non-fast-forward)
    error: failed to push some refs to 'https://my.username@bitbucket.site.com/scm/dev.repo.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    在这种情况下,需要合并才能将两个版本合并在一起,而推送永远不会发生这种情况。无论如何,这不是您想要实现的,因为结果将是一个分支,其中包含来自您本地 deployment-scripts 分支的文件和作为在 Bitbucket 中创建 deployment-scripts 的基础的分支。

    你如何解决它

    删除分支并通过推送创建它

    您可以只删除 Bitbucket 中的分支 deployment-scripts 并将其从本地存储库中推送,然后它将在 Bitbucket 中创建分支(至少如果您的用户被允许创建分支):

    $ git push origin deployment-scripts
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 250 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To https://my.username@bitbucket.site.com/scm/dev.repo.git
     * [new branch]      deployment-scripts -> deployment-scripts
    

    强制推送分支

    或者,您可以执行 git push --force,它会覆盖 Bitbucket 上的提交,从而强制您的推送工作。

    $ git push --force origin deployment-scripts
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 250 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To https://my.username@bitbucket.site.com/scm/dev.repo.git
     + 524095e...87e6bd7 deployment-scripts -> deployment-scripts (forced update)
    

    注意:正如我所说,这将删除所有在远程 (Bitbucket) 分支中而不是在本地分支中的提交,并且只有在您确定这是正确的做法时才应使用。

    【讨论】:

      猜你喜欢
      • 2021-04-07
      • 2021-05-21
      • 2021-11-02
      • 2012-01-05
      • 1970-01-01
      • 2016-10-09
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多