4 ms·
Is there a way to add only certain lines of a file to a commit? Sometimes it'd be nice to say, "only these changes" make it to this commit, while "those changes
by jonnycomputer 4y ago
Is there a way to add only certain lines of a file to a commit? Sometimes it'd be nice to say, "only these changes" make it to this commit, while "those changes" will be for another. Easy enough to do when they happen to be in different files, but sometimes that doesn't work out.
- simonw 4y agoYeah, you can do that with "git add -i" - it's not got a great interface but once you've learned how to use the "patch" and "split" commands you can generally craft commits with just specific lines pretty effectively.
- iib 4y ago`git add -p` should do it. git works great by "patches", not necessarily by "files", so what you are saying is supported out of the box really well.
- 9dev 4y agoIf you use an IntelliJ IDE, they have this— I frequently select just a few lines in a diff viewer and commit selectively what is logically coherent. Not affiliated, just a big fan.
- Noumenon72 4y agoYup, this is awesome. Just flit from one change to the next with Opt-Down Arrow and uncheck the ones you don't want to commit.
- other_herbert 4y agoIt also will track changes you don’t want… like if you have something you’ve changed for testing that can be kept in a separate change list (I call mine “ignore-for-now”) and the n future changes to that file would be in the regular changes, just minus the other stuff… also not affiliated, another fan
- deleted 4y ago[deleted]
- wara23arish 4y agoI know this is made easy with magit
- ElectricalUnion 4y agogit-gui, the Motif (either retro awesome or ugly as a sin, depending on who you ask) tcl/tk gui, besides being pretty fast, can do that. I get side looks every time I commit something in the middle of a file without commiting the entire file. Select a file by clicking it's name. Select the lines, alternative click then "stage this hunk/line to commit". You can also add/remove entire files from staging by clicking the file icons.
- yCombLinks 4y agoI use git kraken and you can easily choose individual lines in a file to include.
- sreevisakh 4y agoOther commenters have mentioned interactive and patch staging. That is the standard way to ensure that only the lines you specify go into a commit. I want to add an alternative method that offers some extra advantages. Patch stack management tools like stacked-git and topgit allow you to setup a number of patches (using git). You can then specify which patch each change goes into. This is sort of like having multiple staging area/indexes at your disposal. They also allow you to add commit messages to each patch at any stage of the development. You could also add, delete, split, squash or edit each patch at any time. And these patches are converted to regular commits at the end of development of the feature. You can also convert any commits into patches if you ever need it. The attraction that I see in this workflow is that it allows you to combine the chaotic development workflow and final rebasing step into a single stage.
- jonnycomputer 4y agoThank you everyone for your great replies. I'm excited to try this out!