Git: Make One Branch Just Like Another

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

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

I know this is probably not the best practice, but as I said, I am new to Git. I plan on learning how to do more complex things in the future, but for now this is all I need.

Thank you SO!

+2


a source to share


4 answers


Branches are just pointers in the commit graph. You can git-reset

point the branch wherever you want, just make sure you checkout the intended branch before making any further commits.



+1


a source


Sorry! Didn't read all the way to answer ...

git checkout master
git merge feature

      



This will work effortlessly if you haven't made any changes to master

since you separated it from feature

.

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

+4


a source


I know this was asked a while ago and there are already several answers, but I thought I would suggest my 2 cents:

If you want to do git merge

, and don't want to worry about merge conflicts (always prefer changes in one branch over another), you can use git merge branch -X ours

and git merge branch -X theirs

. There's more this is due to fooobar.com/questions/67675 / ... .

I would say, however, that git rebase

would make more sense here. Some pros / cons are on the Rebase vs. Merge . A merge is meant to keep a history of events that happened, including the merge itself, but the sound of your question makes it seem like you only want to track events in app development, hiding the fact that you've ever had a separate branch in the first place ...

+3


a source


The accepted answer ("Branches are only pointers ...") doesn't work for me because not only do I need my branch to look like another branch - I need to do this by applying only other commits (without losing any of the commits in the current history of my branch).

I liked this approach for creating a branch A

like a 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.)

+1


a source







All Articles