Last active
July 28, 2025 05:05
-
-
Save 4sskick/9b0bd38c3b1a81ca89365ba325be3f23 to your computer and use it in GitHub Desktop.
Local main Branch Got 19 Unexpected Commits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# π Git Incident Report: Local `main` Branch Got 19 Unexpected Commits | |
## π What Happened | |
One morning, I had already finished developing a feature on a separate branch. Before creating a Pull Request (PR), I merged `main` into my feature branch to make sure everything was up to date. | |
After that, I switched to the `main` branch and ran: | |
```bash | |
git pull | |
``` | |
To my surprise, the local `main` branch suddenly had **19 commits ahead** of `origin/main`, ready to push. Since the `main` branch is protected, I couldnβt push directly. | |
I panicked and tried: | |
```bash | |
git reset | |
``` | |
...but that didn't solve anything. So I decided to analyze the situation step-by-step. | |
## π Investigation | |
### 1. Check commit movements with reflog | |
```bash | |
git reflog | |
``` | |
From there, I saw that commits from the feature branch I had worked on were somehow **merged into `main`** during a brief moment. | |
### 2. Inspect latest commit of `origin/main` | |
```bash | |
git log origin/main -1 --oneline | |
``` | |
Result: | |
``` | |
15fa42562 (origin/main, origin/HEAD) Merged PR 28340: K01335 | |
``` | |
### 3. Compare with local `main` | |
```bash | |
git log main -1 --oneline | |
``` | |
Result: | |
``` | |
3f783009f (HEAD -> main, origin/dev/_falsely_commit, dev/_falsely_commit) Merge branch 'main' of https://visualstudio.com/_git/HRMS2.Mobile | |
``` | |
This confirmed: **local `main` was pointing to a completely different commit** than `origin/main`. | |
## π Compare Commits | |
### Is my local branch behind? | |
```bash | |
git log main..origin/main --oneline | |
``` | |
**Output: (empty)** | |
β This means: Local `main` is **not behind** `origin/main`. | |
### Is my local branch ahead? | |
```bash | |
git log origin/main..main --oneline | |
``` | |
**Output:** | |
``` | |
3f783009f Merge branch 'main'... | |
494a85db7 K1432 - minor style | |
396dd53cd Merge branch 'main' into dev/SAW/K1432 | |
... | |
``` | |
β‘οΈ These were **exactly the same commits** as those in my feature branch `dev/SAW/K1432`. | |
This confirmed that the commits on local `main` were just **accidental merges** from the feature branch, and **didn't belong there**. | |
## β The Fix | |
Since I was confident that those commits already existed in the proper feature branches, I safely reset my local `main` branch to match the remote: | |
```bash | |
git reset --hard origin/main | |
``` | |
All done β the local `main` was clean again with **zero loss**, and the remote commit history remained untouched. | |
## π Notes | |
- If you want to remove commits **permanently**, you can refer to: | |
π [https://gist.github.com/4sskick/7de05ee677a4c5853c744fda476225ef](https://gist.github.com/4sskick/7de05ee677a4c5853c744fda476225ef) | |
- Always consider making a **backup branch** before doing a hard reset: | |
```bash | |
git branch backup-main-before-reset | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment