8

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.

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
  • 1
    Just don't stage some of the files. This is a completely normal and trivial use of Git. You'll be doing this more often than not with each and every commit. Are you using `git add .` or `git commit -a`? Stop using that, and start using `git add -p` *every single time*. – user229044 Aug 29 '13 at 10:37

3 Answers3

11

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
Community
  • 1
  • 1
simont
  • 68,704
  • 18
  • 117
  • 136
  • The file is listed among changes to be committed. I don't know how it got there, probably because I `add`ed it accidentally earlier, or for another reason. So I guess, now I need to somehow make it appear among "changes not staged for commit", which is what I am after, right? – Armen Michaeli Aug 29 '13 at 10:36
  • 1
    `git reset ` to unstage the file, or `git reset .` to unstage everything and `git add -p` to selectively stage stuff. – user229044 Aug 29 '13 at 10:37
7

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
Eugene
  • 313
  • 2
  • 9
1

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
Sergey K.
  • 24,894
  • 13
  • 106
  • 174