为什么“git commit”不保存我的更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7704480/
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
Why does 'git commit' not save my changes?
提问by arezzo
I did a git commit -m "message"
like this:
我做了一个git commit -m "message"
这样的:
> git commit -m "save arezzo files"
# On branch master
# 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: arezzo.txt
# modified: arezzo.jsp
#
no changes added to commit (use "git add" and/or "git commit -a")
But afterwards, when I do git status
it shows the same modified files:
但是之后,当我这样做时,git status
它会显示相同的修改文件:
> git status
# On branch master
# 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: arezzo.txt
# modified: arezzo.jsp
#
no changes added to commit (use "git add" and/or "git commit -a")
What am I doing wrong?
我究竟做错了什么?
回答by Peter Boughton
As the message says:
正如消息所说:
no changes added to commit (use "git add" and/or "git commit -a")
没有更改提交(使用“git add”和/或“git commit -a”)
Git has a "staging area" where files need to be added before being committed, you can read an explanation of it here.
Git 有一个“暂存区”,在提交文件之前需要在其中添加文件,您可以在此处阅读它的解释。
For your specific example, you can use:
对于您的具体示例,您可以使用:
git commit -am "save arezzo files"
(note the extra a
in the flags, can also be written as git commit -a -m "message"
- both do the same thing)
(注意a
标志中的额外部分,也可以写为git commit -a -m "message"
- 两者都做同样的事情)
Alternatively, if you want to be more selective about what you add to the commit, you use the git addcommand to add the appropriate files to the staging area, and git statusto preview what is about to be added (remembering to pay attention to the wording used).
或者,如果你想对添加到提交中的内容更有选择性,可以使用git add命令将适当的文件添加到暂存区,并使用git status预览即将添加的内容(记住要注意使用的措辞)。
You can also find general documentation and tutorials for how to use git on the git documentation pagewhich will give more detail about the concept of staging/adding files.
您还可以在git 文档页面上找到有关如何使用 git 的一般文档和教程,该页面将提供有关暂存/添加文件概念的更多详细信息。
One other thing worth knowing about is interactive staging- this allows you to add parts of a fileto the staging area, so if you've made three distinct code changes (for related but different functionality), you can use interactive mode to split the changes and add/commit each part in turn. Having smaller specific commits like this can be helpful.
值得了解的另一件事是交互式暂存- 这允许您将文件的一部分添加到暂存区域,因此如果您进行了三个不同的代码更改(对于相关但不同的功能),您可以使用交互模式来拆分依次更改和添加/提交每个部分。像这样进行较小的特定提交可能会有所帮助。
回答by Femaref
You didn't add the changes. Either specifically add them via
您没有添加更改。要么通过专门添加它们
git add filename1 filename2
or add all changes (from root path of the project)
或添加所有更改(从项目的根路径)
git add .
or use the shorthand -a
while commiting:
或-a
在提交时使用速记:
git commit -a -m "message".
回答by Baptiste Pernet
You should do:
你应该做:
git commit . -m "save arezzo files"
回答by Simon Hutchison
I copied a small sub project I had that was under Git source control into another project and forgot to delete the .git folder. When I went to commit I got the same message as above and couldn't clear it until I deleted the .git
folder.
我将一个在 Git 源代码控制下的小型子项目复制到另一个项目中,但忘记删除 .git 文件夹。当我去提交时,我收到了与上面相同的消息,并且在删除.git
文件夹之前无法清除它。
It is a bit silly, but it is worth checking you don't have a .git folder under the folder that doesn't commit.
这有点傻,但值得检查一下,您在未提交的文件夹下没有 .git 文件夹。
回答by Albert Vonpupp
You could have done a:
你可以做一个:
git add -u -n
To check which files you modified and are going to be added (dry run: -n option), and then
要检查您修改了哪些文件并将添加哪些文件(试运行:-n 选项),然后
git add -u
To add just modified files
添加刚刚修改的文件
回答by jam99
I find this problem appearing when I've done a git add .
in a subdirectory belowwhere my .gitignore
file lives (the home directory of my repository, so to speak). Try changing directories to your uppermostdirectory and running git add .
followed by git commit -m "my commit message"
.
我发现,当我做了一个出现此问题git add .
的子目录下面在我的.gitignore
文件生命(我的仓库的主目录,可以这么说)。尝试将目录更改为最上面的目录并运行,git add .
然后运行git commit -m "my commit message"
.
回答by Martin
Maybe an obvious thing, but...
也许是显而易见的事情,但是......
If you have problem with the index, use git-gui. You get a very good view how the index (staging area) actually works.
如果索引有问题,请使用git-gui。您可以很好地了解索引(暂存区)的实际工作方式。
Another source of information that helped me understand the index was Scott Chacons "Getting Git" page 259 and forward.
另一个帮助我理解索引的信息来源是 Scott Chacons 的“Getting Git”第 259 页及以后。
I started off using the command line because most documentation only showed that...
我开始使用命令行,因为大多数文档只显示...
I think git-gui and gitk actually make me work faster, and I got rid of bad habits like "git pull" for example... Now I always fetch first... See what the new changes really are before I merge.
我认为 git-gui 和 gitk 实际上让我工作得更快,并且我摆脱了诸如“git pull”之类的坏习惯……现在我总是先获取……在我合并之前看看新的变化到底是什么。
回答by drjorgepolanco
The reason why this is happening is because you have a folder that is already being tracked by Git inside another folder that is also tracked by Git. For example, I had a project and I added a subfolder to it. Both of them were being tracked by Git before I put one inside the other. In order to stop tracking the one inside, find it and remove the Git file with:
发生这种情况的原因是,您有一个已被 Git 跟踪的文件夹,位于另一个也被 Git 跟踪的文件夹中。例如,我有一个项目,并向其中添加了一个子文件夹。在我将一个放入另一个之前,它们都被 Git 跟踪。为了停止跟踪里面的那个,找到它并删除 Git 文件:
rm -rf .git
In my case I had a WordPress application and the folder I added inside was a theme. So I had to go to the theme root, and remove the Git file, so that the whole project would now be tracked by the parent, the WordPress application.
就我而言,我有一个 WordPress 应用程序,我在里面添加的文件夹是一个主题。因此,我必须转到主题根目录,并删除 Git 文件,以便父级 WordPress 应用程序现在可以跟踪整个项目。
回答by drjorgepolanco
if you have more files in my case i have 7000 image files when i try to add them from project's route folder it hasn't addedthem but when i go to the image folder everything is ok. Go through the target folder and command like abows
如果在我的情况下有更多文件,当我尝试从项目的路由文件夹添加它们时,我有 7000 个图像文件,但它没有添加它们,但是当我转到图像文件夹时,一切正常。浏览目标文件夹和命令,如 abows
git add .
git commit -am "image uploading"
git push origin master
git push origin master Enumerating objects: 6574, done. Counting objects: 100% (6574/6574), done. Delta compression using up to 4 threads Compressing objects: 100% (6347/6347), done. Writing objects: 28% (1850/6569), 142.17 MiB | 414.00 KiB/s
git push origin master 枚举对象:6574,完成。计数对象:100% (6574/6574),完成。使用最多 4 个线程的 Delta 压缩 压缩对象:100% (6347/6347),完成。写入对象:28% (1850/6569),142.17 MiB | 414.00 KiB/s
回答by brianwaganer
I had a very similar issue with the same error message. "Changes not staged for commit", yet when I do a diff it shows differences. I finally figured out that a while back I had changed a directories case. ex. "PostgeSQL" to "postgresql". As I remember now sometimes git will leave a file or two behind in the old case directory. Then you will commit a new version to the new case.
我有一个非常相似的问题,同样的错误消息。“未为提交而暂存的更改”,但是当我进行差异时,它会显示差异。我终于发现不久前我改变了一个目录案例。前任。“PostgeSQL”到“postgresql”。正如我现在记得的,有时 git 会在旧的 case 目录中留下一两个文件。然后,您将向新案例提交新版本。
Thus git doesn't know which one to rely on. So to resolve it, I had to go onto the github's website. Then you're able to view both cases. And you must delete all the files in the incorrect cased directory. Be sure that you have the correct version saved off or in the correct cased directory.
因此 git 不知道该依赖哪一个。所以为了解决它,我不得不去github的网站。然后您就可以查看这两种情况。并且您必须删除不正确的 cased 目录中的所有文件。确保您保存了正确的版本或保存在正确的 cased 目录中。
Once you have deleted all the files in the old case directory, that whole directory will disappear. Then do a commit.
删除旧案例目录中的所有文件后,整个目录将消失。然后做一个提交。
At this point you should be able to do a Pull on your local computer and not see the conflicts any more. Thus being able to commit again. :)
此时,您应该能够在本地计算机上执行 Pull 操作,并且不再看到冲突。从而能够再次提交。:)