Git sidebar to stage individual lines (or at least blocks)

The new Git sidebar and previews look super cool and are way more useful than before. What is stopping me from switching over to using the Git sidebar is the ability to stage individual lines.

For example, let’s say we have file with changes on lines 1-2 and 6-7. Then I would like to:

  • go to the file in the Git sidebar
  • select lines 5-9 (so, somewhat around what I want to stage)
  • right click → Select “Stage lines”
  • I see a partial stage of the file; lines 6-7 are staged, but lines 1-2 are not.

I currently use the Terminal with git add --patch to get the same functionality.

If individual lines is too much, allows to stage a block; the blue thingy that shows what was change, if I click on it in gutter, I only see “Discard changes”, that would be a perfect place for a “Stage changes”.

Thanks for all the great work so far :smiley:

1 Like

@eirvandelden I’ve been working on a git diff sidebar extension as an experiment. I’m trying to implement this feature but I have not been able to find a Git command that allows me to stage specific lines or hunks without going through interactive mode. If there was a command like git add --lines 6-7 then this would be easy to implement. Do you know if a command like this exists?

So I wondered how does Github Desktop have this feature? From what I can tell they actually swap out the file with a version with just your selected changes, stage and commit the entire file, and then swap back in a version of the file with all the previously saved changes. Kind of a complicated thing to implement.

Hi @eablokker!

You know, I never thought of how to implement such a thing myself. I did some research and committing exact lines apparently can’t be done. I think that is why Github Desktop uses a swap file.

The best that I could find is a git diff algorithm which could be set to minimal: Git - git-diff Documentation

This means you still can’t stage any lines, but you can stage very small blocks (i.e. patches). Maybe that would be a good first step?

Thanks for your comment, it became an enlightening search in how I could help make such a feature :slight_smile:

The idea of a commit of part of a file seems contrary to git entirely. But if you could express each of those changes as separate patches, then sure. I’ve never used a git client that me break a change up like this … would be kind of cool, I think, in that it would have saved me some effort to manually pick apart changes when I’m not disciplined enough to have structured them as separate commits in the first place.

I do this plenty inside tower, it’s really nice

1 Like