Pepe
Pepe

Reputation: 213

Remove file from repository under git

I want to remove several files from git repository. My repository has several branches and I do following:

git rm --cached filename 
git commit -am "deleted filename"
git checkout anotherbranch

I am getting:

error: The following untracked working tree files would be overwritten 
by checkout:
    output/filename

Please move or remove them before you can switch branches.
Aborting

So question is how to remove the file (accidentally added) from repo? I do NOT want to track the filename by git I plan to add this filename to .gitignore.

Best regards,

Peter

Upvotes: 1

Views: 3998

Answers (4)

Sebastian Łaskawiec
Sebastian Łaskawiec

Reputation: 2737

In my opinion my opinion the best way is to amend your commit:

git commit --amend

However, if your commit has been already pushed into the remote repository, I would suggest creating another commit and removing those file. I think this is the easiest way to fix this.

Upvotes: 0

Harshith J.V.
Harshith J.V.

Reputation: 897

To remove file permanently from your repo you have to pass -f option:

git rm -f filename

Your problem of untracked file while switching the branch has been discussed over here: The following untracked working tree files would be overwritten by checkout

Upvotes: 0

Kuba
Kuba

Reputation: 3076

When you do

git rm --cached

you delete files from git, but they are left on your disk, so after you commit this change, your files are still on the disk, but not in git. Thus git recognizes them as untracked files.

You want to switch to the branch that still have those files, and git won't let you switch to other branch if the checkout to this branch would affect your local changes (the utracked files, or the modified files)

Upvotes: 0

triad
triad

Reputation: 21567

I normally delete those files normally (not through git commands). Then after I have deleted all the files, I do:

git add . -A

git commit -a -m "remove files"

Upvotes: 2

Related Questions