Jonas Möller Jonas Möller - 2 months ago 7
Git Question

Staging a renamed attribute using hunk edit mode

I'm trying to use 'git add -p' to commit only a part of my code. In the diff below I want to rename 'model' to 'sharedData' (thus, removing the model line and adding the sharedData-line).

@@ -58,9 +60,11 @@
</div>
`
})

export class PanelComponent implements OnInit, OnPanelAction, OnDestroy {
- @Input() data: any;
- @Input() model: any;
+ @Input() sharedData: any;
+ @Input() model: Wrapper<any>;
+ @Input() window: string;
+
@Input() map: Map.WindowMapper;
@Input() modules: any[];


I've tried adding unnecessary lines to context (adding a space) with multiple setting, but I get 'Your edited hunk does not apply.':

@@ -58,9 +60,12 @@
@@ -58,12 +60,12 @@
</div>
`
})

export class PanelComponent implements OnInit, OnPanelAction, OnDestroy {
@Input() data: any;
- @Input() model: any;
+ @Input() sharedData: any;
@Input() model: Wrapper<any>;
@Input() window: string;

@Input() map: Map.WindowMapper;
@Input() modules: any[];


Removing context lines did also not work:

@@ -58,9 +60,8 @@
</div>
`
})

export class PanelComponent implements OnInit, OnPanelAction, OnDestroy {
- @Input() model: any;
+ @Input() sharedData: any;
@Input() map: Map.WindowMapper;
@Input() modules: any[];

Answer

The "Edit hunk" view says the following:

 # To remove '-' lines, make them ' ' lines (context).
 # To remove '+' lines, delete them.
 # Lines starting with # will be removed.

You want to keep the data attribute, so you need to change the leading "-" of that line to a space - make sure not just to remove the "-", even if that might look the same if you are using tabs.

Additional you do not want to add the attributes model and window, so just delete that lines. Do the same for the empty line if you don't want to add it.

The result should look like this:

  export class PanelComponent implements OnInit, OnPanelAction, OnDestroy {
     @Input() data: any;
-    @Input() model: any;
+    @Input() sharedData: any;
     @Input() map: Map.WindowMapper;
     @Input() modules: any[];

In both of your examples you are changing the context so git cannot recognize the correct location:

The first example contains the model and the window attribute without the plus sign at the beginning, which means to git that they should exist in the previous code. In the second example the previously existing attribute data is missing, which should be there for the correct context.