简体   繁体   English

如何使用 GitLab CI/CD 合并分支?

[英]How do I merge branches using GitLab CI/CD?

job1:
    stage: build
    script:
        - echo 'Hello'
        - git branch
        - git merge cicd
    tags:
        - cicd

I want to branch merge when I do a job,The following error occurs after executing the job command做作业时想分支合并,执行作业命令后出现如下错误

merge: cicd - not something we can merge
Did you mean this?
    origin/cicd

I also tried using git checkout master and got the following error我也尝试使用git checkout master并得到以下错误

error: pathspec 'master' did not match any file(s) known to git
job1: stage: build script: - echo 'Hello' - git branch - git merge cicd tags: - cicd

I want to merge a branch when I run a job, but the following error occurs after executing the job command:运行job时想合并一个分支,但是执行job命令后出现如下错误:

 merge: cicd - not something we can merge Did you mean this? origin/cicd

Firstly, note that by default, GitLab CI uses shallow clones in order to be faster and to use less resources, which means that it does not fetch all remote references.首先,请注意,默认情况下, GitLab CI 使用浅克隆是为了更快并使用更少的资源,这意味着它不会获取所有远程引用。 In which case, it will likely not fetch all the commits necessary to do a proper merge and thus you will have to disable shallow cloning .在这种情况下,它可能不会获取进行正确合并所需的所有提交,因此您将不得不禁用浅克隆

Secondly, by default git does not create any branch (other than master /the default branch for normal clones) when cloning a repository.其次,默认情况下 git 在克隆存储库时不会创建任何分支(除了master / 普通克隆的默认分支)。 The same error will probably happen if you clone the repository from scratch to your machine with git clone URL , for example.例如,如果您使用git clone URL从头开始将存储库克隆到您的机器,则可能会发生相同的错误。 Only references to the remote branches will be fetched by default (such as origin/cicd ).默认情况下,只会获取对远程分支的引用(例如origin/cicd )。

So you can either merge the remote branch directly:所以你可以直接合并远程分支:

git merge origin/cicd

Or create a normal branch from the remote branch and then merge:或者从远程分支创建一个普通分支然后合并:

git branch cicd origin/cicd
git merge cicd

The only difference should be what will be written on the auto-generated merge commit message:唯一的区别应该是将写入自动生成的合并提交消息的内容:


For merging the remote branch directly:直接合并远程分支:

Merge remote-tracking branch 'origin/cicd'合并远程跟踪分支 'origin/cicd'

For merging the local branch:合并本地分支:

Merge branch 'cicd'合并分支'cicd'


I also tried using git checkout master and got the following error:我也尝试使用git checkout master并得到以下错误:

 error: pathspec 'master' did not match any file(s) known to git

The same commands above should work for master .上面相同的命令应该适用于master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM