-1

This answer says that git commit --include is equivalent to a git add of the specified files, followed by a git commit.

The man-page says:

Before making a commit out of staged contents so far, stage the contents of paths given on the command line as well.

However, I can't get it to work as advertised:

% git init
Initialized empty Git repository in /tmp/git/.git/
% touch foo
% git commit -m "Why doesn't this work?" --include foo
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        foo

nothing added to commit but untracked files present (use "git add" to track)

How do I get it to work as it's apparently supposed to?


Historical note: This question previously used the functionally equivalent command git commit -m "Why doesn't this work?" --include -- foo, which confused some people by the inclusion of the unnecessary -- pathspec separator.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • [stage the contents of **paths given on the command line** as well](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---include) You've not passed any file names. What is `-- foo` supposed to be? – Liam May 15 '20 at 10:55
  • I've updated the answer to which you linked to add a bold phrase in the footnote where I say "in essence". The description of `--include foo` as acting a lot like doing a `git add foo` isn't meant to say that it is *exactly* the same, because it isn't; this is one of those places where it isn't. – torek May 15 '20 at 18:50
  • 1
    @Liam: `foo` is the path of a file, as noted by `touch foo` in the setup. The `--` stops option parsing and isn't required since `foo` does not resemble an option. I actually think this is a pretty good question, by the way (despite some downvoters) because it exposes what's pretty clearly a bug in Git. *Silently* ignoring a file like this is not good. – torek May 15 '20 at 19:02

2 Answers2

2

git commit can only commit modified tracked files and staged changes. Untracked files cannot be committed even with --include. You need to git add untracked files before committing and there is no way around that.

git commit --include can be used this way:

edit file1 file2 # 2 tracked files
git add file1 # stage new changes
git commit --include file2

This git commit --include commits both file1 and file2. Without --include only file2 will be committed and file1 will stay staged awaiting another commit or reset.

phd
  • 82,685
  • 13
  • 120
  • 165
  • I think you've got it backwards... without `--include`, only the explicitly added `file1` would be committed. – Tom Hale May 16 '20 at 13:47
  • 1
    Bare `git commit` — yes, will commit staged `file1`. `git commit file2` will commit **only** `file2`. `git commit --include file2` will commit both. – phd May 16 '20 at 15:02
1

shouldn't this be:

git commit -m "Why doesn't this work?" --include foo

with foo directly after the --include (and no extra --)

(assuming foo is a filename)

  • 2
    The `--` is harmless: it just stops option parsing. For instance, if we had a file named `--patch` and we want to `git add` it, we can't run `git add --patch`, we have to add `git add ./--patch` or `git add -- --patch`. This problem comes up more often when someone accidentally creates a file named `-f` and tries to remove it with `rm -f` or `git rm -f`. – torek May 15 '20 at 19:05