开发者

Merge a topic branch on git, removing all the topic branch's commit history?

开发者 https://www.devze.com 2023-03-24 04:43 出处:网络
I would like to know if the following is possible in git: Suppose I have the following history: A---B---C---D---Emaster

I would like to know if the following is possible in git:

Suppose I have the following history:

A---B---C---D---E  master
         \
          W---X---Y  topic

I would like to merge all the changes made in the topic branch back to master, but without retaining the commit history. That is, instead of:

A---B---C---D---E---F  master
         \         /
          W---X---Y  topic

I would like to have the following history:

A---B---C---D---E---F  master

Where F is a new commit in the master branch that is equivalent to the changes made in commits W, X, Y, applied all at once.

I would also like to be able to keep the topic branch intact:

A---B---C---D---E---F  master
         \         
          W---X---Y  topic

Is this possible? I've been making some tests with git rebase, but so far it hasn't behaved on the way I nee开发者_StackOverflowd.

Thanks in advance.


It should be simpler than what bad zeppelin suggested, but I haven't tried this:

$ git checkout master
$ git merge --squash topic
$ git commit *what has changed*

However, when I understand git help mergeright, it should do the trick.


Create a copy of the topic branch, interactively rebase it to the master branch(git rebase -i E) squashing the commits in topic branch into one and finally delete the copy of the topic branch you created. After these you must be able to get what you wanted.


Create a new branch off topic (this is a throwaway)

git rebase master --interactive

(squash the commits into one)

Then on master, cherrypick the one commit on the throwaway branch.

You'll end up with exactly the history you want on master, and the topic branch will be intact.

There may be simpler ways to do the same thing...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号