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
2 Answers
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
In your case, you have to split the changes s and then choose only the required ones.

- 1
- 1

- 128,036
- 21
- 144
- 167
-
`$ git add -p myFirstBranch.txt diff --git a/myFirstBranch.txt b/myFirstBranch.txt index 6f3aab7..5be9c98 100644 --- a/myFirstBranch.txt +++ b/myFirstBranch.txt @@ -1 +1 @@ - in branch dvs, added \ No newline at end of file +sasd in branch dvs, \ No newline at end of file Stage this hunk [y,n,q,a,d,e,?]? ` – Shahrukh Tramboo Sep 17 '19 at 12:21
-
how do i not let ---a happen – Shahrukh Tramboo Sep 17 '19 at 12:21
-
i just want addition here, not deletion – Shahrukh Tramboo Sep 17 '19 at 12:22
-
Choose [y or n] (yes or no) for each change – CodeWizard Sep 17 '19 at 12:23
-
change is in same file, – Shahrukh Tramboo Sep 17 '19 at 12:28
-
Sorry, cannot split this hunk – Shahrukh Tramboo Sep 17 '19 at 12:30
-
how do i select the changes – Shahrukh Tramboo Sep 17 '19 at 12:30
-
This is much easier to do in `git gui`. You can highlight a region, right-click and select 'Stage Hunk' or 'Stage Lines' – EncryptedWatermelon Sep 17 '19 at 12:52
-
This whole answer is premised on the idea that you only want to commit the addition (not the deletion) in the first place; the question as written asks about the case where all changes are committed and you want to do a selective merge. – Mark Adelsberger Sep 17 '19 at 13:26
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).

- 42,148
- 4
- 35
- 52