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? 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 .

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 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 update-index --no-assume-unchanged path/to/file.txt

可能的解决方案之一:

git stash
git checkout <another-branch>
git stash apply
git add <one-file>
git commit 
git stash
git checkout <original-branch>
git stash apply
链接地址: http://www.djcxy.com/p/4182.html

上一篇: eclipse中的部分git提交(egit)

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