如何处理多分支脏提交?Git指定提交压缩至HEAD求助
Hey there, let's get this sorted out without more messy commits or branches. Here's a step-by-step approach to squash commit 9fd5653f231 into your current HEAD:
Step 1: Ensure a Clean Workspace
First, make sure you have no uncommitted changes hanging around—stash them if needed:
git stash
You can retrieve them later with git stash pop if you need those changes.
Step 2: Start an Interactive Rebase
We'll target the parent commit of 9fd5653f231 to include it and all commits up to HEAD in the rebase process:
git rebase -i 9fd5653f231^
The ^ here tells Git to select the commit immediately before 9fd5653f231, so the rebase will cover 9fd5653f231 and every subsequent commit up to your current HEAD.
Step 3: Mark Commits for Squashing
A text editor will pop up listing commits from oldest to newest. You'll see 9fd5653f231 near the top, and your current HEAD commit at the bottom.
- Keep the first line (for
9fd5653f231) aspick—this will be the base commit we squash other commits into. - For every commit below it (including your HEAD commit), change
pickto either:squash(orsfor short): This preserves the commit messages of those commits, letting you combine them into a single, polished message.fixup(orffor short): This discards the commit messages of those commits, keeping only the message from9fd5653f231.
Save and close the editor once you've made these changes.
Step 4: Resolve Conflicts (If They Arise)
If Git hits conflicts during the rebase, it'll pause and notify you which files have issues. Open those files, fix the conflicting sections, then stage the resolved changes:
git add <conflicted-file>
Then continue the rebase:
git rebase --continue
If things get too tangled and you need to start over, abort the rebase entirely with:
git rebase --abort
Step 5: Clean Up Leftover Branches
If you have stray branches from your earlier merge/rebase attempts, delete them once you confirm they're no longer needed:
# For fully merged branches (safe to delete) git branch -d <unwanted-branch-name> # For unmerged branches (use with caution—this deletes without checks) git branch -D <unwanted-branch-name>
Critical Note for Remote Branches
If this branch has already been pushed to a remote repository, squashing commits rewrites Git's history. You'll need to force push the updated branch:
git push -f
Make sure to alert your team members before doing this—they'll need to sync their local branches properly to avoid conflicts.
内容的提问来源于stack exchange,提问作者user86776

