仅在 Git 中的一个目录中提交更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5862233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Commit changes only in one directory in Git
提问by Dave
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:
我在 Mac 10.6.6 上使用 Git 1.7.4.1。从命令行,如何仅在单个目录中提交更改?我通过执行以下操作添加了目录:
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
.
调出我的 repo 中所有更改的列表,我只想提交和推送my-dir
.
采纳答案by Rob Kennedy
Omit the -a
option. Then git
will commit only the files that you staged with git add
.
省略该-a
选项。然后git
将仅提交您暂存的文件git add
。
You can also try committing the directory without staging with git commit my-dir
.
您也可以尝试提交目录而不使用git commit my-dir
.
回答by sehe
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
在我看来,OP 不习惯/不喜欢直接使用暂存区。在没有进一步上下文的情况下推荐这种方法也更安全,因为很可能默认提交(所有已上演的)将
- 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)
- 如果已经上演了 my-dir,则提交的不仅仅是 my-dir
- 当 OP 不用于显式管理暂存区时,会产生令人困惑的结果(因为工作树可能与索引不同步)
回答by ralphtheninja
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
回答by user6120452
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 --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
记住用空格或疯狂字符引用路径
回答by Hazok
Do it all in one command:
在一个命令中完成所有操作:
git commit -- my-dir
回答by vcsjones
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.
只需git add
按照您指定的方式暂存文件夹,并在没有-a
选项的情况下进行提交:git commit -m "Committing stuff"
。该-a
选项意味着提交所有已修改的文件,即使它们没有暂存。
回答by ninjagecko
You will commit any changes in the "staging area"; you can see these with git status
.
您将在“暂存区”中提交任何更改;你可以看到这些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
根据手册页,中的-a
标志git commit -a
告诉 git 粗略地“暂存所有已修改或删除的文件,但不暂存您没有告诉 git 的新文件”-这不是您想要的
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.
要解决此问题,您要做的第一件事是根据如何在提交前撤消“git add”?, 取消暂存您使用该commit -a
选项意外添加的所有文件。根据那个答案,您必须执行 command git rm -r --cached .
,现在您的更改应该仍然存在,但没有任何内容。
Now you can do git add my-dir
like you did before. Then you can do git commit
(WITHOUT THE -a
)
现在你可以git add my-dir
像以前那样做。然后你可以做git commit
(没有-a
)