Commit changes only in one directory in Git
I'm using Git 1.7.4.1 on Mac 10.6.6. From the command line, h开发者_StackOverflowow 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
.
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)
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
.
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
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
Do it all in one command:
git commit -- my-dir
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.
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
)
精彩评论