Git操作疑问:误提交至master分支后的分支与冲突问题
Hey there! Let's work through your Git questions step by step to get you un-stuck.
1. Does creating a new branch from master include all committed changes on master?
Absolutely. When you create a new branch from master (using git checkout -b new-branch or git switch -c new-branch for newer Git versions), the new branch will be an exact copy of master's current state—this includes every local commit you've made to master, even if those commits haven't been pushed to the remote master branch yet. So your local changes will carry over to the new branch automatically.
2. Steps to resolve the conflict you encountered
Conflicts pop up when the remote version of your branch (or the branch you're pushing to) has overlapping changes with your local commits. Here's a straightforward workflow to fix this:
- Fetch the latest remote updates to ensure you have all the newest code:
git fetch origin - Switch to your new branch if you aren't already on it:
git checkout your-new-branch-name - Merge or rebase with the remote branch (merge is simpler for beginners; rebase gives a cleaner commit history):
# Option 1: Merge remote changes into your local branch git merge origin/your-new-branch-name # Option 2: Rebase your local commits on top of the remote branch git rebase origin/your-new-branch-name - Resolve conflicts in your code editor: Git will flag conflicting sections with markers like
<<<<<<<,=======, and>>>>>>>. Edit these parts to keep the correct code, then delete the conflict markers. - Stage the resolved files:
git add . - Complete the merge/rebase process:
# If you used merge git commit # If you used rebase git rebase --continue - Push your fixed changes to the remote:
git push origin your-new-branch-name
3. A common follow-up (since your question was cut off)
If you're looking to clean up your local master branch (to match the remote master again, now that your changes are in the new branch), here's how:
- Switch back to master:
git checkout master - Reset your local master to mirror the remote master (this will erase your local commits—double-check your new branch has those changes first!):
git reset --hard origin/master
内容的提问来源于stack exchange,提问作者hilda_sonica_vish

