You switched tasks, made two commits, and then noticed you were still on main. If both commits are still local, create a branch at the current tip to save them. Then reset main to its last published commit.
Check your branch and commits
In this example, main tracks origin/main and both commits belong on a new branch called fix/cart. Use your own branch and remote names. Run each step in the same terminal, in the repository.
root=$(git rev-parse --show-toplevel) &&
cd "$root" &&
git status --short --branch \
--ignored --untracked-files=all
The first line should name your original branch and its upstream. Set aside any staged or unstaged edits to tracked files before continuing. Note any untracked (??) or ignored (!!) files. You’ll check those before resetting.
Fetch the published branch and save the exact commit you will compare against.
git fetch --no-tags --no-prune \
--no-prune-tags origin \
refs/heads/main &&
base=$(git rev-parse --verify FETCH_HEAD)
Stop if the fetch fails or the branch has no upstream. A local tracking ref can be stale.
git log --left-right --oneline \
"$base"...HEAD
> f8a3864 Round cart subtotal
> 28a418f Add cart subtotal test
Each > marks a commit on your current branch that is absent from the fetched branch. Continue only if you see at least one commit and every line starts with > and belongs on the new branch. A < means the remote branch has commits you do not have.
If you might have pushed elsewhere
Fetch each branch you might have pushed to from the repository you pushed to. Local tracking refs can miss published commits. This example checks review/cart on origin.
git fetch --no-tags --no-prune \
--no-prune-tags origin \
refs/heads/review/cart &&
git log --oneline FETCH_HEAD..HEAD
Every commit you plan to move must appear in this log. If one is missing, that remote branch already contains it. Stop if a commit is missing, a fetch fails, or you’re unsure where you might have pushed.
Save the commits on a new branch
git branch fix/cart &&
git log --oneline "$base"..fix/cart
This creates fix/cart at your current commit and keeps you on main. Before going on, confirm the new branch was created and its log matches the commits you just reviewed. If the name is taken, pick a new one.
Reset the original branch
While still on main, run these checks from the repository root. All paths in the output are relative to that root.
root=$(git rev-parse --show-toplevel) &&
cd "$root" &&
git status --short --branch \
--ignored --untracked-files=all &&
git diff --no-relative --name-status \
--no-renames HEAD "$base" &&
git diff --no-relative --raw \
--ignore-submodules=none --no-renames \
HEAD "$base"
The raw diff starts each line with two file modes after the colon. If either mode is 160000, stop before resetting. That path involves a submodule, and the parent repository’s status can omit ignored files inside it that a reset could overwrite or delete.
Move any untracked or ignored files that overlap a listed path outside the worktree. Check for exact matches and directory conflicts. If you’re unsure, move all untracked and ignored files aside. Git can overwrite an ignored file when it restores a tracked path there, even with --keep.
Once those files are out of the way and your tracked files are clean, confirm main and fix/cart still point to the same commit. From main, reset to the commit you saved in $base.
git reset --keep "$base" &&
git status --short --branch &&
git log --oneline "$base"..fix/cart
## main...origin/main
f8a3864 Round cart subtotal
28a418f Add cart subtotal test
main is back at the fetched commit, and both commits are on fix/cart. Run git switch fix/cart to pick up where you left off.
If a server rule blocked your push, see how to push a feature branch for review. If one fix is ready to share and you want to keep working on this branch, see how to push one commit and keep your branch intact.
See what’s waiting before you push
With push& (Pushand) in your Mac menu bar, you can see which branch has commits waiting and read them before you push.