简体   繁体   English

git:如何在提交时忽略对文件的更改?

[英]git: How to ignore changes to a file when committing?

I did search for the solution, yet I believe my situation is a bit different than those I read about. 我确实在寻找解决方案,但我相信我的情况与我读到的情况略有不同。

I am working on a particular branch and made a mistake, having edited a file in a way that is unrelated to the theme of the branch, and now want these changes to not make it into the commit. 我正在一个特定的分支上工作并犯了一个错误,以一种与分支主题无关的方式编辑了一个文件,现在希望这些更改不会进入提交。 I do not want to lose these changes, as I will try to commit them on another branch later, one way or another. 我不想丢失这些更改,因为我将尝试稍后以某种方式将它们提交到另一个分支。

I tried git rm --cached but then I see status deleted in my git status - will the file be removed from the repository? 我试过git rm --cached然后我看到我的git status deletedgit status - 该文件是否会从存储库中删除 I don't want that - I want it to stay unchanged from whatever it is in previous commit on my working branch. 我不希望这样 - 我希望它在我工作分支上的先前提交中保持不变。

If you do not stage the changes, they will not be part of the commit, something like this: 如果您不进行更改,它们将不会成为提交的一部分,如下所示:

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

Here, ResearchProposal.tex has changes, but they will not be committed by git commit . 这里, ResearchProposal.tex有变化,但它们不会被git commit

If you have two sets of changes in a single file , some that you want to commit and some that you don't, read this . 如果您在一个文件中有两组更改,一些是您要提交的,另一些是您不提交的,请阅读此内容

If you've run git add on a file that you don't want to commit, you need to unstage it: 如果你在一个你不想提交的文件上运行git add ,你需要unstage它:

git reset HEAD <file>

like this: 像这样:

$ 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

Run the following command in your terminal: 在终端中运行以下命令:

git update-index --assume-unchanged

To make Git track the file again, simply run: 要让Git再次跟踪文件,只需运行:

git update-index --no-assume-unchanged path/to/file.txt

One of the possible solutions: 一种可能的解决方案:

git stash
git checkout <another-branch>
git stash apply
git add <one-file>
git commit 
git stash
git checkout <original-branch>
git stash apply

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM