Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Make one branch exactly like another

Tags:

git

merge

I am relatively new to Git, and I'm still not very comfortable with it. Right now, I'm looking for the command/options/magic that can make the current branch look like another branch; that is, to merge them, but when a conflict arises, to always choose the difference in the branch that is being merged into the current one.

My situation is thus; I have an stable(ish) application on the "master" branch. I also have another branch, called "feature". I basically want to make changes/additions/deletions to feature until I like the new feature I'm working on. Once I feel it is ready, I want to make the master branch look identical to the feature branch.

I know this probably isn't a best practice, but as I said, I'm new to Git. I plan on learning how to do more complicated things in the future, but for now, this is all I need.

Thanks, SO!

like image 457
G. Martin Avatar asked Apr 22 '10 22:04

G. Martin


2 Answers

The accepted answer ("Branches are just pointers ...") is no good for me, because not only do I need my branch to look like another branch - I need to do so by only adding commits (not losing any of the commits in the current history of my branch).

I liked this approach for making branch A look like branch B:

git checkout B
git diff A > patch_to_make_A_like_B
git checkout A
git apply patch_to_make_A_like_B

(And rm patch_to_make_A_like_B at the end.)

like image 65
DavidC Avatar answered Oct 05 '22 23:10

DavidC


Sorry! Didn't read all the way through before answering...

git checkout master
git merge feature

This will work effortlessly if you have not made any changes to master since you branched feature off it.

And what you are trying to do is exactly the way that branching and merging are supposed to work. Develop your features on a branch, when you have it stable and working like you want it to work, merge it back into the master branch.

like image 33
kubi Avatar answered Oct 06 '22 01:10

kubi