66

I'm using Git 1.7.4.1 on Mac 10.6.6. From the command line, how do I commit changes in only a single directory? I added the directory by doing:

git add my-dir

but doing

git commit -a

brings up a list of all changes in my repo, and I only want to commit and push changes from my-dir.

random
  • 9,774
  • 10
  • 66
  • 83
Dave
  • 8,667
  • 25
  • 72
  • 90

7 Answers7

82

Why does no one mention you can simply

git commit -m 'message' -- my-dir

It seems to me the OP isn't used to / doesn't like to use the staging area directly. This approach is also a lot safer to recommend without further context, because chances are that a defaault commit (of everything that's staged) will

  • commit more than just my-dir if it already had been staged
  • will produce confusing results when the OP is not used to managing the staging area explicitly (because the working tree can have gotten out of synch with the index)
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    I believe I *did* mention that — at least the part of it that was relevant to the question. (Whether the commit message is included in the command line or prompted for later is a separate issue from the topic at hand, which was how to commit a specific directory without committing everything else.) – Rob Kennedy May 02 '11 at 22:00
  • 1
    Oops. I missed it (the `-a` talk threw me off track?). I might subconsciously scan for the **`--`** token. (_oh and the commit message thing is just habit :)_) – sehe May 02 '11 at 22:05
54

Omit the -a option. Then git will commit only the files that you staged with git add.

You can also try committing the directory without staging with git commit my-dir.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 1
    If I do the second thing you say (try committing without staging), which changes will be committed? – Stefan Monov Dec 19 '16 at 20:47
  • In that case, @Stefan, "the commit will ignore changes staged in the index, and instead record the current content of the listed files (which must already be known to Git)," says [the documentation](https://git-scm.com/docs/git-commit). – Rob Kennedy Jul 12 '17 at 14:18
  • Also if you have staged some excluded files, you can unstage them with: $ git reset -- and then $ git commit -m 'msg' and tthen $ git push – Mohsen Abasi Dec 27 '20 at 05:26
20

Hmm, odd. The following should work. Assuming you have nothing staged.

git add my-dir
git commit -m "Commiting first revision of my-dir folder. Very exciting feature!"
git push origin master
rtn
  • 127,556
  • 20
  • 111
  • 121
  • I really don't understand your question, but that's probably my own fault :) I rewrote my answer. I hope it is to more satisfaction. – rtn May 02 '11 at 21:39
  • Well, that's a little bit clearer. I still don't get why you are '_yelling_' that it must. In fact, that will only be correct once you limit the scope of the `commit` as well (since you cannot know what else is staged already) – sehe May 02 '11 at 21:56
  • Ok, I follow you. Was not my intention to yell, it was just to emphasize the word. Replaced with should. And yes of course, I assumed that nothing else was added to the index. – rtn May 02 '11 at 22:03
12

Basic

git add -- ./myfolder
git commit -m'my comment' -- ./myfolder

With more files and dirs

git add -- ./myfolder1 ./myfolder2./some/random/file.txt
git commit -m'cool' -- ./myfolder1 ./myfolder2 ./some/random/file.txt

What I was looking for git bare repo

git --git-dir=path/to/my/repo --work-tree=path/to/my/working/tree add -- ./myfolder
git --git-dir=path/to/my/repo --work-tree=path/to/my/working/tree commit -m'my comment' -- ./myfolder

Remember to quote paths with spaces or crazy chars

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user6120452
  • 121
  • 1
  • 2
4

Just stage the folder using git add as you specified, and do a commit without the -a option: git commit -m "Committing stuff". The -a option means commit all files which have been modified, even if they aren't staged.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
4

Do it all in one command:

git commit -- my-dir
Hazok
  • 5,373
  • 4
  • 38
  • 48
  • Same [answer from sehe](/a/5862894/938111) is two days older and provides some explanations. Same [answer from user6120452](http://stackoverflow.com/a/36247058/938111) provides useful details. – oHo Jan 06 '17 at 19:04
2

You will commit any changes in the "staging area"; you can see these with git status.

the -a flag in git commit -a, according to the man page, tells git to roughly "stage all files that have been modified or deleted, but not new files you have not told git about" - this is not what you want

the lesson is to be aware of what command line options do

To fix this, the first thing you want to do is, according to How to undo 'git add' before commit? , to unstage all the files you've accidentally added with the commit -a option. According to that answer, you must perform the command git rm -r --cached ., and now your changes should still be there, but nothing is staged.

Now you can do git add my-dir like you did before. Then you can do git commit (WITHOUT THE -a)

Community
  • 1
  • 1
ninjagecko
  • 88,546
  • 24
  • 137
  • 145