Fixing Commits in the Wrong Place

Updated · 12 min read
mediumGit 2.53gitwrong-branchrecipescherry-pickresetinterview

A wrong-place mistake in Git is work that ended up on a different branch from the one it belongs to: committed on main when it should have been on a feature branch, committed on the wrong feature branch, or edited while the wrong branch was checked out. It is one of the most common Git mistakes, and one of the easiest to fix, because a commit is never tied to a branch: a branch is only a pointer, and pointers can be moved. Each recipe below combines commands you already know: git branch, git switch, git reset, git cherry-pick, git stash and git revert.

It is like filing a letter in the wrong folder. You don't rewrite the letter. You take it out of one folder and put it in the other.

Which Recipe Do I Need?

SituationRecipe
Committed on main, meant to start a new branch, not pushed1. Branch here, move main back
Committed on the wrong existing branch, not pushed2. Cherry-pick across, remove from the wrong one
Want to undo the last few commits but keep the changes3. git reset --soft
Uncommitted changes, should be on a new branch4. git switch -c
Uncommitted changes, should be on an existing branch5. Stash, switch, pop
The wrong commit was already pushed6. Revert on the wrong branch, cherry-pick to the right one

Before any recipe, run git status and git log --oneline -5 to see exactly where you are.

Pick the recipe by two questions

The Starting Point

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
printf "Fee Tracker\n\nTracks school fees.\n" > README.md
git add .
git commit --quiet -m "Add the term fee and README"
git switch --quiet -c feature/receipts
echo "Receipts: coming soon." >> README.md
git commit --quiet -am "Plan receipts in the README"
git switch --quiet main
git log --oneline --all
Output
3377d7b (feature/receipts) Plan receipts in the README
c1f9704 (HEAD -> main) Add the term fee and README

Recipe 1: Commits on main That Belong on a New Branch

Navika starts the hostel fee feature, and makes two commits before noticing she is still on main:

Terminal
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
echo "Hostel fee is per term" >> fees.txt
git commit --quiet -am "Explain the hostel fee"
git log --oneline -3
Output
606cb9c (HEAD -> main) Explain the hostel fee
3fd4d0c Add the hostel fee
c1f9704 Add the term fee and README

Steps: create the new branch where you are (so it keeps the commits), then move main back, then switch:

Terminal
git branch feature/hostel
git reset --keep HEAD~2
git switch --quiet feature/hostel
git log --oneline --graph --all
Output
* 606cb9c (HEAD -> feature/hostel) Explain the hostel fee
* 3fd4d0c Add the hostel fee
| * 3377d7b (feature/receipts) Plan receipts in the README
|/
* c1f9704 (main) Add the term fee and README

Explanation: git branch feature/hostel put a new name on the two commits. git reset --keep HEAD~2 moved main back two commits (--keep is a safe --hard, see "Git Reset in Depth"). The commits now belong only to feature/hostel. Nothing was copied, and the commit IDs didn't change.

Recipe 2: Commits on the Wrong Existing Branch

Now on feature/hostel, Navika adds a receipt feature by mistake: it belongs on feature/receipts, which already exists:

Terminal
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
git log --oneline -1
Output
6075bb5 (HEAD -> feature/hostel) Add receipt numbers

Steps: copy the commit to the right branch with cherry-pick, then remove it from the wrong one:

Terminal
git switch --quiet feature/receipts
git cherry-pick ":/Add receipt numbers"
Output
[feature/receipts b407109] Add receipt numbers
 Date: Mon Jan 5 10:27:00 2026 -0600
 1 file changed, 1 insertion(+)
 create mode 100644 receipts.txt
Terminal
git switch --quiet feature/hostel
git reset --keep HEAD~1
git log --oneline --graph --all
Output
* b407109 (feature/receipts) Add receipt numbers
* 3377d7b Plan receipts in the README
| * 606cb9c (HEAD -> feature/hostel) Explain the hostel fee
| * 3fd4d0c Add the hostel fee
|/
* c1f9704 (main) Add the term fee and README

Explanation: the receipt commit was copied to feature/receipts (with a new ID, as every cherry-pick), and feature/hostel was moved back one commit, so it no longer contains it. For several commits, cherry-pick a range (A^..B) and reset back by that many.

