1

I have a folder in my repository that contains a large number of files. If I have made changes to 20 files, and need to commit these files (say something like this)

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    SWIFT/db/add_sub_2lh.tdf
        deleted:    SWIFT/db/add_sub_ape.tdf
        deleted:    SWIFT/db/add_sub_bmb.tdf
        deleted:    SWIFT/db/add_sub_kka.tdf
        deleted:    SWIFT/db/cmpr_e7g.tdf
        deleted:    SWIFT/db/mult_6ct.v
        deleted:    SWIFT/db/prev_cmp_swift.qmsg
        deleted:    SWIFT/db/swift.(0).cnf.cdb
        deleted:    SWIFT/db/swift.(10).cnf.cdb
        deleted:    SWIFT/db/swift.(10).cnf.hdb
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   SWIFT/altpll_50M.bsf
        modified:   SWIFT/altpll_50M.cmp
        modified:   SWIFT/altpll_50M.qip
        modified:   SWIFT/altpll_50M.v
        modified:   SWIFT/altpll_50M/altpll_50M_0002.v
        modified:   SWIFT/altpll_50M_sim/mentor/msim_setup.tcl
        modified:   SWIFT/altpll_50M_sim/synopsys/vcs/vcs_setup.sh
        modified:   SWIFT/altpll_50M_sim/synopsys/vcsmx/vcsmx_setup.sh
        modified:   SWIFT/greybox_tmp/cbx_args.txt
        modified:   SWIFT/output_files/swift.flow.rpt

What I want to do is only commit the files that are staged for commit. I do not want the "changes not staged for commit" to be committed. However when I run

git commit .

I see that all my changes are included in the commit.

If I use the -m option,I need to provide the file names, and I am not too keen to do that as the file names are all different and there are instances in my workflow, where the files may be in various nested folders

[EDIT]

If I use just

git commit

It will includes all the files that I have staged for committing in all the folders in the repository. I wish to commit only the files I have staged for committiing in the present folder or matching some pattern that I can specify on the command line

Prashant
  • 353
  • 5
  • 17
  • When you say "in the present folder", do you mean excluding subdirectories, or do you just mean "in a given directory" (and its subdirectories)? – Davis Herring Jan 24 '18 at 05:01
  • No it will include sub diectories. – Prashant Jan 24 '18 at 05:06
  • @DavisHerring, I am not looking for a script that will help me achieve this. I was wondering if git had the option natively. Something along the lines of "Git - please commit all these changes in the following directories that I have staged for commit. Do not commit any unstaged changes" – Prashant Jan 24 '18 at 05:08

3 Answers3

3

Assuming that the number of files fits in a single command line (and that there are no wildcards in the file names, since git commit will consider them again),

git diff -z --cached --name-only SWIFT/greybox_tmp '*.cdb' |\
  xargs -0 git commit -m "..."

will commit the staged versions of all files in the directory SWIFT/greybox_tmp or whose names end in .cdb. (The --cached --name-only is due to Ben Jackson.)

The pathspec syntax is quite intricate and can even cover the "this directory but not subdirectories" case with . :\!*/* or (in newer versions) :^*/*. We do still have to do a bit of scripting because it doesn't support filtering by "differs between HEAD and index".

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Okay. But will this includes all the files that I have staged for committing in all the folders in the repository. I wish to commit only the files I have staged for committiing in the present folder or matching some pattern that I can specify on the command line – Prashant Jan 24 '18 at 04:37
  • @Prashant: You should edit your question, then; that’s rather less trivial. – Davis Herring Jan 24 '18 at 04:43
1

Simply remove the .

git commit
git commit -m "ABC."

From the docs:

git commit [args] [file]

In your command, . is interpreted to mean everything in the current directory, thus you are staging all the unstaged files before the commit.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • Note that this answers the original question as asked, but the OP then edited the original question into a substantially more complicated case. – torek Jan 24 '18 at 05:55
1

I wish to commit only the files I have staged for committing in the present folder or matching some pattern that I can specify on the command line

You would need to git stash push a/path in order to save your index and working tree: a pathspec can used a pattern.

Once what you don't want to commit is stashed, what remains in the index will be committed with a simple git commit.

Finally, a git stash pop will restore your working tree and index state.

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