2

I made two sets of changes in a single file. They are conceptually different so I would like to keep them in seperate commits.

Can I stage the changes in a certain range or everything above or a below a line from the command line?

I.e. something like the following:

git add file -lines 124:421
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
465b
  • 1,047
  • 1
  • 13
  • 25

1 Answers1

2

You can use git add -p and then select the hunks ou wish to add.

These are the options you can do within add -p:

y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining 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

Once you use the s it will pick the chunk of code which can be considered as a standalone change. If you want to split it even more you will have to use the e to edit the hunk and then add it back to the stage area.

Summary:

To split hunks you use the s flag.
If you need to split it into even smaller chunks you will need to manually edit it using the e option.

enter image description here

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