git - 使用 Git 提交时如何忽略对文件的更改?

标签 git branch

我确实搜索了解决方案,但我相信我的情况与我读到的情况有些不同。

我在一个特定的分支上工作并犯了一个错误,以与分支主题无关的方式编辑了一个文件,现在希望这些更改不会进入提交。我不想丢失这些更改,因为稍后我会尝试以某种方式在另一个分支上提交它们。

我试过 git rm --cached 但后来我在我的 git status 中看到状态 deleted - 文件会被删除吗 来自存储库?我不希望那样 - 我希望它与之前在我的工作分支上提交的内容保持不变。

最佳答案

如果您不暂存更改,它们将不会成为提交的一部分,如下所示:

git status
# On branch development
# 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:   ResearchProposal.tex

这里,ResearchProposal.tex 有变化,但是不会被git commit 提交。

如果您在一个文件中有两组更改,一些您想要提交,一些您不想提交,请阅读this .

如果您在不想提交的文件上运行了git add,您需要unstage它:

git reset HEAD <file>

像这样:

$ git add ResearchProposal.tex
$ git status
# On branch development
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   ResearchProposal.tex

$ git reset HEAD ResearchProposal.tex 
Unstaged changes after reset:
M   ResearchProposal.tex

$ git status
# On branch development
# 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:   ResearchProposal.tex

关于git - 使用 Git 提交时如何忽略对文件的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18508421/

相关文章:

android - 比特桶 : The requested URL returned error: 402

android - 未知选项 `allow-unrelated-histories'

git - 将提交从 GitHub 中的一个分支移动到另一个分支?

git - 为 Git 分支名称创建别名

git - GitLab 中名为 [1] 的分支是什么?

Git:创建一个跟踪远程master的远程分支

Git 子模块 : It is possible to provide both https and ssh access to submodules?

git - 如何判断 git merge 是否可快进

git - 启用 git rerere 有什么缺点吗?

git - 数据包错误: "Package was referenced, but it was not found in the paket.lock file"