git 如何从git存储库中删除目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6313126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove a directory from git repository?
提问by Sahat Yalkabov
I have 2 directories on my GitHub repository. I'd like to delete one of them. How could I do that without deleting and re-creating entire repository?
我的 GitHub 存储库中有 2 个目录。我想删除其中之一。我怎么能在不删除和重新创建整个存储库的情况下做到这一点?
回答by karmakaze
Remove directory from git and local
从 git 和本地删除目录
You could checkout 'master' with both directories;
您可以使用两个目录结帐“master”;
git rm -r one-of-the-directories // This deletes from filesystem
git commit . -m "Remove duplicated directory"
git push origin <your-git-branch> (typically 'master', but not always)
Remove directory from git but NOT local
从 git 但不是本地删除目录
As mentioned in the comments, what you usually want to do is remove this directory from git but not delete it entirely from the filesystem (local)
如评论中所述,您通常想要做的是从 git 中删除此目录,但不会从文件系统(本地)中完全删除它
In that case use:
在这种情况下使用:
git rm -r --cached myFolder
回答by eirenaios
To remove folder/directory only from git repository and not from the local try 3 simple commands.
要仅从 git 存储库而不是从本地删除文件夹/目录,请尝试 3 个简单的命令。
Steps to remove directory
删除目录的步骤
git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master
Steps to ignore that folder in next commits
在下一次提交中忽略该文件夹的步骤
To ignore that folder from next commits make one file in root named .gitignoreand put that folders name into it. You can put as many as you want
要从下一次提交中忽略该文件夹,请在根目录中创建一个名为.gitignore 的文件 ,并将该文件夹名称放入其中。你可以放任意多
.gitignorefile will be look like this
.gitignore文件看起来像这样
/FolderName
回答by cmcculloh
If, for some reason, what karmakaze said doesn't work, you could try deleting the directory you want using or with your file system browser (ex. In Windows File Explorer). After deleting the directory, issuing the command: git add -A
and then git commit -m 'deleting directory'
and then git push origin master
.
如果由于某种原因,karmakaze 所说的不起作用,您可以尝试删除您想要使用的目录或使用文件系统浏览器(例如在 Windows 文件资源管理器中)。删除目录后,发出命令:git add -A
然后git commit -m 'deleting directory'
然后git push origin master
。
回答by Breen ho
You can try this:
git rm -rf <directory_name>
你可以试试这个:
git rm -rf <directory_name>
It will force delete the directory.
它将强制删除目录。
回答by dbr
If you remove the files in the directory (with git rm
as the other answers explain), then the directory no longer exists as far as git is concerned. You cannot commit an empty directory, nor can you remove one.
如果您删除目录中的文件(git rm
正如其他答案所解释的那样),那么就 git 而言,该目录不再存在。你不能提交一个空目录,也不能删除一个。
This is unlike subversion where you have to explicitly svn rm emptyfolder/
, and is incidentally why the man
page for git describes itself as "the stupid content tracker"
这与 subversion 不同,你必须明确地这样做svn rm emptyfolder/
,顺便说一句,为什么man
git 页面将自己描述为“愚蠢的内容跟踪器”
An answer on "How do I add an empty directory to a git repository"links to the FAQ on this subject:
关于“如何将空目录添加到 git 存储库”的答案链接到有关此主题的常见问题解答:
Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.
Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.
You can say "
git add <dir>
" and it will add files in there.If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose; you can leave it empty, or fill in the names of files you expect to show up in the directory.
目前 git 索引(暂存区)的设计只允许列出文件,没有人有足够的能力进行更改以允许空目录,已经足够关心这种情况来纠正它。
在目录中添加文件时会自动添加目录。也就是说,目录永远不必添加到存储库中,并且不会自行跟踪。
你可以说“
git add <dir>
”,它会在那里添加文件。如果您确实需要一个目录存在于结帐中,您应该在其中创建一个文件。.gitignore 很适合这个目的;您可以将其留空,或填写您希望出现在目录中的文件名。
回答by Ridha Rezzag
for deleting Empty folders
用于删除空文件夹
i wanted to delete an empty directory(folder) i created, git can not delete it, after some research i learned Git doesn't track empty directories. If you have an empty directory in your working tree you should simply removed it with
我想删除我创建的一个空目录(文件夹),git 无法删除它,经过一些研究我了解到 Git 不跟踪空目录。如果您的工作树中有一个空目录,您应该简单地将其删除
rm -r folderName
There is no need to involve Git.
没有必要涉及 Git。
回答by Skyborg
I already had committed the folder before and want to remove the directory in the history as well.
我之前已经提交了该文件夹,并且还想删除历史记录中的目录。
I did the following:
我做了以下事情:
Add folder to .gitignore
:
将文件夹添加到.gitignore
:
echo Folder_Name/ >> .gitignore
Remove from all commits:
从所有提交中删除:
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all
remove the refs from the old commits:
从旧提交中删除引用:
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
Ensure all old refs are fully removed
确保完全删除所有旧参考
rm -Rf .git/logs .git/refs/original
Perform a garbage collection
执行垃圾收集
git gc --prune=all --aggressive
push you changes to the online repository:
将您的更改推送到在线存储库:
git push
You are done here.
你在这里完成了。
But you can to the following to push all the changes to all branches with: But be careful with this command!
但是您可以使用以下命令将所有更改推送到所有分支:但要小心此命令!
git push origin --all --force
git push origin --tags --force
After that the folder was removed from git, but was not deleted from local disk.
之后,该文件夹从 git 中删除,但未从本地磁盘中删除。
回答by anky
Go to your git Directory then type the following command: rm -rf <Directory Name>
转到您的 git 目录,然后键入以下命令: rm -rf <目录名称>
After Deleting the directory commit the changes by: git commit -m "Your Commit Message"
删除目录后通过以下方式提交更改: git commit -m "Your Commit Message"
Then Simply push the changes on remote GIT directory: git push origin <Branch name>
然后只需将更改推送到远程 GIT 目录: git push origin < Branch name>
回答by ettanany
I usually use git add --all
to remove files / folders from remote repositories
我通常用来git add --all
从远程存储库中删除文件/文件夹
rm -r folder_name
git add --all
git commit -m "some comment"
git push origin master
master
can be replaced by any other branch of the repository.
master
可以被存储库的任何其他分支替换。
回答by shrikant
You can delete the folder locally and then push, as follow:
可以在本地删除文件夹然后push,如下:
git rm -r folder_name
git commit -m "your commit"
git push origin master