Search code examples
gitgit-commitgit-add

Is there a way in git to split up changes in a single file into two commits?


So I a have a file:

...

some code here..

...

some unrelate code here..

...

and I make the following changes to it:

...

some code here that needs to be changed a bunch..

...

some unrelated code here..

...

Let's say I'm in the middle of some significant changes to the first section and I notice the typo in the later section. I want to fix the typo and commit and possibly push that right away but I'm still working on the first part and not read to share it. Obviously I could use git stash or do an intermediate commit and fix the typo in another branch but is there any way of adding only some of the changes in file to the staging area. Obviously I can unfix the typo, git add myfile then refix the typo but if the typo fix is more complex that could be kind of annoying. Is there a way to specify the lines in the file that I want to add with git add?


Solution

  • The -p flag will allow you to select which parts of a file you are adding. There's a popular question here which provides more details on the patch flag.