Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Git

Git: Recover From Committing to the Wrong Branch by Accident

- 31.08.26 - ErcanOPAK

🔀 Wrong Branch, Right Commit – Easy to Fix

You typed a few commits, went to push, and only then noticed you were still on main instead of your feature branch. Nothing is actually broken – the commits exist and are safe – they’re just sitting on the wrong branch, waiting to be moved.

🐞 The Situation

$ git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
  (use "git push" to publish your local commits)

# Oops - those 3 commits should have gone on
# feature/new-checkout, not main.

🔍 Why This Is Recoverable

Nothing has been pushed yet, so nothing outside your local
repository knows or cares which branch these commits are
attached to. A branch in Git is just a movable pointer to a
commit - moving the pointer for "main" back to where it was,
and creating (or fast-forwarding) a pointer for your feature
branch to where "main" currently sits, fully separates them
with zero data loss.

✅ The Fix: Create the Branch, Then Reset the Original

  • First, create the correct branch AT your current position — this captures all 3 commits before you touch anything: git branch feature/new-checkout
  • Then move main back to where it was before those commits, WITHOUT touching your working files: git checkout main && git reset --hard origin/main
  • Finally switch to the new branch and push it normally: git checkout feature/new-checkout && git push -u origin feature/new-checkout

🚨 If You Already Pushed to main

  • Do NOT force-push over a shared main branch to remove the commits — that rewrites history everyone else has already pulled.
  • Instead, create the feature branch from the current tip as above, then revert the unwanted commits on main with git revert (which adds new, safe commits undoing the change) rather than rewriting history.

A commit on the wrong branch isn’t lost work — branches are just labels, and relabeling costs nothing as long as you haven’t force-pushed over shared history.

— Version Control Tip

Related posts:

Git: Use Submodules for External Repositories

Git: Use Submodules for External Repositories

Git: Use git restore to Unstage Files Without Losing Changes

Git: Use git restore to Unstage Files Without Losing Changes

Git: Use Git Bundle to Transfer Repos Without a Server

Git: Use Git Bundle to Transfer Repos Without a Server

Views: 11
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Ajax: Stop a Long-Running Request From Leaving Your UI Stuck With No Feedback
CSS: Fix Elements That Ignore max-width Because of a Missing box-sizing Reset

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com