Recipe 3: Undo the Last Few Commits, Keep the Changes

Navika made two small commits on feature/hostel that should have been one, with a better message:

Terminal
echo 'Hostel fee for term 2: $9,500' >> fees.txt
git commit --quiet -am "term 2"
echo 'Hostel fee for term 3: $10,000' >> fees.txt
git commit --quiet -am "term 3"
git reset --soft HEAD~2
git status -s
git commit --quiet -m "Add the hostel fees for terms 2 and 3"
git log --oneline -3
Output
M  fees.txt
ba6878e (HEAD -> feature/hostel) Add the hostel fees for terms 2 and 3
606cb9c Explain the hostel fee
3fd4d0c Add the hostel fee

Explanation: git reset --soft HEAD~2 took back both commits and left all their changes staged. One new commit then replaced them. Use --mixed (the default) instead to keep the changes unstaged, for example to split them differently. Interactive rebase can do the same, as in "Interactive Rebase".

Recipe 4: Uncommitted Work That Belongs on a New Branch

Navika edited files on main without committing, and realizes the work should be a new feature branch. Uncommitted changes travel with you when you switch (see "Git Switch and Checkout"), so creating the branch is all it takes:

Terminal
git switch --quiet main
echo 'Exam fee: $800' >> fees.txt
git switch -c feature/exam-fee
Output
Switched to a new branch 'feature/exam-fee'
Terminal
git status -s
Output
 M fees.txt

Explanation: git switch -c created the branch at the current commit and switched to it, and the uncommitted change came along. main has no new commit, because nothing was committed on it. Commit here when ready.

Recipe 5: Uncommitted Work That Belongs on an Existing Branch

Navika commits the exam fee, then starts renaming the project in the README. That change belongs on feature/receipts, which already exists. But the README is different on that branch, so Git refuses the switch, to protect the edit:

Terminal
git commit --quiet -am "Add the exam fee"
sed -i.bak "s/^Fee Tracker$/Fee Tracker for Anamaneni Public School/" README.md
rm README.md.bak
git switch feature/receipts
Output
error: Your local changes to the following files would be overwritten by checkout:
	README.md
Please commit your changes or stash them before you switch branches.
Aborting

Steps: stash the change, switch, and pop it on the right branch:

Terminal
git stash push --quiet -m "Rename the project"
git switch --quiet feature/receipts
git stash pop --quiet
git status -s
cat README.md
Output
 M README.md
Fee Tracker for Anamaneni Public School
 
Tracks school fees.
Receipts: coming soon.

Explanation: the stash carried the edit across. On feature/receipts, it applied cleanly next to that branch's own README line, and feature/exam-fee is clean again. git switch -m feature/receipts is a shortcut that tries the same merge of your uncommitted changes while switching.

Recipe 6: The Wrong Commit Was Already Pushed

First, Navika commits the README change on feature/receipts, where it now belongs. Then she makes a new mistake: the sports fee is committed on main, and pushed, though it belongs on feature/exam-fee. If the commit is already on the server and others may have it, don't reset. Undo it on the wrong branch with git revert, and copy it to the right branch with git cherry-pick:

Terminal
git commit --quiet -am "Name the school in the README"
git switch --quiet main
echo 'Sports fee: $700' > sports.txt
git add sports.txt
git commit --quiet -m "Add the sports fee"
git switch --quiet feature/exam-fee
git cherry-pick ":/Add the sports fee"
git switch --quiet main
git revert --no-edit HEAD
git log --oneline -3
Output
[feature/exam-fee d0292ca] Add the sports fee
 Date: Mon Jan 5 11:07:00 2026 -0600
 1 file changed, 1 insertion(+)
 create mode 100644 sports.txt
[main cc63137] Revert "Add the sports fee"
 Date: Mon Jan 5 11:11:00 2026 -0600
 1 file changed, 1 deletion(-)
 delete mode 100644 sports.txt
cc63137 (HEAD -> main) Revert "Add the sports fee"
60b8024 Add the sports fee
c1f9704 Add the term fee and README

Explanation: main keeps its history and gains a revert commit, which is safe for everyone who already pulled it. The change now lives on feature/exam-fee. Push both branches as usual, with no force push.

