git - 为什么 'git commit' 不保存我的更改?

标签 git github version-control git-commit git-status

我做了一个 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")

但之后,当我执行 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")

我做错了什么?

最佳答案

正如消息所说:

no changes added to commit (use "git add" and/or "git commit -a")

Git 有一个“暂存区”,文件在提交前需要添加到这里,你可以 read an explanation of it here .


对于您的具体示例,您可以使用:

git commit -am "save arezzo files"

(注意标志中额外的 a,也可以写成 git commit -a -m "message" - 两者做同样的事情)

或者,如果您想对添加到提交中的内容更有选择性,可以使用 git add 命令将适当的文件添加到暂存区,以及 git status 预览即将添加的内容(记住要注意使用的措辞)。

您还可以在 git documentation page 上找到有关如何使用 git 的一般文档和教程。 这将提供有关暂存/添加文件概念的更多详细信息。


另一件值得了解的事情是 interactive staging - 这允许您将文件的部分 添加到暂存区,因此如果您进行了三个不同的代码更改(针对相关但不同的功能),您可以使用交互模式来拆分更改并依次添加/提交每个部分。像这样进行较小的特定提交会很有帮助。

关于git - 为什么 'git commit' 不保存我的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7704480/

相关文章:

version-control - Mercurial - 插入子 repo 头

git - 如何将以前的存储库版本克隆到本地计算机?

javascript - 垂直制表符在 github 上显示不正确

Git - 撤销功能无法检查证书的撤销

svn - 用于分支共享库(并保持理智)的存储库布局(或 Hook )

svn - 当您开始重写程序时,您在源代码控制存储库中做什么?

git - SVN 和 Git 中的发布管理

git - 如何在 GitHub 中 stash SVG 差异(或将 SVG 显示为图像)

javascript - 跨存储库共享 react native 组件

android - 混帐 : How to Push missing branches from one repo to another repo