5

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!

G. Martin
  • 163
  • 1
  • 7
  • 1
    Possible duplicate of [git command for making one branch like another](http://stackoverflow.com/questions/4911794/git-command-for-making-one-branch-like-another) – Brad Werth Oct 26 '15 at 21:26

4 Answers4

7

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

DavidC
  • 1,409
  • 10
  • 25
4

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.

kubi
  • 48,104
  • 19
  • 94
  • 118
  • what if I made a change to master that might cause a conflict? I guess I should have mentioned that possiblity, because it is possible I might need to make a change to something small. – G. Martin Apr 22 '10 at 22:56
  • @G. Martin AFAIK There should not be a problem. Perform `git checkout master` on the feature branch will bring up any merging problems. Once you have resolved those `git merge feature` on the master branch should not have any problems. – Carlos Rendon Apr 22 '10 at 23:56
4

I know this was asked awhile ago, and there are a few answers already, but I thought I would offer my 2 cents:

If you're looking to do a git merge and you don't want to worry about merge conflicts (always prefer the changes in one branch over another), then you can use git merge branch -X ours and git merge branch -X theirs. There's more explanation on this in https://stackoverflow.com/questions/13594344.

I would argue, however, that a git rebase would make more sense here. Some pros/cons are on the Rebase vs. Merge page. The merge is intended to retain a history of events that transpired including the merge itself, but from the sound of your question it seems like you're only looking to keep track of the events in the application development, hiding the fact that you ever had a separate branch in the first place.

Community
  • 1
  • 1
AndyJW
  • 191
  • 8
1

Branches are just pointers within the graph of commits. You can git-reset a branch to point to anywhere you like, just make sure you checkout the intended branch before making any further commits.

crazyscot
  • 11,819
  • 2
  • 39
  • 40
  • So in other words, I find the commit id of the last commit I made on the feature branch, switch to the master branch, and do a git reset --hard to that commit id, making the HEAD the same as the feature branch contents? Is that what happens? – G. Martin Apr 23 '10 at 14:11
  • Yes. I should stress that this is only one way to do it; in many cases you're better off doing it with the "traditional" workflow of merging from a feature branch back to your master. I would add that merges in git are quite intelligent; they're not like how things were under CVS and SVN. – crazyscot Apr 23 '10 at 22:26