Skip to content
\n

The same thing happens from CLI:

\n
gitlab project create --name myproj --path myrepo --namespace-id GROUP_ID --default-branch master\n
\n

Expected Behavior

\n

After creation as described, the project object should show default_branch == \"master\" and the branch should show as protected.

\n

Regardless of being created from CLI or SDK.

\n

Actual Behavior

\n

The CLI and SDK both show that the default branch is \"main\". The console says that a default branch cannot be set on an empty repo.

\n

If this is not possible because of how the gitlab package is a thin wrapper over gitlab REST API, the readme in this repo should at least mention such discrepencies between the docs (auto generated from the REST API, shown in the CLI --help output) and actual behavior.

\n

Workaround (possibly a hack)

\n

Call these 2 functions, in order:

\n
def gitlab_project_repo_setup_content(project):\n    try:\n        project.files.get('README.md', ref='main')\n    except GitlabGetError:\n        print('Creating file in repo')\n        project.files.create(\n            {\n                'file_path': 'README.md',\n                'branch': 'main',\n                'content': '# Placeholder file',\n                'commit_message': 'Add README to initialize repository',\n            }\n        )\n\ndef gitlab_project_set_default_branch(project):\n    \"\"\"\n    Workaround for gitlab default branch can only be changed after there is content added:\n    if you set default_branch in the project.create(), it gets ignored, ie project.default_branch will\n    still be 'main'. Same goes for command line. It appears that there HAS to be at least one\n    file in the reop for the default branch to be changed.\n    \"\"\"\n    # verify that that readme exists, this will raise exception if not:\n    project.files.get('README.md', ref='main')\n\n    project.branches.create({'branch': 'master', 'ref': 'main'})\n    project.default_branch = 'master'\n    project.save()\n\n    project.protectedbranches.create({'name': 'master'})\n    project.protectedbranches.delete('main')\n    project.branches.delete('main')\n
\n

After running these 2, the git repo will have only master branch, and gitlab console will show this and that it is default and protected,

\n

Specifications

\n","upvoteCount":1,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"

Maybe try with initialize_with_readme?

","upvoteCount":1,"url":"https://github.com/python-gitlab/python-gitlab/discussions/3182#discussioncomment-12957077"}}}
Discussion options

You must be logged in to vote

Maybe try with initialize_with_readme?

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by schollii
Comment options

You must be logged in to vote
2 replies
@JohnVillalovos
Comment options

@schollii
Comment options

Comment options

You must be logged in to vote
3 replies
@JohnVillalovos
Comment options

@schollii
Comment options

@JohnVillalovos
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #3181 on April 26, 2025 20:41.