Staging & committing a portion of a new file with git

Today I found myself wanting to merge a brand new file upstream, with way too many brand new dependencies in tow. I wasn’t ready to include any of those dependencies just yet, and of course I didn’t want to break the build. The solution? Stage a skeleton of my file, excluding any dependencies, so it can be merged nice and cleanly.

Unless you tell it otherwise, git-add will simply stage the entire file as one giant change. So, to tell it otherwise:

-N, --intent-to-add Record only the fact that the path will be added later. An entry for the path is placed in the index with no content. This is useful for, among other things, showing the unstaged content of such files with git diff and committing them with git commit -a.

This effectively stages a blank file (i.e. touch some_file.txt && git add some_file.txt), excluding the contents. You can then proceed to interactively stage some portion of that file:

git add --intent-to-add some_file.txt git add --patch some_file.txt

In —patch mode (part of —interactive), choose e to manually edit the current hunk, delete any lines you don’t want to stage, and commit as usual!


Originally published at dolphm.com on May 8, 2014.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.