简体   繁体   中英

Merge git branch without moving branch pointer

I'm looking for a solution to apply a feature branch to the development branch in git without actually moving the branch pointer. Essentially, I'd like to create a single commit off of the feature branch and make sure it's pointing at the originating branch somehow (mention in the log might be enough).

I know that stacked git should offer something like that, although that would introduce new commands to the standard workflow which I'd rather avoid.

What I'm trying to achieve is some sort of feature management which allows us to apply / remove whole features, while keeping the visibility of the patch branch itself. If I do merge the branch, most commands trying to diff the change will not do what I want because the branch is already merged into develop.

I cannot really follow the upstream - sometimes there are concurrent changes which are easier to correct by getting new tag and reapplying only needed features, rather than resolving conflicts and reverting redundant features - so this way of work is not possible. On the other hand I do want to see the change, so that I can either rework it to match new version, or submit upstream at a later time.

Actually, there is an easier method. The git merge command has an option --squash , which just accumulates all the changes from the branch to be merged, and adds them to the staging area.

So, you can do what you want with the following commands (when you are on master, and want to merge feature branch feature-foobar):

$ git merge --squash feature-foobar
$ git commit -m "Implemented feature: foobar"

To what I understand, you may use git rebase -i to achieve your goal. Below is the scenario you want to achieve...

A--B--C(develop)
\
 \D--E--F(feature)

you can combine DEF as a single commit named as G the final outcome will be

   A--B--C--G(develop)
    \
     \D--E--F(feature)

G is a single commit that have all the things in DEF, and you can easily take it off from the develop branch.


So in order to process this surgery, you can do....

git checkout feature_branch

make a temp branch...

git checkout -b temp

git rebase -i develop_branch

and interactive editor will be prompted out... mark commit D and E as squash, retain the last line(commit) as your last single commit save and quit

then a commit G will rebase on to your developement branch.

   A--B--C(develop)--G(temp)
    \
     \D--E--F(feature)

Reset your development onto commit G and delete temp branch if you want.

git checkout develop_branch
git reset --hard <shaSum of commit G>
git -D temp

That's it!

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