Skip to content
subuilds.dev

← logbuild #9 passed188ca87

How to Fix: Accidentally Committed to Main Branch Instead of Feature Branch

Step-by-step guide to recover when you forget to create a feature branch and commit directly to main. Move your commits safely without losing work.

· 8 min read

The “Oh No” Moment#

You push to main and get this:

bash
git push origin main
bash
remote: GitLab: You are not allowed to push code to protected branches on this project.

You never created a feature branch. All your commits went straight to main.

Your commits are safe, and this takes about 2 minutes to fix.

In Simple Words#

The problem
You made commits on main, and the remote does not accept the push. Your work is stuck on your computer.
The root cause
You forgot to create a feature branch before you started. Your team protects main, so nobody can push to it directly.
The solution
Put a new branch name on your commits. Then move main back to where the remote is. Push the new branch and open a merge request.
Think of it like this
You wrote three pages in the team’s shared notebook, but new pages must go to a reviewer first. You do not write the pages again. You move them into your own folder, put the notebook back as it was, and give the folder to the reviewer.

What Went Wrong?#

Your Git history right now, and how the fix changes it:

  1. You made 3 commits on main. The remote rejects the push because main is protected.
  2. git checkout -b feature/user-authentication creates a new branch name. It points to the same commits.
  3. git reset --hard origin/main moves local main back. The commits now live only on the feature branch.
  4. git push -u origin feature/user-authentication sends the feature branch to the remote.
  5. Create a merge request. After review, your commits go into main the right way.
5 / 5

Local main is ahead of remote main by a few commits. The remote won’t accept the push because main is protected.

The plan:

  1. Move your commits to a new feature branch
  2. Reset local main to match remote
  3. Push the feature branch
  4. Create a merge request

No commits will be lost. We only move them.

The Fix: Move Commits to Feature Branch#

You should be on the main branch with commits that can’t be pushed.

Step 1: Check Your Current Status#

bash
git status
git log --oneline -5

This tells you:

  • Which branch you’re on
  • How many commits ahead you are
  • What your recent commits look like

Example output:

bash
On branch main
Your branch is ahead of 'origin/main' by 3 commits.

a1b2c3d (HEAD -> main) Fix: Update user validation logic
e4f5g6h Add: User authentication endpoint
i7j8k9l Refactor: Database connection handling

Those 3 commits need to move.

Step 2: Create Feature Branch From Current Position#

This creates a branch at the place where you are now. It keeps all your commits:

bash
git checkout -b feature/user-authentication

Replace feature/user-authentication with a name that fits your work. Follow your team’s branch naming rules.

Your new branch now has all your commits, and you’re on it.

Step 3: Verify Feature Branch Has Your Commits#

bash
git log --oneline -5

All your commits should be there. They’re safe on the feature branch.

Step 4: Switch Back to Main and Reset It#

Reset main to match remote:

bash
# Switch back to main
git checkout main

# Reset main to match remote (THIS IS SAFE - your commits are on feature branch)
git reset --hard origin/main

This makes local main the same as origin/main. It removes the commits from main, but they still live on your feature branch.

Step 5: Verify Main Is Reset#

bash
git status
git log --oneline -5

Expected output:

bash
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

Main is clean again.

Step 6: Push Feature Branch and Create Merge Request#

Switch back and push:

bash
# Switch to feature branch
git checkout feature/user-authentication

# Push to remote (creates new branch on GitLab/GitHub)
git push -u origin feature/user-authentication

The -u flag (--set-upstream) creates the remote branch and links your local branch to it. Next time, a plain git push is enough.

Step 7: Create Merge/Pull Request#

Create a merge request (MR) or pull request (PR) on the website, or from the command line:

GitLab:

bash
# If you have GitLab CLI installed
glab mr create --title "feat: Add user authentication" --description "Adds JWT-based authentication for API endpoints"

GitHub:

bash
# If you have GitHub CLI installed
gh pr create --title "feat: Add user authentication" --body "Adds JWT-based authentication for API endpoints"

After you push, both platforms also print a direct link in your terminal.

Visual: What We Just Did#

  1. You made 3 commits on main. The remote rejects the push because main is protected.
  2. git checkout -b feature/user-authentication creates a new branch name. It points to the same commits.
  3. git reset --hard origin/main moves local main back. The commits now live only on the feature branch.
  4. git push -u origin feature/user-authentication sends the feature branch to the remote.
  5. Create a merge request. After review, your commits go into main the right way.
5 / 5
  • main branch: In sync with remote
  • feature branch: Has all your commits, ready for MR
  • Your work: Safe and in the right place

Verification Checklist#

A quick check:

  • git status on main shows “up to date with origin/main”
  • git log on main doesn’t show your recent commits
  • git checkout feature/user-authentication switches to feature branch
  • git log on feature branch shows all your commits
  • Feature branch exists on remote (check GitLab/GitHub UI)
  • Merge request is created and shows your changes

Preventing This (Because We All Forget)#

1. Git Aliases for Common Workflows#

Add these to your ~/.gitconfig:

ini
[alias]
    # Create feature branch and switch to it in one command
    feature = "!f() { git checkout -b feature/$1; }; f"

    # Before starting work, make sure you're on main and up to date
    sync = "!git checkout main && git pull origin main"

    # Start new feature (syncs main, creates feature branch)
    start = "!f() { git checkout main && git pull origin main && git checkout -b feature/$1; }; f"

Usage:

bash
# Old way (easy to forget checkout)
git checkout main
git pull
git checkout -b feature/my-feature

# New way (one command)
git start my-feature

2. Pre-Commit Hook to Warn on Main#

Create .git/hooks/pre-commit in your repository:

bash
#!/bin/bash

# Get current branch name
branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

# Check if on main/master
if [ "$branch" = "main" ] || [ "$branch" = "master" ]; then
    echo "⚠️  WARNING: You're committing directly to $branch!"
    echo "Are you sure you want to do this? (y/n)"
    read -r response
    if [ "$response" != "y" ]; then
        echo "Commit cancelled. Create a feature branch first:"
        echo "  git checkout -b feature/my-feature"
        exit 1
    fi
fi

Make it executable:

bash
chmod +x .git/hooks/pre-commit

This warns you every time you try to commit on main.

Conclusion#

This happens to everyone. Branch protection caught it, so the system works.

The fix:

  1. Create feature branch from current position (saves commits)
  2. Reset local main to remote (cleans up main)
  3. Push feature branch (puts the work in the right place)
  4. Create merge request (the correct workflow)

Your commits were never in danger. Set up the aliases and hooks above so this happens less often.

Related reading: