259

I love git add --interactive. It is now part of my daily workflow.

The problem seems that it does not work with untracked files. What I want to do is track a new file, but only add part of it, i.e. some parts of this new file are not yet ready to be staged.

For example, with git add -i, I can chose the patch option and even edit individual hunks in order to stage parts of the new code, leaving debug code comments unstaged. I love working this way because it makes it obvious which places of the mega patch I am currently working on still need work.

Unfortunately, I don't seem to be able to do the same with an untracked file. Either I stage the whole file, or nothing. The workaround I have been using is staging or even committing a new file when it is empty, and then staging individual changes in the usual way. But this solution feels like a dirty hack and when I forget, or change my mind, it creates more troubles than there should be.

So the question is: How to stage only part of a new file, so that this new file gets tracked but leaving the whole or parts of its content unstaged?

0

5 Answers 5

502

Whoa, all that update-index and hash-object business seems overly complicated. How about this instead:

git add -N new_file
git add -i  # or 'git add -p' if you prefer

From git help add:

-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.
7
  • 10
    ++1! due credit. I do trust a bit too much that I know these manpages by now .... </shame>
    – sehe
    Jun 22, 2011 at 13:20
  • 2
    Well found. I should have scrolled the man page a bit further. Perhaps I should return Git Pro badge.
    – dave1010
    Jun 23, 2011 at 8:14
  • 4
    Wonder why git-gui doesn't use this internally to be able to stage lines from a new file.
    – Deiwin
    Aug 6, 2014 at 14:20
  • 9
    Wonderful -N discovery, thanks! Though I'd prefer combining it with -p in that case. gid add -N the_file & git add -p. One should add that -N also works with -A Aug 25, 2015 at 9:30
  • @Deiwin - you could add a [guitool] to your .gitconfig to do a "git add -N $FILENAME": [guitool "Add with intent"] cmd = "git add -N $FILENAME" needsfile = yes noconsole = yes Dec 8, 2017 at 14:40
8
git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
git add --interactive newfile

Simple demo:

mkdir /tmp/demo
cd /tmp/demo
git init .

echo hello > newfile
git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile
  • Hint If you're sure the 'empty' blob already exists in your git object database, you could hardcode the hash e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 instead. I don't recommend doing that_
  • Hint If you're on Windows, you probably can just use NUL: instead of /dev/null. Otherwise, use something like echo -n '' | git hash-object --stdin -w

Now the index will contain newfile as the empty blob, and the empty blob has been entered into the object database if it didn't exist yet:

$ find .git/objects/ -type f 
.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391

$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   newfile
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   newfile
#

$ git diff
diff --git a/newfile b/newfile
index e69de29..ce01362 100644
--- a/newfile
+++ b/newfile
@@ -0,0 +1 @@
+hello

This should be precisely what you want. May I also recommend the vim fugitive plugin for very intelligent index management (see Better git add -p?)

2
  • 1
    This seems to work well. I've aliased it to "stage-empty-file" as it's a bit complex to remember.
    – dave1010
    Jun 22, 2011 at 12:27
  • @dave1010: Glad that it helped! Yeah I thought about adding an alias, but I didn't know how you'd expect to handle e.g. wildcards or multiple filename arguments in general. So I opted not to
    – sehe
    Jun 22, 2011 at 13:14
7

The easiest way to do this (and imho interactive staging in general) is git gui. It comes bundled with git and should work on almost all platforms that are supported by git.

Simply run git gui and a gui will open that allows staging and unstaging hunks and even single lines of tracked and untracked files.

Git Gui screenshot

7
  • This is a creative answer and sometimes it's very, very useful. Oct 4, 2013 at 3:56
  • 3
    Just a note that not all git distributions include git gui. In Debian, for example, it is in a separate package called 'git-gui'.
    – cristoper
    Jul 1, 2014 at 18:16
  • Have been looking (not hard enough) for this feature for years, this is a life changer Nov 21, 2014 at 7:12
  • 3
    For me the staging of single lines of untracked files in git gui has never worked. In fact, it disables these menu options.
    – Deiwin
    Jul 13, 2015 at 23:20
  • 3
    @Deiwin I must actually have missed this in the question – my screenshot shows an already tracked file. If you want to use git gui for a new file, you have to run git add -N new_file first – see Richard Hansen‘s answer.
    – Chronial
    Jul 15, 2015 at 9:40
3

Edit: this doesn't seem to be working now. I'm sure it was before (on git 1.7.1). In case it doesn't work, I suggest staging /dev/null as sehe suggests above:

git update-index --add --cacheinfo 100644 $(git hash-object -w /dev/null) newfile

If you're on Windows (without /dev/null) then you can replace it with the path to an empty file.

Original answer

You want

git add -p # (or --patch)

This adds untracked files for me. From the man page:

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.

2
  • 2
    However interesting, it doesn't work for me (git 1.7.4); git add -p newfile doesn't work at all (nu effect) and git add --interactive '[a]dd untracked' has the effect that git add newfile has; Edit also checked with git 1.7.5.3; no dice
    – sehe
    Jun 22, 2011 at 12:06
  • Good hint for Windows, added hints for that in my answer. Thx
    – sehe
    Jun 22, 2011 at 13:04
1

Just for the record of another possibility: I use

git add -N file
git add -p file

And then you can stage hunks, or edit them in place.

3
  • 1
    This answer says the same as the accepted answer, that dates back to 2011. Downvote. Oct 17, 2018 at 13:06
  • 4
    The accepted answer combines add -N with add -i. I just added that add -p instead of add -i works, too. Oct 18, 2018 at 18:42
  • 1
    Richard edited-in the -p comment 3 years after this answer. It's a fair game. +1 Apr 28, 2022 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.