Why GitLab Rejects Your First Branch (and How to Fix It Easily)

Starting a new repository should be simple—but sometimes GitLab surprises you with errors when pushing your first branch. One common issue happens when trying to create a protected branch (like dev) directly from a local repository. The result? A rejected push and a confusing message.

Here’s the key idea:
GitLab does not allow protected branches to be created “from nothing.”
They must be based on a branch or commit that already exists on the server.

Many developers used to start like this:

git checkout -b dev
git commit -m "Initial commit" --allow-empty
git push -u origin dev

This worked before, but now GitLab blocks it if dev is protected. The problem is not the empty commit—it’s that the branch has no existing base on the remote.

The correct approach is to create a base branch first:

git checkout -b main
git commit -m "Initial commit" --allow-empty
git push -u origin main

git checkout -b dev
git push -u origin dev

Now dev is created from an existing branch (main), and GitLab accepts it.

Alternative solutions:

  • Temporarily remove protection on dev
  • Create the first branch using the GitLab UI

Takeaway:
Always create at least one branch on the remote before pushing protected branches. This small change avoids errors and keeps your workflow smooth.

git
gitlab
devops
branches
workflow

Comments