← 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:
git push origin mainremote: 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
mainback 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:
- You made 3 commits on
main. The remote rejects the push becausemainis protected. git checkout -b feature/user-authenticationcreates a new branch name. It points to the same commits.git reset --hard origin/mainmoves localmainback. The commits now live only on the feature branch.git push -u origin feature/user-authenticationsends the feature branch to the remote.- Create a merge request. After review, your commits go into
mainthe right way.
Local main is ahead of remote main by a few commits. The remote won’t accept the push because main is protected.
The plan:
- Move your commits to a new feature branch
- Reset local
mainto match remote - Push the feature branch
- 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#
git status
git log --oneline -5This tells you:
- Which branch you’re on
- How many commits ahead you are
- What your recent commits look like
Example output:
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 handlingThose 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:
git checkout -b feature/user-authenticationReplace 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#
git log --oneline -5All 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:
# 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/mainThis 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#
git status
git log --oneline -5Expected output:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleanMain is clean again.
Step 6: Push Feature Branch and Create Merge Request#
Switch back and push:
# Switch to feature branch
git checkout feature/user-authentication
# Push to remote (creates new branch on GitLab/GitHub)
git push -u origin feature/user-authenticationThe -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:
# If you have GitLab CLI installed
glab mr create --title "feat: Add user authentication" --description "Adds JWT-based authentication for API endpoints"GitHub:
# 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#
- You made 3 commits on
main. The remote rejects the push becausemainis protected. git checkout -b feature/user-authenticationcreates a new branch name. It points to the same commits.git reset --hard origin/mainmoves localmainback. The commits now live only on the feature branch.git push -u origin feature/user-authenticationsends the feature branch to the remote.- Create a merge request. After review, your commits go into
mainthe right way.
- 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 statusonmainshows “up to date with origin/main” -
git logonmaindoesn’t show your recent commits -
git checkout feature/user-authenticationswitches to feature branch -
git logon 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:
[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:
# Old way (easy to forget checkout)
git checkout main
git pull
git checkout -b feature/my-feature
# New way (one command)
git start my-feature2. Pre-Commit Hook to Warn on Main#
Create .git/hooks/pre-commit in your repository:
#!/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
fiMake it executable:
chmod +x .git/hooks/pre-commitThis 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:
- Create feature branch from current position (saves commits)
- Reset local main to remote (cleans up main)
- Push feature branch (puts the work in the right place)
- 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:
- Git Branching Strategy: A Practical Case - The workflow this fix is part of
- GitLab CI/CD for Astro on Cloudflare - Branch protection in action with CI/CD