Common Mistakes

Resetting before creating the branch in recipe 1. The commits are then on no branch. Recover them with git reflog and git branch <name> <commit>.

Using --hard with uncommitted changes present. They are destroyed. Use --keep, which refuses in that case.

Cherry-picking without removing the commit from the wrong branch. The change ends up in both places.

Resetting a pushed branch. Teammates' history no longer matches. Use recipe 6.

Forgetting that untracked files aren't stashed. Plain git stash leaves new files in the folder. They stay there when you switch, so they arrive on the other branch anyway, but git stash -u is clearer.

Under the Hood

None of these recipes moves a commit. A commit's parent is fixed forever, so "moving" a commit means either pointing a different branch at it (git branch, git reset) or creating a copy with a new parent (git cherry-pick, git rebase). Uncommitted changes, on the other hand, belong to no branch at all: they live in the index and working tree, which is why git switch -c carries them to a new branch for free, and why git stash is needed only when the switch would overwrite them.

Interview Corner

You committed to main but meant to create a feature branch, and haven't pushed. What do you do?

git branch feature/x to keep the commits on a new branch, git reset --keep HEAD~<n> (or --hard) to move main back, then git switch feature/x.

You committed to the wrong existing branch. How do you move the commit?

Switch to the right branch and git cherry-pick <commit>, then switch back to the wrong branch and git reset --keep HEAD~1 to remove it (if it isn't pushed).

How do you undo the last three commits but keep their changes?

git reset --soft HEAD~3 keeps the changes staged, and git reset HEAD~3 keeps them unstaged.

You started editing on the wrong branch, without committing. How do you move the work?

To a new branch: git switch -c <new>, and the changes come along. To an existing branch: git stash, git switch <branch>, git stash pop, or git switch -m <branch>.

The wrong commit was already pushed. How do you fix it?

Don't reset. git revert it on the wrong branch, and git cherry-pick it onto the right branch, then push both normally.

Practice

Challenge: three commits, wrong place. On main, make three commits for a discount feature (create discount.txt with Sibling discount: 10%, then add Staff discount: 20%, then add Discounts apply per term). Then move all three to a new branch feature/discount, leaving main as it was.

What does Git print? Before you open the solution, decide which recipe this is, and how far back main has to move.

Show solution
Terminal
git switch --quiet main
echo "Sibling discount: 10%" > discount.txt
git add discount.txt
git commit --quiet -m "Add the sibling discount"
echo "Staff discount: 20%" >> discount.txt
git commit --quiet -am "Add the staff discount"
echo "Discounts apply per term" >> discount.txt
git commit --quiet -am "Explain discounts"
git branch feature/discount
git reset --keep HEAD~3
git log --oneline -1 main
git log --oneline -3 feature/discount
Output
cc63137 (HEAD -> main) Revert "Add the sports fee"
33a56fe (feature/discount) Explain discounts
27c5a67 Add the staff discount
375720e Add the sibling discount

Recipe 1, with three commits: main moved back three commits, to the revert from recipe 6, and the three discount commits are on feature/discount with their original IDs.

Checkpoint. Do Lab 7 from the course labs: in a practice repository, create each of the five "wrong place" situations from recipes 1 to 5, and fix each one. Then push a commit to the wrong branch of a remote copy and fix it with recipe 6.

Quick check: Wrong-place recipes

4 questions
1You made two commits on main that belong on a new branch. What is the first command?
2A commit is on the wrong existing branch and not pushed. Which commands move it?
3You have uncommitted changes that belong on a new branch. What do you run?
4The wrong commit was already pushed to main. How do you remove it there?

Key Takeaways

Quick reference

SituationCommands
Commits on main, want a new branchgit branch <new>, git reset --keep HEAD~<n>, git switch <new>
Commit on the wrong branchgit switch <right>, git cherry-pick <c>, git switch <wrong>, git reset --keep HEAD~1
Undo commits, keep changesgit reset --soft HEAD~<n>
Uncommitted work to a new branchgit switch -c <new>
Uncommitted work to an existing branchgit stash, git switch <branch>, git stash pop
Pushed to the wrong branchgit revert <c> there, git cherry-pick <c> on the right branch

Was this tutorial helpful?

Lesson 63 of 106