sinhayash
sinhayash

Reputation: 2803

Merge changes to specific file from one branch to other in git

I am a newbie in Git. I have the following setup.

SETUP:

master-branch: Main stable branch of my project. Actually, it contains the html code of my website.

When I uploaded the files on the web, I did some changes in index.html to adjust proper file locations. So I have two versions of the same file: masterbranch (mbVersion) and web (wVersion)

WHAT I HAVE DONE:

Now I have done some changes in the mbVersion. I want those changes in the wVersion. (But I don't want to change the links in wVersion to the old mbVersion)

WHAT I PLAN TO DO:

I'm thinking to create a new branch for web version(say Web-branch), then merge specific commits from master-branch to Web-branch.

QUESTION:

  1. Am I doing it right? Is there any other better way?

  2. If, this is the correct way, will merging that specific commit to the web-branch give me the desired changes and NOT change the links? So, how do I merge? I plan to use this: How can I merge a specific file from one branch into another branch in Git

  3. Shall I use cherry-pick?

WHAT I HAVE TRIED:

I have tried http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/ but it doesn't merge the contents of both files. It basically just write over the file instead of actually merging the contents of the file.

Actually, it is a big project and ~20 people are working on it. So, I don't want to mess up anything.

Upvotes: 0

Views: 143

Answers (3)

dyng
dyng

Reputation: 3054

  1. I think to create two individual branches for them is a good idea.

  2. make sure you are on wVersion and merge mbVersion, git will do the job as you expect.

  3. I don't recommend cherry-pick personally, because what cherry-pick actually do is make a patch based on diff of specific commit and its parent, then apply it to current branch. It may cause conflict in future merge, because cherry-pick only cares about the file not the history.

Upvotes: 1

Vasiliy
Vasiliy

Reputation: 16228

You want to pull latest changes from mbVersion, but you want to make sure that the changes you made in wVersion to some specific file won't be lost, right?

In this case you can just pull as usual. Git sees that you made changes in wVersion, and these changes won't be overwritten straight away. After the pull you'll get either of two:

  1. If no one worked (in mbVersion) on links you changed in wVersion - your pull will be applied cleanly (auto-merge) to these links.
  2. If some of these links changed in mbVersion after the last pull from mbVersion - you will get merge conflicts. In this case just tell git to keep local changes.

NOTE I assumed that your workflow includes pulls form mbVersion into wVersion, but not the other way. If you do pull from wVersion back to mbVersion - this approach may not work!

Upvotes: 0

Chronial
Chronial

Reputation: 70663

I would recommend having just one branch and branching in your config depending on where your code is deployed. Using git, you can support that with a smudge filter, as detailed here: https://stackoverflow.com/a/13616911/758345

Upvotes: 1

Related Questions