0

Consider the following situation:

$ printf $'line1-v1\nline2-v1\n' > test.txt
$ git init .
$ git add test.txt 
$ git commit -m 'Initial commit'
$ printf $'line1-v2\nline2-v2\n' > test.txt

Now, magit-status looks like this:

Head:     master Initial commit

Unstaged changes (1)
modified   test.txt
@@ -1,2 +1,2 @@
-line1-v1
-line2-v1
+line1-v2
+line2-v2

Recent commits
285d881 master Initial commit

The task is to stage just the changes in the first line. But if you try doing it the obvious way (stage the removal of line1-v1 and the addition of line1-v2), the lines end up in the wrong order in the index:

Head:     master Initial commit

Unstaged changes (1)
modified   test.txt
@@ -1,2 +1,2 @@
-line2-v1
 line1-v2
+line2-v2

Staged changes (1)
modified   test.txt
@@ -1,2 +1,2 @@
-line1-v1
 line2-v1
+line1-v2

Recent commits
285d881 master Initial commit

The obvious work-around is to edit the source file and temporarily undo the changes to line2. But, is there a simpler way, which doesn't involve editing the source file?

If Magit had a way of editing the file's contents in the index, then that would be an option - one could then simply reorder the lines after staging, or just apply the edit there directly. But there is no way to do this, as far as I can tel.

3

If Magit had a way of editing the file's contents in the index, then that would be an option

Magit's ediff commands allow this. Use E s (magit-ediff-stage) and choose test.txt as the file to stage (alternatively, with point on the unstaged file in the status buffer, e (magit-ediff-dwim) will do the same). This will pop up three buffers, with the middle ("B") text.txt.~index~ buffer representing the index. You can edit it as normal, or use ediff commands to apply whole hunks at a time. Then quit the ediff session with q, and answer y to the Update index with contents of text.txt.~index~ query.

2

That is not possible but I am considering a feature that would allow doing so: https://github.com/magit/magit/issues/2938.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.