How to remove a file from Git, but keep it locally?

Asked

Viewed 150 times

5

When I have a file on my local computer that I would like not to be uploaded to the repository on Github, I simply create a file .gitignore and add the path file. It so happens that I would like to remove a file from the repository on Github, but keep it on my local computer.

In other words, I would like the file to be ignored after it has been part of commits previous.

What I tried to do was:

  1. Delete the file in the Github repository;
  2. Add the file path to .gitignore on my local computer;
  3. Make a push.

Well, that didn’t work, since the push failed and asked me to make a pull before. Well, do the pull updates my local directory and removes the file I’d like to keep. There’s a way to discontinue a file in a Git repository?

1 answer

7


You don’t even have to go through the Github interface. You can do everything locally. Basically, you use the command git rm:

The problem is that by using the git rm "alone, "the file will be removed from the Git index and from the working tree. The problem is that we just want to remove the file from the Git index by keeping it locally.

For this, the documentation suggests the use of flag --cached:

Use this option to unstage and remove paths only from the index. Working Tree files, whether modified or not, will be left alone.

So what you need is:

git rm --cached filename.ext

Once you’ve done this, you can even add the file name to .gitignore, to prevent future additions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.