1

I have a source tree with a bunch of modifications. I selected a big list of files and put then into a file. Is there a way to commit only these ones?

I want something like:

git commit --files "/tmp/files-to-commit.txt" -m "Fixed bug"
Carlo Pires
  • 4,606
  • 7
  • 32
  • 32

3 Answers3

1
git add `cat /tmp/files-to-commit.txt`
git commit -m "Fixed bug"

The `` do shell expansion. So if you have the names of files in a file, cat'ing them out will list them as args.

kjprice
  • 1,316
  • 10
  • 26
0

The files was stagged already, therefore I did not need to add them using git add. Besides, I didn't want to remove other files stagged together, I just wanted to commit a specific set of files.

I got it by this way:

git commit <first-file-of-list> -m "Fixed bug"

Then, removed the first-file-of-list from file list and commit the others using:

cat /tmp/files-to-commit.txt|while read file; do
    git commit $file --amend --m "Fixed bug"
done
Carlo Pires
  • 4,606
  • 7
  • 32
  • 32
0

With Git 2.25 (Q1 2020), 6 years later, a few commands (git add, git-commit, git reset) learned to take the pathspec from the standard input or a named file, instead of taking it as the command line arguments.

See commit e440fc5, commit 66a25a7, commit 64bac8d, commit d137b50, commit 24e4750 (19 Nov 2019), and commit add9770 (06 Nov 2019) by Alexandr Miloslavskiy (SyntevoAlex).
(Merged by Junio C Hamano -- gitster -- in commit c58ae96, 10 Dec 2019)

Example:

commit: support the --pathspec-from-file option

Signed-off-by: Alexandr Miloslavskiy

Decisions taken for simplicity:

  1. For now, --pathspec-from-file is declared incompatible with --interactive/--patch, even when <file> is not stdin.
    Such use case it not really expected.
    Also, it would require changes to interactive_add().

  2. It is not allowed to pass pathspec in both args and file

The git commit man page now includes:

--pathspec-from-file=<file>:

Pathspec is passed in <file> instead of commandline args.
If <file> is exactly - then standard input is used.
Pathspec elements are separated by LF or CR/LF.
Pathspec elements can be quoted as explained for the configuration variable core.quotePath

--pathspec-file-nul:

Only meaningful with --pathspec-from-file.
Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes).


Still with Git 2.25 (Q1 2020), the above series (teaching "--pathspec-from-file" to "git commit") forgot to make the option incompatible with "--all", which has been corrected.

See commit 509efef (16 Dec 2019) by Alexandr Miloslavskiy (SyntevoAlex).
(Merged by Junio C Hamano -- gitster -- in commit ff0cb70, 25 Dec 2019)

commit: forbid --pathspec-from-file --all

Reported-by: Phillip Wood
Signed-off-by: Alexandr Miloslavskiy

I forgot this in my previous patch --pathspec-from-file for git commit (Commit e440fc58).
When both --pathspec-from-file and --all were specified, --all took precedence and --pathspec-from-file was ignored.
Before --pathspec-from-file was implemented, this case was prevented by this check in parse_and_validate_options() :

die(_("paths '%s ...' with -a does not make sense"), argv[0]);

It is unfortunate that these two cases are disconnected.
This came as result of how the code was laid out before my patches, where pathspec is parsed outside of parse_and_validate_options(). This branch is already full of refactoring patches and I did not dare to go for another one.

Fix by mirroring die() for --pathspec-from-file as well.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250