-1

I have a file in my branch and when i merge it in master and push master, it works fine, then i edit the file , delete some portion of text in it, and adding some portion, i'm doing this in branch, so when i merge it again i just want the text in file that is in master to be added and the removed part should not be merged. How do i do that? This is my repository https://github.com/intern-it123/myrepo.git

CodeWizard
  • 128,036
  • 21
  • 144
  • 167

2 Answers2

0

When you are adding file use the git add -p and select only the required changes.

git add -p

Its called patch mode or interactive mode. Once you execute git add -p you will get list of options to choose from:

 Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?

If i understood you correctly you have a big file which you want to use (add) only parts of the file. In this case you will need to split it with the s and then choose y/n if to use this hunk or not.


And here the option you will have to choose from:

  • y stage this hunk for the next commit
  • n do not stage this hunk for the next commit
  • q quit; do not stage this hunk or any of the remaining ones
  • a stage this hunk and all later hunks in the file
  • d do not stage this hunk or any of the later hunks in the file
  • g select a hunk to go to
  • / search for a hunk matching the given regex
  • j leave this hunk undecided, see next undecided hunk
  • J leave this hunk undecided, see next hunk
  • k leave this hunk undecided, see previous undecided hunk
  • K leave this hunk undecided, see previous hunk
  • s split the current hunk into smaller hunks
  • e manually edit the current hunk
  • ? print help

enter image description here

In your case, you have to split the changes s and then choose only the required ones.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

There's not really a straightforward way to do that. The merge mechanism is set up for combining all changes from another branch into the current branch[1].

You can tweak how files are combined in the event both "sides" of the merge changed the file using a merge driver. This is relatively easy and well documented. But unless the file is modified on master, the changes from the branch will be applied as-is without invoking a custom merge driver.

To change how the commits as a whole are treated, you would switch the merge strategy. For example, the ours strategy keeps things as they are in the current branch, even if there is a file unchanged in the current branch but changed in the "other" branch.

None of the provided merge strategies do what you want, so you'd have to write your own. This would still have to do most of the things that the default strategy does (no small task), and would have to modify "their' patch to boot (identifying and removing unwanted changes).

And I haven't been able to find good docs for doing this. Which makes some sense, as I don't believe this is a way an end-user is intended to interface with git. A little info about custom merge strategies can be found here: git: How do I add a custom merge strategy?


[1] Well.... it's a bit more general than that; you could say something like "lines of commits' instead of branches, and with octopus merges you can combine more than two at once (if certain conditions are met).

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52