Committed to the wrong branch? Move the commit
These blocks describe different cases, not one script. Choose the case that matches your repository. Replace example branch names, file paths and VERIFIED_COMMIT before use. Abort commands apply only to their active operation; do not run both alternatives.
Try it in your browser
You made a commit on main that belongs on feature/discounts, and you have not pushed it yet.
Goal: Move "Apply discounts" to feature/discounts and take it off main.
Practice uses a simulated repository in your browser memory. It does not access your files or connect to a real remote repository.
Commands to practise
This simulator supports a subset of Git commands. Unsupported commands explain their limits in the terminal.
git log --oneline --all
git switch feature/discounts
git cherry-pick main
git switch main
git reset --hard HEAD~1With FluxGit
- Right-click the commit and cherry-pick it onto the right branch.
- Then reset main back one commit: FluxGit makes a restore point first, so even the reset can be undone.
Short answer
If you committed to the wrong branch, first give the existing commit a rescue branch. Apply its change to the intended branch with cherry-pick and inspect the result. Only then remove it from the original branch: reset can suit a private tip; a shared branch normally needs a revert. Move the change first, then choose how to correct the original branch.
The example and its prerequisites
This example starts with one case: one ordinary, non-merge commit at the tip of main, which belongs on an existing feature/fix branch. The working tree is clean. Replace these example branch names with your own. If several commits, a merge, or unrelated work followed the mistake, stop and identify the precise range rather than copying the final reset below.
Inspect before changing anything
Read status, the current branch and the recent graph before changing anything. Check the commit's files and patch, not just its subject. A commit can contain work from several people or tasks. Record its full object ID as SCHOOL_MISTAKE; this names the original commit even after switching branches. Check whether colleagues already have it before choosing how to repair main.
git status --short
git branch --show-current
git log -5 --oneline --decorate
git show --stat HEAD
SCHOOL_MISTAKE=$(git rev-parse HEAD)Keep a reference or a separate checkout
A rescue branch keeps the committed object reachable while you work. It does not copy uncommitted edits. If status shows local changes, save and verify those separately before the example: a deliberate commit, a reviewed stash, or an external copy can be appropriate. A stash with -u includes untracked files, but not ignored files. Do not assume reflog contains any of those unsaved file contents.
git branch rescue/wrong-branch "$SCHOOL_MISTAKE"
git show --stat rescue/wrong-branchThe first workflow
Switch to the intended branch and cherry-pick the saved object ID. Git applies that commit's change and usually creates a new commit with a different ID. That is expected: its parent has changed. Inspect the new patch and run the project's relevant checks. Do not reset the original branch until the destination contains the change you actually wanted and you have a recoverable reference to the original.
git switch feature/fix
git cherry-pick "$SCHOOL_MISTAKE"
git show --stat HEADChoose the other outcome deliberately
For the private-tip case only, return to main and confirm its HEAD still equals SCHOOL_MISTAKE. With a clean working tree and the rescue branch preserved, reset to the mistake's parent removes that commit from main. The hard reset also replaces tracked working-tree content, so the clean-tree condition matters. If main moved, or the commit was shared, this exact command sequence no longer fits.
git switch main
git status --short
git rev-parse HEAD
git reset --hard "${SCHOOL_MISTAKE}^"Limits and exceptions
When the mistake reached a shared branch, prefer an ordinary revert on that branch. It adds a new commit reversing the selected change without removing other people's history. The destination cherry-pick and the original revert have separate outcomes; review both. Coordinate with collaborators when the wrong commit has already been merged elsewhere. Do not turn this guide into an automatic force-push recipe.
git switch main
git revert "$SCHOOL_MISTAKE"Conflicts and cleanup
Cherry-pick can stop with conflicts. Read git status, resolve only the intended files, stage them and continue; use cherry-pick --abort if the chosen change or destination was wrong. Revert has its own continue and abort commands. An abort exits the active operation; it is not a general backup for edits made outside it. Avoid starting a second repair while Git reports an unfinished operation.
git status
git cherry-pick --abort
git revert --abortVerify the result
The final graph should show the intended change on feature/fix and the chosen correction on main. Compare both patches and inspect status in each checkout. Keep rescue/wrong-branch until the repair is reviewed; delete it later only when you no longer need that reference. A clean status alone does not prove the application still behaves correctly.
git log -6 --oneline --decorate --all
git show --stat feature/fix
git status --shortCommon questions
Can I move several commits? Yes, but first identify their order, dependencies and exact range. Can I use reset --soft instead? It moves the branch while keeping the index and worktree; it does not transfer a commit to another branch by itself. What if I already pushed? Start with the shared-history revert case and coordinate any exceptional rewrite explicitly.
Review the operation in FluxGit
Use FluxGit’s visual history and patch view to review the operation and the relevant commits. Keep the branch, sharing and local-file checks in this example. The download page lists the currently available builds and platforms.
Official Git references
Committed to the wrong branch in Git? Move the commit
You made a commit on main, but it belonged on the feature discounts branch. It is not pushed yet, so it can be moved.
Watch on YouTube — opens a new tab
Recorded in a test repository.
Read the narration transcript
- You made a commit on main, but it belonged on the feature discounts branch. It is not pushed yet, so it can be moved.
- Here it is: the discount commit, on main.
- The idea in two moves. The discount commit is on main, but it belongs on feature discounts. First, cherry-pick copies it there: a new commit with the same change. Then a hard reset moves main back to where it was. The original commit stays reachable from the restore point.
- Switch to feature discounts.
- Right-click the commit, cherry-pick. The dialog names the risk and the restore point. Confirm.
- Back on main.
- Reset main to the commit before. See the plan shows the three resets side by side: what moves, and how each is undone.
- Hard is the one that removes the change from main too. Use this path.
- The hard reset runs with a safety stash and a restore point first.
- Check: main matches origin main again, and feature discounts has the commit.
- In the terminal: switch to the branch, cherry-pick the commit, switch back, and reset main hard to origin main. Careful: in a terminal there is no restore point.
- Recap. Copy the commit where it belongs, then move the label back. Next lesson: detached HEAD.