Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge testing branch (final commit) to master branch

I have created a testing branch. It has a lot of tiny commits to build one feature. At the end of it, I want to take the final completed changes, and put them into the master branch.
The master branch, shouldn't contain the history of the testing branch.
Testing branch will be removed eventually.

What is the best way to achieve this?
Would generating a patch and applying it on master be the best way?
If so, how do I generate/apply the patch?

like image 669
resting Avatar asked Feb 02 '12 06:02

resting


People also ask

How do I merge a test branch into master?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.

Can we commit on branch that's already been merged with Master?

You can continue working on your branch and then when you merge with master again, it will bring the commits that are missing on master.

Can I commit directly to master branch?

Master branch often reflects the code used in production. Hence, it is not a good practice to commit your work to the master branch. It is recommended to branch out or create a copy of the master branch in a different branch where you will commit your work.


2 Answers

Various approaches are described in "Understanding the Git Workflow":

Short lived work

The vast majority of the time, my cleanup is just a squash merge.

git checkout master
git merge --squash private_feature_branch
git commit -v

Larger work

I decide my change should be broken into smaller changes, so squash is too blunt an instrument. (As a rule of thumb I ask, “Would this be easy to code review?”)

git rebase --interactive master

(not an option in your case, as you mention in your question)

Declaring Branch Bankruptcy

Maybe my feature branch existed for a very long time, and I had to merge several branches into my feature branch to keep it up to date while I work. History is convoluted.
It’s easiest to grab the raw diff create a clean branch.

git checkout master
git checkout -b cleaned_up_branch
git merge --squash private_feature_branch
git reset

I now have a working directory full of my changes and none of the baggage from the previous branch. Now I manually add and commit my changes.

like image 52
VonC Avatar answered Oct 08 '22 00:10

VonC


You are looking for --squash option of git merge:

--squash --no-squash

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

With --no-squash perform the merge and commit the result. This option can be used to override --squash.

So git merge --squash test-branch and commit away.

You may also try git merge --no-commit --no-ff test-branch

like image 1
manojlds Avatar answered Oct 08 '22 00:10

manojlds