简体   繁体   中英

git rebase pulls in changes from unrelated branch?

I'm something of a newbie so don't laugh too hard...

I did some work on branch A and made a few commits. Then, had to make a hotfix change so I created branch B and changed a few files. Then, because master didn't change I thought it would be simpler and clearer if I did:

git rebase master
git checkout master
git merge B

But, this applied changes from branch A. What am I not understanding? This can't be the way it's supposed to work, is it?

When you created branch B , you were on branch A , so the base of your branch B contains all the work you did in branch A . This is why those branch A changes are now in master . Diagramatically, you have the following situation:

branchA: A -- B -- C
branchB: A -- B -- C -- D

The correct strategy to making a hotfix in master would be to create a branch from master :

# assuming you start on branch B
git checkout master
git checkout -b master_hotfix
# make the fix
git commit -m 'made the hotfix'
git checkout master
git merge master_hotfix
# carry on as you were
git checkout branchB

To undo what you have done, you can try resetting your master branch to the commit before the erroneous merge took place. Type git log and count how many commits came in frok the merge with branchB . Then type:

git reset --hard HEAD~N

where N is the number of commits you wish to remove from master . Then apply the steps I gave above for correctly making the hotfix. Note that you should not use git reset if you have already pushsd your local master to the remote. In this case, you could try using git revert , but it might just make sense to leave master the way it is, and just merge branchB again later once you have completex your work there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM