Question

Before I stage files, I like to perform a git diff to check that everything is okay. While I'm vertically traversing down a diff, I make a mental note of each file that is "OK" for staging. If it's a particularly long diff, I find it hard to keep track of which ones were OK'ed, so I tend to quit out of the diff at intervals and stage what I can. This gets a bit tedious.

Is there a way I can improve this workflow? Obviously, one should not let their diff get huge in the first place, but sometimes things don't work out like that (e.g. changing the method signature for a method that is called across many files).

For instance, is it possible to stage everything from the "top" of the currently active git diff "session" to the current point in the diff that you are at? This way I could just keep staging without quitting the diff and losing my train of thought.

Was it helpful?

Solution

Try using git add -p instead. It will show you each of the diff hunks and ask you if you want to add them.

OTHER TIPS

You can do this with a partial add:

git add -p

It will take you through different hunks of the file, letting you stage or ignore as you see fit for each one.

Use:

git add --interactive 

I've made an alias for it in ~/.bashrc:

alias gi='git add --interactive'

Learn how to us git add -i to its fullest potential. It's now part of my daily workflow and that's one of the things I love the most about git! :)

The other answers are hiding the other nice features of git add -i:

   -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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top