Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fatal: A branch named 'main' already exists. #1388

Open
nschloe opened this issue Jun 30, 2023 · 7 comments
Open

fatal: A branch named 'main' already exists. #1388

nschloe opened this issue Jun 30, 2023 · 7 comments
Labels
bug 🐦 Something isn't working good first issue 🍩 Good for newcomers

Comments

@nschloe
Copy link

nschloe commented Jun 30, 2023

In a GitHub action, I create a fresh repo with

      - name: Create new GitHub repository
        run: |
          curl \
            --request POST \
            --header "Accept: application/vnd.github+json" \
            --header "Authorization: Bearer ${{ secrets.MY_TOKEN }}"\
            --header "X-GitHub-Api-Version: 2022-11-28" \
            --data '{
              "name": "foobar",
              "private": true,
              "visibility": "private",
            }' \
            --url "https://api.github.com/orgs/<org>/repos"

I'd like to deploy to this repo with

      - name: Deploy to GitHub
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          token: ${{ secrets.MY_TOKEN }}
          repository-name: "<org>/foobar"
          branch: main
          folder: deploy/
          single-commit: true

This then fails with

Checking configuration and starting deployment… 🚦
Deploying using Deploy Token… 🔑
Configuring git…
/usr/bin/git config --global --add safe.directory /__w/baz/baz
/usr/bin/git config user.name nschloe
/usr/bin/git config user.email nschloe@users.noreply.github.com
/usr/bin/git config core.ignorecase false
/usr/bin/git config --local --unset-all http.https://github.com/.extraheader
/usr/bin/git remote rm origin
/usr/bin/git remote add origin ***github.com/<org>/foobar.git
Git configured… 🔧
Starting to commit changes…
/usr/bin/git ls-remote --heads ***github.com/<org>/foobar.git refs/heads/main
Creating worktree…
/usr/bin/git worktree add --no-checkout --detach github-pages-deploy-action-temp-deployment-folder
Preparing worktree (detached HEAD fb01a93)
/usr/bin/git checkout --orphan main
fatal: A branch named 'main' already exists.

Any idea what's going wrong?

@JamesIves
Copy link
Owner

JamesIves commented Jun 30, 2023

Does it work if you turn single-commit to false? This may be access token scope related as cross-deploying to repos with single-commit should work. Can you try increasing the permissions and see if that works?

@nschloe
Copy link
Author

nschloe commented Jun 30, 2023

Does it work if you turn single-commit to false?

Nope, same error.

It does work if I set the branch to anything else but main, e.g.,

branch: main2

@JamesIves
Copy link
Owner

JamesIves commented Jul 20, 2023

Couple of questions:

  • Is main protected?
  • What scopes have you given your PAT?
  • Is the account that the PAT was generated from admin of the org or have admin privileges in the repo?

@nschloe
Copy link
Author

nschloe commented Jul 24, 2023

Is main protected?

When creating a repo via the API as in the original post, there is no code, and no branch main in the repo. It's an empty repo.

What scopes have you given your PAT?

Full access to private repos.

Is the account that the PAT was generated from admin of the org or have admin privileges in the repo?

Yes.

(I don't think permissions are a problem since I can create repos with the key, and it all works if I use main2 as a branch name.)

The error message suggests that github-pages-deploy-actions calls

/usr/bin/git checkout --orphan main

in a repo where a main branch already exists. Not sure at all why it does that. Perhaps the current working directory needs to be changed at some point before that?

@yy4382
Copy link

yy4382 commented Jan 4, 2024

I encountered the same issue. The problem is triggered under two conditions:

  1. The target repo is an empty repo, with no commits.
  2. The selected target branch is an existing branch within the repo (such as the default 'main' branch), but no commits have been made on this branch.

It seems that when there are no commits in the selected branch, this action tries to create it, but this branch actually already exists.

I suspect that the issue may be triggered if a target branch does not have any commits (not just in wholly new repositories), but haven't tested yet.

BTW:

  • my solution: commit some random things in the target repo in target branch to make it not empty
  • the error message is misleading: the error occurs when trying to remove the worktree in the end, and the real failure is in the "info" of the output.

the error message:

/usr/bin/git worktree remove github-pages-deploy-action-temp-deployment-folder --force
Error: The deploy step encountered an error: There was an error creating the worktree: The process '/usr/bin/git' failed with exit code 128 ❌ ❌

@nhuet
Copy link

nhuet commented Jan 11, 2024

I got the same issue when deploying with "single-commit=true" + "clean:false" to the root "/" of an orphan branch (gh-pages).

This works:

      - name: Commit versions.json
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: gh-pages # The branch the action should deploy to.
          folder: versions_folder # The folder the action should deploy.
          target-folder: / # The folder the action should deploy to.
          commit-message: publish documentation
          clean: false  # do not remove other files (including whole doc versions)

This does not work:

      - name: Commit versions.json
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: gh-pages # The branch the action should deploy to.
          folder: versions_folder # The folder the action should deploy.
          target-folder: / # The folder the action should deploy to.
          commit-message: publish documentation
          clean: false  # do not remove other files (including whole doc versions)
          single-commit: true

This worked also:

  • with single-commit:true without clean, and to a subfolder:
      - name: Deploy documentation in a version subfolder on GH pages
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: gh-pages # The branch the action should deploy to.
          folder: docs/build/html # The folder the action should deploy.
          target-folder: /main # The folder the action should deploy to.
          commit-message: publish documentation
          single-commit: true
  • with single-commit:true, to the root, with a clean-exclude pattern
      - name: Deploy documentation in root folder on GH pages 🚀
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: gh-pages # The branch the action should deploy to.
          folder: docs/.vuepress/dist # The folder the action should deploy.
          target-folder: / # The folder the action should deploy to.
          commit-message: publish documentation
          single-commit: true
          clean-exclude: |
            "version/*"

@nhuet
Copy link

nhuet commented Jan 11, 2024

I found why i add examples working and others not working. Actually in the example not working (single-commit:true) i was also doing a checkout of gh-pages (the very branch on which i want to deploy) before in order to parse something. So this is the sequence making the bug:

  • checkout gh-pages
  • do some stuff
  • try to publish on gh-pages (same branch) with "single-commit:true" (without single-commit, it is actually working)
  update-doc-versions:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          ref: gh-pages
      - name: Do some stuff to generate something in versions_folder
        run: ...
      - name: Commit versions.json
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: gh-pages # The branch the action should deploy to.
          folder: versions_folder # The folder the action should deploy.
          target-folder: / # The folder the action should deploy to.
          commit-message: publish documentation
          clean: false  # do not remove other files (including whole doc versions)
          single-commit: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐦 Something isn't working good first issue 🍩 Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants