git add * does not add deleted files

2020-07-09 09:57发布

I have a local directory in which I have initialized git. I have added all files of that directory in git using:

git add *

Now if I delete a file manually from my local directory, I want to get it deleted from github too. I have tried

git add -A *

But it does not work. Everytime I have to delete it manually from github too.

标签: git github
4条回答
Evening l夕情丶
2楼-- · 2020-07-09 10:12

The problem is that the glob (*) is expanded by your shell, not git, and the shell does not know anything about the files that you have already deleted. git add -A without any more arguments would add all the files, including deleted files. git add . will also do this in the current git version. You could also use git rm --cached <file> for individual files as suggested in other answers.

It's usually easier to just use git rm to remove the files, as this will both remove the file AND stage the removal.

查看更多
劫难
3楼-- · 2020-07-09 10:23

If you want to delete a file from your index you should use

git rm filename

Documentation can be found here: https://git-scm.com/docs/git-rm

An important note for this command is:

git rm will not remove a file from just your working directory. (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.)

Let me know if it works!

查看更多
家丑人穷心不美
4楼-- · 2020-07-09 10:29

You should use the "git rm" command.
git rm /path/to/file

ex: go to your git directory and type
git rm file.txt

Then do a git commit say:
git commit -m "Removing file"

Finally push your changes to your git repository. The file will now be removed from your repository too once you push these changes.

This earlier post on stack overflow gives more information: Git: why the file deletion does not sync?

Hope this helps!

查看更多
手持菜刀,她持情操
5楼-- · 2020-07-09 10:35

The only thing you have to do is when removing files from local repository, rather than manually deleting the file from the local repository use,

git rm <file-name>

or

git rm -rf <folder-name>

After that, commit the change locally and push it. It should reflect in both local and remote repository.


Or if you have already manually deleted the files in the local repository and now if you want to remove from the remote repository as well, use --cached attribute. That is,

git rm --cached <file-name>

or

git rm -rf --cached <folder-name>

Now if you have manually deleted files and want that reflect in both local and remote git repository, use,

git add -u .

Hope this helps! :)

查看更多
登录 后发表回答