Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore changes to a file when committing with Git?

Tags:

git

branch

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.

like image 725
amn Avatar asked Aug 29 '13 10:08

amn


People also ask

How do I ignore a committed file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I exclude changes in a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.

How do I ignore files while pushing to git repository?

Excluding local files without creating a .Use your favorite text editor to open the file called . git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.

How do I ignore unwanted changes in git?

For files that aren't tracked by Git, you can use a . gitignore or exclude file. For files that are tracked by Git, you can tell Git to stop tracking them and to ignore changes.


1 Answers

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
like image 118
simont Avatar answered Oct 02 '22 18:10

simont