Home > front end >  How to stage only single line of changes in Git CLI?
How to stage only single line of changes in Git CLI?

Time:01-02

I added three lines of code in a row:

local function delete()
    return nil -- TODO: Stub
end

And I want to stage and commit only the first and the last ones. Adding it with -p or -i option does not help - the whole hunk will be added. I know that I can do this with GUIs like Git Extensions. But is it possible to stage single lines instead of hunks using command line?

CodePudding user response:

While interactively staging using the git add -p command, you have the option s to split a hunk further into smaller hunks. But, in this case, this hunk cannot be split further. So, you can use the manual edit option e

  1. Select the e option during the interactive staging
 local function delete()
    return nil -- TODO: Stub
 end
Stage this hunk [y,n,q,a,d,e,?]? e
  1. You will now be in the "manual hunk edit mode", which shows the hunk being staged.
 local function delete()
    return nil -- TODO: Stub
 end
  1. To not stage the second line, remove the second line and exit the "manual hunk edit mode"
 local function delete()
 end
  1. You have now staged only the first and the third line
  •  Tags:  
  • git
  • Related