简体   繁体   English

如何从 Git 存储库中删除目录?

[英]How do I remove a directory from a Git repository?

How can I delete a single directory containing files from a Git repository?如何从 Git 存储库中删除包含文件的单个目录?

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

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 folder (main project directory where the git is initialized) named .gitignore and put that folder name into it.要从下一次提交中忽略该文件夹,请在根文件夹(初始化 git 的主项目目录)中创建一个名为.gitignore的文件并将该文件夹名称放入其中。 You can ignore as many files/folders as you want您可以根据需要忽略任意数量的文件/文件夹

.gitignore file will look like this .gitignore文件看起来像这样

/FolderName

删除目录

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).如果由于某种原因,karmakaze 所说的不起作用,您可以尝试使用文件系统浏览器删除您想要使用的目录(例如在 Windows 文件资源管理器中)。 After deleting the directory, issuing the command:删除目录后,发出命令:
git add -A
and then接着
git commit -m 'deleting directory'
and then接着
git push origin master . git push origin master

You can try this: git rm -rf <directory_name>你可以试试这个: git rm -rf <directory_name>

It will force delete the directory.它将强制删除目录。

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.如果删除目录中的文件(使用git rm作为其他答案的解释),则就 git 而言,该目录不再存在。 You cannot commit an empty directory, nor can you remove one.您不能提交一个空目录,也不能删除一个。

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"这与颠覆不同,您必须明确svn rm emptyfolder/ ,顺便说一下,为什么 git 的man页将自己描述为“愚蠢的内容跟踪器”

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.目前 git 索引(暂存区)的设计只允许列出文件,并且没有足够能力进行更改以允许空目录的人足够关心这种情况来补救它。

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.你可以说“ git add <dir> ”它会在那里添加文件。

If you really need a directory to exist in checkouts you should create a file in it.如果您确实需要一个目录存在于结帐中,您应该在其中创建一个文件。 .gitignore works well for this purpose; .gitignore 很适合这个目的; you can leave it empty, or fill in the names of files you expect to show up in the directory.您可以将其留空,或填写您希望在目录中显示的文件的名称。

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:从旧提交中删除 refs:

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

Ensure all old refs are fully removed确保完全删除所有旧 refs

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 中删除,但并未从本地磁盘中删除。

Go to your git Directory then type the following command: rm -rf < Directory Name > Go 到您的 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 >

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.我想删除我创建的空目录(文件夹),git 无法删除它,经过一些研究我了解到 Git 不跟踪空目录。 If you have an empty directory in your working tree you should simply removed it with如果你的工作树中有一个空目录,你应该简单地删除它

rm -r folderName

There is no need to involve Git.无需涉及 Git。

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

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可以被存储库的任何其他分支替换。

You can use Attlasian Source Tree (Windows) ( https://www.atlassian.com/software/sourcetree/overview ).您可以使用 Atlassian 源代码树 (Windows) ( https://www.atlassian.com/software/sourcetree/overview )。 Just select files from tree and push button "Remove" at the top.只需 select 文件从树中,然后按顶部的“删除”按钮。 Files will be deleted from local repository and local git database.文件将从本地存储库和本地 git 数据库中删除。 Then Commit, then push.然后提交,然后推送。

If you are deleting directory from GitHub web application, you will find a delete option.如果您要从 GitHub web 应用程序中删除目录,您会发现一个删除选项。 However, after that it will give you list of files being deleted, with commit option at the bottom.但是,之后它将为您提供要删除的文件列表,底部有提交选项。 Do remember to commit it.请记住提交它。 Otherwise, directory will not be deleted.否则,目录不会被删除。

Reference - https://docs.github.com/en/repositories/working-with-files/managing-files/deleting-files-in-a-repository参考 - https://docs.github.com/en/repositories/working-with-files/managing-files/deleting-files-in-a-repository

One of my colleague suggested BFG Repo-Cleaner which I think powerful.我的一位同事建议我认为强大的 BFG Repo-Cleaner It is not only delete unwanted data but also clean your repository from any related commit information.它不仅可以删除不需要的数据,还可以从任何相关的提交信息中清除您的存储库。

First git command need to know who you are before deleting anything首先 git 命令在删除任何东西之前需要知道你是谁

  1. git init git 初始化
  2. git config user.name "someone" git 配置用户名“某人”
  3. git config user.email "someone@someplace.com" git 配置用户。email "someone@someplace.com"
  4. git rm -r git rm -r
  5. git commit -m "deleting dir" git commit -m "删除目录"
  6. git push origin master git 推源主机

To add new directory:添加新目录:

mkdir <YOUR-DIRECTORY>

But now Git is not aware by this new directory, because Git keep tracks of file not directories DIRECTORY但是现在 Git 不知道这个新目录,因为 Git 跟踪文件而不是目录

git status

Git won't be aware with the change we've made, so we add hidden .keep file to make Git aware by this new change. Git 不会知道我们所做的更改,因此我们添加隐藏.keep文件以使 Git 可以通过此新更改了解。

touch /YOUR-directory/.keep

Now, if you hit git status Git will be aware with the changes.现在,如果您点击git status Git 将会知道这些变化。

And If you want to delete the directory, you should use this command.如果要删除目录,则应使用此命令。

rm -r <YOUR-DIRECTORY>

And If you checked by using git status , you will see the directory has been removed.如果您使用git status检查,您将看到该目录已被删除。

A combination of the 2 answers worked for me 2个答案的组合对我有用

git rm -r one-of-the-directories
git commit . -m "Remove duplicated directory"
git push

if it still shows some warning, remove the files manually如果仍然显示一些警告,请手动删除文件

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD
git push

try rm -r -f <folder name> or rm -r --force <folder name> - forcefully delete a folder尝试rm -r -f <folder name>rm -r --force <folder name> - 强制删除文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从主 git 存储库中删除未跟踪的目录树(子模块/子存储库)? - How do I remove untracked directory tree (submodule / sub-repository) from main git repository? 如何从 git 存储库中删除作者? - How do I remove an author from a git repository? 如何从 git gui 中删除存储库? - How do I remove a repository from git gui? 如何从git存储库中删除有冲突的引用? - How do I remove conflicting reference from git repository? 如何从 git 存储库中删除旧历史记录? - How do I remove the old history from a git repository? 如何从本地 git 存储库和本地文件系统中删除文件,而不是从远程存储库中删除文件? - How do I remove a file from local git repository and local file system but not from remote repository? 如何从目录中删除git存储库而不删除其中的任何内容? - How to remove a git repository from a directory without deleting anything from it? 我克隆了一个git存储库。 如何从中拉入工作目录? - I cloned a git repository. How do I pull from it into a working directory? 如何将空目录添加到 Git 存储库? - How do I add an empty directory to a Git repository? 如何从 git 存储库克隆、获取或稀疏检出单个目录或目录列表? - How do I clone, fetch or sparse checkout a single directory or a list of directories from git repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM