298

I have a git repository hosted on Github. After committing many files, I am realizing that I need to create .gitignore and exclude .exe, .obj files.

However, will it automatically remove these committed files from the repository? Is there any way to force that?

4
  • 16
    Future googlers may find this extremely useful. Succinct and to the point: git-tower.com/learn/git/faq/ignore-tracked-files-in-git Commented Feb 20, 2018 at 22:51
  • 4
    Does this answer your question? How to make Git "forget" about a file that was tracked but is now in .gitignore?
    – LightCC
    Commented Jul 27, 2020 at 21:33
  • The correct answer is here. If you git rm --cached (or just delete the offending files and commit), it can delete those files on checkout. Using git update-index --skip-worktree <files> will remove them from being tracked directly.
    – LightCC
    Commented Jul 27, 2020 at 21:35
  • I've just got it on GitHub Desktop by temporary moving the wrong-commited folders or files outside the local rep, modify correctly the .gitignore, Ammend commits, bring back inside the rep to its original location the folder and files moved, and push the commit. Commented May 18, 2023 at 9:26

6 Answers 6

418

No you cannot force a file that is already committed in the repo to be removed just because it is added to the .gitignore

You have to git rm --cached to remove the files that you don't want in the repo. ( --cached since you probably want to keep the local copy but remove from the repo. ) So if you want to remove all the exe's from your repo do

git rm --cached /\*.exe

(Note that the asterisk * is quoted from the shell - this lets git, and not the shell, expand the pathnames of files and subdirectories)

6
  • 7
    @Madhur Ahuja Actually if you do git rm /\*.exe it will delete all exe's in your repo
    – manojlds
    Commented Jun 30, 2011 at 13:51
  • 7
    What are the /\'s for? Commented May 9, 2014 at 13:55
  • 7
    @Costa they're there to escape the asterisk, to prevent the shell from interpreting it, as it is intended for git.
    – Lambart
    Commented Sep 14, 2016 at 19:24
  • 6
    Warning: The files are kept in local, it's true... But other users running a git pull will see their files deleted.
    – Byscripts
    Commented Oct 10, 2019 at 12:55
  • Undo last commit is quite helpful in VS code, just wanted to mention that Commented Nov 2, 2021 at 15:42
100

If you have not pushed the changes already:

git rm -r --cached .
git add .
git commit -m 'clear git cache'
git push
0
64

However, will it automatically remove these committed files from the repository?

No. Even with an existing .gitignore you are able to stage "ignored" files with the -f (force) flag. If they files are already commited, they don't get removed automatically.

git rm --cached path/to/ignored.exe
2
  • Does that mean i have remove individually each .exe Commented Jun 30, 2011 at 13:37
  • You are probably able to batch this, but I don't know how this works under windows
    – KingCrunch
    Commented Jun 30, 2011 at 13:40
26

I had to remove .idea and target folders and after reading all comments this worked for me:

git rm -r .idea
git rm -r target
git commit -m 'removed .idea folder'

and then push to master

1
  • 1
    .idea was my problem I guess it was yours too :) Commented Jan 5, 2020 at 8:48
5

However, will it automatically remove these committed files from the repository?

No.

The 'best' recipe to do this is using git filter-branch as written about here:

The man page for git-filter-branch contains comprehensive examples.

Note You'll be re-writing history. If you had published any revisions containing the accidentally added files, this could create trouble for users of those public branches. Inform them, or perhaps think about how badly you need to remove the files.

Note In the presence of tags, always use the --tag-name-filter cat option to git filter-branch. It never hurts and will save you the head-ache when you realize later taht you needed it

2
  • I ran this command it showed Rewrite 57c1f1f04a3ed01f50c3260714cfc82c973ac816 (3/3) WARNING: Ref 'refs/heads/master' is unchanged and nothing happened Commented Jun 30, 2011 at 13:36
  • It is a warning, and most likely means that the master branch did not (yet) contain any revisions with the files to be removed. You can easily check that. If there is a problem, please update the question. PS: use -- --all to rewrite all (local) branches at once
    – sehe
    Commented Jun 30, 2011 at 13:46
2

Even after you delete the file(s) and then commit, you will still have those files in history. To delete those, consider using BFG Repo-Cleaner. It is an alternative to git-filter-branch.

Not the answer you're looking for? Browse other questions tagged or ask your own question.