DEV Community

Discussion on: A Thorough Introduction to Git's Interactive Patch Mode

Collapse
 
mak_arnautovic_69e661f698 profile image
Mak Arnautovic • Edited

Just want to make a minor correction. At the time of writing (git version 2.30.2) you can edit changes in a line and stage them separately via patch mode. Though at the end of the day the changes do have to be able to be represented in terms of an entire line. Maybe that's what you meant in the first place but in case anyone needs an example:

Say I add this function to a file:

function gonnaChangeThis(thisComesLater) {
  console.log("nothing here!");
  console.log(thisComesLater);
}
Enter fullscreen mode Exit fullscreen mode

I can do:
git add -p


+  function gonnaChangeThis(thisComesLater) {
+    console.log("nothing here!");
+    console.log(thisComesLater);
+  }
+
function someOtherThing() {
(1/1) Stage this hunk [y,n,q,a,d,e,?]? e
Enter fullscreen mode Exit fullscreen mode

and edit just by deleting what I don't want via the editor (in this case the func arg and corresponding line):

# Manual hunk edit mode -- see bottom for a quick guide.
@@ -37,6 +37,11 @@ 

+function gonnaChangeThis() {
+  console.log("nothing here!");
+}
+
Enter fullscreen mode Exit fullscreen mode

save, quit, and voila:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   exampleFile.js

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   exampleFile.js
Enter fullscreen mode Exit fullscreen mode

git commit
git add -p

-  function gonnaChangeThis() {
+ function gonnaChangeThis(thisComesLater) {
    console.log("nothing here!");
+  console.log(thisComesLater);
}
Enter fullscreen mode Exit fullscreen mode