Git - stage only a part of a file

So, did it ever happen to you? You made some changes to a file and you want to stage them, but you messed around with some other lines also and those belong to another feature that you will commit right after this one. Ok, the situation is kinda awkward and you don't want to export a patch, checkout the file again and only apply the right changes for the first feature - that would be awful.

Don't worry! Git will help you save the day. Maybe you didn't know, but you can stage only a part of a file - isn't that awesome?

First, let be guide you the best resource to find out git's awesome features - the git help.

1. How to use git help?

It's really easy, you have a couple of different ways to use git's help.

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

2. Let's check git help add

-p, --patch
    Interactively choose hunks of patch between the index 
    and the work tree and add them to the index. This gives 
    the user a chance to review the difference before adding 
    modified contents to the index. This effectively runs add 
    --interactive, but bypasses the initial command menu and 
    directly jumps to the patch subcommand.
    See "Interactive mode" for details.

Ok, so using git add -p filename you're running the add interactively and jump to patch command. Let's see how it looks.

index 86ccd6a..1103871 100644
--- a/test.txt
+++ b/test.txt
@@ -1,5 +1,9 @@
 This is revision one of this file.

-It has some random content so I can demosntrate a few aspects of git.
+It has some random content so I can demonstrate a few aspects of git.
 Git is a really nice distributed version control system with a lot
 of features that help you be more productive. It also saves lives!
+
+Never underestimate its power!
+
+Git is magic :)
Stage this hunk [y,n,q,a,d,/,s,e,?]?

So I have here a file that has one line removed and then added with a fix (typo) and some text added at the end of the file. Let's say I only want to stage the fix. Since this batch of changes covers two things instead of one, we can use the provided actions to split it further.

Notice the Stage this hunk [y,n,q,a,d,/,s,e,?]? ? Well, if you type h or ? you'll get a full menu with the commands and the description for each of them, but let's see fast what each of them does:

  • y - stage the hunk
  • n - don't stage the hunk
  • q - quit
  • a - stage this hunk and the following hunks in this file
  • d - don't stage this hunk an the following hunks in this file
  • / - search for hunk matching regex
  • s - split this hunk in smaller hunks
  • e - manually edit this hunk
  • ? - help

My initial goal was to only stage the first change of that chunk so I'll hit s and let's see what happens.

Split into 2 hunks.
@@ -1,5 +1,5 @@
 This is revision one of this file.

-It has some random content so I can demosntrate a few aspects of git.
+It has some random content so I can demonstrate a few aspects of git.
 Git is a really nice distributed version control system with a lot
 of features that help you be more productive. It also saves lifes!
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?

Cool! The initial chunk was split into two and now I can stage my change. I'll hit y and I'll go ahead and commit my changes.


I know at first it might seem strange, but the git add -p is an awesome tool that will make your commits more clear than before.

Start using it! Your future self will thank you.

Written by Bogdan Constantinescu on
Tagged: git