62

If I have made multiple sets of changes to a large file, is it possible to split those into separate commits using git?

markdorison
  • 139,374
  • 27
  • 55
  • 71
  • Possible duplicate of [How to split a commit into smaller commits with Git?](http://stackoverflow.com/questions/2118042/how-to-split-a-commit-into-smaller-commits-with-git). –  Jul 14 '13 at 19:34
  • 4
    Also, it's not clear if the changes have already been committed or not. If they've been committed in several commits already, `rebase --interactive` is the way to go. If not, `git add --patch` is one of the options you should consider. –  Jul 14 '13 at 19:36

4 Answers4

101

You want git add --patch (documentation), which will allow you to select which changes to stage.

dumbledad
  • 16,305
  • 23
  • 120
  • 273
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 6
    Why the downvote? git add -i is overkill if all you want is to select hunks, since the first option you'll pick from the -i menu is 'p' (or 5), so you might as well have jumped straight to it with --patch. – William Pursell Feb 10 '11 at 12:25
  • 11
    Really, another downvote with no explanation? If you believe there is something wrong with `git add --patch`, please have the courtesy to provide an explanation as to why. – William Pursell Apr 29 '14 at 12:38
  • 3
    I think the downvotes (not from me!) are from folk who have already committed, where this answer won't work. The question doesn't say whether the changes are committed or not. – mikemaccana Apr 05 '16 at 18:20
  • 5
    Short form to this is `git add -p` which allows us to select which hunk to stage – Venkat Ch Oct 20 '16 at 07:31
  • @mikemaccana the question does not say that the files are commited. I updated this answer. – stephanmg Feb 12 '21 at 10:58
18

Yes, you can -- use git add -i to select which hunks you want to stage for each commit. You can get documentation by running git help add and scrolling to "Interactive Mode".

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
1

One more option which I constantly use is just because i forget about the patch option :):

Say you have updated files: aaa.txt, bbb.txt, ccc.txt and you want to push the aaa.txt file in the first commit and then bbb.txt and ccc.txt in the second commit:

step 1:
--------------------
git add aaa.txt
git commit -m "Added first file"
git stash
git push

step 2:
--------------------
git stash pop
git add -A
git commit -m "Added the rest of the modified data"
git push

Might be a little more verbose, but in my opinion I it gives a bit more control.

Cheers!

syntax-punk
  • 3,736
  • 3
  • 25
  • 33
0

Williams answer is perfectly valid. But sometimes it is easier to do things by hand. For example if you accidentally updated some third-party library with a lot of files before committing the changes you previously made. With git add -p (same as --patch) you would need to walk through all of this files. So in this case it is much more convenient to just stage the file you want to commit and do a second commit with all of the other changes:

> git add /path/to/your/file.txt
> git commit -m "my commit message"
[master a0c5ea6] my commit message
1 file changed, 2 insertions(+), 1 deletion(-)
> git add --all
> git commit -m "updated library xyz"
Community
  • 1
  • 1
AvL
  • 3,083
  • 1
  • 28
  • 39
  • 7
    Actually, `git add -p` allows you to select the particular file at the command line, and within the interactive session you can jump to a particular hunk using `g`, or search for a hunk matching a regex using `/`. The point of `add -p` is to select hunks from a file, but this proposed solution requires adding the entire file, which is not what the OP wants. – William Pursell Apr 29 '14 at 12:42