简体   繁体   中英

Create a clean branch from master with required commits from another branch

So, I'm new to git and its quite a learning curve - one that i'm navigating horribly as you will soon see.

I created a dev branch from master and then made a few commits.

dev -> A -> B -> C-> D

I then merged to master.

(on branch dev)$ git merge master
git checkout master
git merge dev

Then i squashed the 4 commits and pushed to remote

git rebase -i HEAD~4
git push

So now

master: ABCD

(I now realize this would've done what i wanted too.)

git reset --soft HEAD~3 &&
git commit

Here's where things go to hell.

I continued development on dev brach and made a commit.

dev -> A -> B -> C-> D -> E 

Then, having realized the spot i got myself into, i created a new branch from master, new_dev but - i rebased dev onto it and made a couple of commits (Again, evidently a stupid thing to do)

new_dev -> ABCD -> A -> B-> C-> D-> E -> F-> G

Now, having realized the error of my ways I'm considering doing the following but would like approval from more experienced people.

  1. Create new branch from master
  2. Cherry pick E from dev
  3. Cherry pick FG from new_dev

Let me know if I've got it right this time or what i should do instead.

That's the reason why you usually want to squash on the feature/dev branch, not on master.

But since you are already in this situation, you can create a new branch from master and use git cherry-pick to add the commits you want from dev (cherry-pick simply creates new commits on the current branch that do the same thing as the commit specified as argument). And you have to throw away the current dev branch.

Alternatively, you can merge dev into master, with a possible nasty merge conflict, and then either create a new dev branch or merge master into dev to sync them up.

I can't stress this enough. You don't want to do any changes on the master branch, including squashes, resets etc which might create inconsistencies with the dev branch. That's why you have a dev. In master, what's merged is merged for good.

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