0
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:

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:

error: pathspec 'master' did not match any file(s) known to git
kelvin
  • 1,421
  • 13
  • 28
YuSheng
  • 97
  • 7
  • 1
    CI/CD usually don't do full clone — they clone enough to test one commit. If you need more commits and branches you have to fetch them yourself. Don't forget to push after merge. – phd Nov 12 '21 at 05:52

1 Answers1

2
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:

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. 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. The same error will probably happen if you clone the repository from scratch to your machine with git clone URL, for example. Only references to the remote branches will be fetched by default (such as 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'

For merging the local branch:

Merge branch 'cicd'


I also tried using git checkout master and got the following error:

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

The same commands above should work for master.

kelvin
  • 1,421
  • 13
  • 28
  • 1
    What is that repo's default branch? It could be `main` or any other name. A name-independent command to find can be found in https://stackoverflow.com/a/67284026/4341322 ;-) – Katrin Leinweber Nov 12 '21 at 07:46