0

I save my work on GitHub. Normally, I use the following commands:

git add *  # To save all files
git commit -m "a commit"
git push

It works perfectly except for one case: Let's say my repository has two files, file1.c and file2.c.

I use above commands, everything is saved. Then, I delete file2.c, only file1.c is left.

I run above commands again, and there is no error, but when I clone the repository, both files will be there.

How do I remove these files from my repository?

elaforma
  • 652
  • 1
  • 9
  • 29

4 Answers4

2

Instead of git add * (which is expanded by the shell) try:

rm file2
rm file3
...
git add .
git commit -m "record modified files, remove files"
git push

That should take into adccount addition and deletion of files, since Git 2.0 (where git add . is like git add -A)

That would record your 20 deleted files, and allow you to push that.

git add . would detect automatically the files modified, added or removed.
As opposed to git add *, which is managed by the shell, expanding the '*' to the files seen by the shell (ie, by definition, not the deleted files!)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • And do you know a way to not use the rm file2 ... If I don't know the name of all deleted files or don't want to track them ? –  Feb 19 '17 at 10:39
  • @f42 you don't have to know the name of the deleted file: `git add .` will detect them for you :) – VonC Feb 19 '17 at 10:40
  • @f42 I have edited my answer to explain why the other answers were missing the point. Your problem comes from using '*', as I detail in my edit. – VonC Feb 19 '17 at 10:44
  • The best way to do it, I didn't see your answer first. I tried and it works. Thanks a lot –  Feb 19 '17 at 10:44
0

In order to delete a file with git, use the following command:

git rm file2.c

This will delete the file both on the file system and in the repository. You can then commit and push the changes as usual:

git commit -m "Removed file2.c"
git push

If you have deleted a lot of files without git rm, you can remove all files from the repository, then add them again:

git rm --cached *
git add *

The --cached deletes files only from the repository, they are kept on the file system so that you can add them back with git add.

elaforma
  • 652
  • 1
  • 9
  • 29
  • Thank you for the command, but how to to if I have 20 files to delete ? Have you a way to just save the work and if a file is missing automatically delete it from the repository ? –  Feb 19 '17 at 10:27
0
git rm file1.c
git add *
git commit -m "message"
git push
Specas
  • 98
  • 1
  • 8
0

I think you can use git rm to delete file or folder like you use rm in linux ~

for example, run

  • git rm path/file2.c
  • git status

you will see file2.c is deleted in you repository

then commit it to your remote repository

  • git commit -m "del: delete the file file2.c"
TIGERB
  • 51
  • 5