Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a file which is already committed?

Tags:

git

gitignore

Previously, the following was my .gitignore file:

... config/database.yml .DS_Store 

Later I created an app_config.yml file in the config directory and committed it.

Now, I realized that I don't need the app_config.yml file in the git repository. And I modified my .gitignore file:

... config/app_config.yml config/database.yml .DS_Store 

Now, when I commit, that app_config.yml file is still in my repo. I want to remove that file from my repo. How can I do it?

like image 844
Autodidact Avatar asked Mar 09 '09 11:03

Autodidact


People also ask

How do I ignore an already tracked file in Git?

Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.

Does Gitignore need to be committed to work?

The root of a project is the folder that is the parent for all the project files and subfolders. In order for a . gitignore file to work correctly, it must be committed to GitHub before we commit (by accident) any code we don't want to push to GitHub.

What is .gitignore file for?

gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo. / will ignore directories with the name.

How do you exclude files from a commit in Visual Studio?

In Visual Studio, you can directly Click on "Git" -> "Settings" -> Then select "Git Repository settings" -> then in section "Git Files", click the Add button for Ignore file.


2 Answers

As mentionned in "ignoring doesn't remove a file "
(the following quote the great article from Nick Quaranto,
in his blog git ready "learn git one commit at a time"):

When you tell Git to ignore files, it’s going to only stop watching changes for that file, and nothing else.
This means that the history will still remember the file and have it
.

If you want to remove a file from the repository, but keep it in your working directory, simply use:

git rm --cached <file> 

However, this will still keep the file in history.

If you actually want to remove it from history, you really have two options:

  • rewrite your repository’s commits, or
  • start over.

Both options really suck, and that’s for a good reason: Git tries hard not to lose your data.
Just like with rebasing, Git forces you to think about these kinds of options since they are destructive operations.

If you did actually want to remove a file from history, git filter-branch is the hacksaw you’re looking for.
Definitely read up on its manpage before using it, since it will literally rewrite your project’s commits. This actually is a great tool for some actions, and can do all sorts of stuff like totally removing an author’s commits to moving the project root folder around. The command to remove a file from all revisions is:

git filter-branch --index-filter 'git rm --cached <file>' HEAD 

This action can definitely be useful when you need to blow out sensitive or confidential information that may have been placed in your repository

like image 52
VonC Avatar answered Sep 19 '22 15:09

VonC


that doesn't help when you want to start ignoring a file that is committed and you want to keep it in the project... since git rm --cached would delete it from git repo...

I found this How do I stop Git from tracking any changes to a file from this commit forward? which is what I was looking for..

hopefully it's useful for other people as well...

like image 31
awdk Avatar answered Sep 19 '22 15:09

awdk