Commit changes only in one directory in Git

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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)

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

Do it all in one command:

git commit -- my-dir

Solution 6 - Git

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.

Solution 7 - Git

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 https://stackoverflow.com/questions/348170/undo-git-add , 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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - GitseheView Answer on Stackoverflow
Solution 2 - GitRob KennedyView Answer on Stackoverflow
Solution 3 - GitralphtheninjaView Answer on Stackoverflow
Solution 4 - Gituser6120452View Answer on Stackoverflow
Solution 5 - GitHazokView Answer on Stackoverflow
Solution 6 - GitvcsjonesView Answer on Stackoverflow
Solution 7 - GitninjageckoView Answer on Stackoverflow