"file1.txt"
Gitリポジトリに名前の付いたファイルを追加しました。その後、それをコミットし、andというディレクトリをいくつか追加dir1
しdir2
て、Gitリポジトリにコミットしました。
現在のリポジトリには、、、"file1.txt"
およびdir1
がありdir2
ます。や"file1.txt"
など、他の人に影響を与えずに削除するにはどうすればよいですか?dir1
dir2
を使用しgit rm
ます。
Gitリポジトリとファイルシステムからファイルを削除する場合は、次を使用します。
git rm file1.txt
git commit -m "remove file1.txt"
ただし、ファイルをGitリポジトリからのみ削除し、ファイルシステムからは削除しない場合は、次を使用します。
git rm --cached file1.txt
git commit -m "remove file1.txt"
そして、変更をリモートリポジトリにプッシュする
git push origin branch_name
git rm file.txt
リポジトリからファイルを削除しますが、ローカルファイルシステムからも削除します。
リポジトリからファイルを削除し、ローカルファイルシステムからファイルを削除しないようにするには、次を使用します。
git rm --cached file.txt
以下の正確な状況は、gitを使用してビジネスのWebサイトのバージョン管理を維持しているが、「mickey」ディレクトリはCAD開発者とプライベートコンテンツを共有するためのtmpフォルダーでした。彼が巨大なファイルを必要としたとき、私はリンクされていないプライベートディレクトリを作成し、ブラウザ経由でフェッチできるようにファイルをftpで転送しました。これをしたことを忘れて、後でgit add -A
Webサイトのベースディレクトリからを実行しました。続いて、git status
コミットが必要な新しいファイルを示しました。今、私はそれらをgitの追跡とバージョン管理から削除する必要がありました...
以下の出力例は、意図せずに.003
ファイルを削除したときに発生したものです。ありがたいことに、ローカルコピーに何が起こったのかは気にしませんが.003
、現在変更されている他のファイルのいくつかは、Webサイトに加えたばかりの更新であり、ローカルファイルシステムで削除されたのは素晴らしいことです。「ローカルファイルシステム」=ライブWebサイト(優れた方法ではありませんが、現実です)。
[~/www]$ git rm shop/mickey/mtt_flange_SCN.7z.003
error: 'shop/mickey/mtt_flange_SCN.7z.003' has local modifications
(use --cached to keep the file, or -f to force removal)
[~/www]$ git rm -f shop/mickey/mtt_flange_SCN.7z.003
rm 'shop/mickey/mtt_flange_SCN.7z.003'
[~/www]$
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: shop/mickey/mtt_flange_SCN.7z.001
# modified: shop/mickey/mtt_flange_SCN.7z.002
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$
[~/www]$
[~/www]$ git rm --cached shop/mickey/mtt_flange_SCN.7z.002
rm 'shop/mickey/mtt_flange_SCN.7z.002'
[~/www]$ ls shop/mickey/mtt_flange_S*
shop/mickey/mtt_flange_SCN.7z.001 shop/mickey/mtt_flange_SCN.7z.002
[~/www]$
[~/www]$
[~/www]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: shop/mickey/mtt_flange_SCN.7z.002
# deleted: shop/mickey/mtt_flange_SCN.7z.003
#
# Changed but not updated:
# modified: shop/mickey/mtt_flange_SCN.7z.001
[~/www]$
更新:この回答はトラフィックを獲得しているので、他のGit回答がいくつかの優れたリソースを共有していることを言及したいと思いました。このページには、Gitをわかりやすく説明するのに役立つグラフィックがあります。「ProGit」の本はオンラインで、私を大いに助けてくれます。
まず、特に複数のファイルにを使用している場合はgit rm
、ワイルドカードがコマンドではなくシェルによって解決されることを考慮してgit
ください。
git rm -- *.anExtension
git commit -m "remove multiple files"
ただし、ファイルがすでにGitHubにある場合は、(2013年7月以降) WebGUIから直接ファイルを削除できます。
リポジトリ内のファイルを表示し、上部にあるゴミ箱アイコンをクリックして、他のWebベースの編集と同じように削除をコミットするだけです。
次にgit pull
、ローカルリポジトリで「」を実行すると、ファイルもローカルで削除されます。
この答えをgitリポジトリからファイルを削除する(回りくどい)方法にするのはどれですか?
(GitHub上のファイルが「gitリポジトリ」にあることは言うまでもありません)
(コミットはそのファイルの削除を反映します):
そして、ちょうどそのように、それはなくなっています。
これらの機能のヘルプについては、ファイルの作成、移動、名前の変更、および削除に関するヘルプ記事を必ずお読みください。
注:これはバージョン管理システムであるため、後でファイルを回復する必要がある場合は、Gitが常に責任を負います。
最後の文は、削除されたファイルがまだ履歴の一部であり、簡単に復元できることを意味します(ただし、GitHub Webインターフェイスからはまだ復元できません)。
「 Gitリポジトリで削除されたファイルを復元する」を参照してください。
これは私のために働いた唯一のオプションです。
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch *.sql'
注:*。sqlをファイル名またはファイルタイプに置き換えてください。これはすべてのコミットを通過し、このファイルタイプを削除するため、非常に注意してください。
編集:注意してください-このコマンドの後、プッシュまたはプルすることはできません-'無関係な履歴'の拒否が表示されます'git push --force -uoriginmaster'を使用してプッシュまたはプルできます
さらに、削除するフォルダーであり、後続の子フォルダーまたはファイルである場合は、次を使用します。
git rm -r foldername
より一般的にgit help
は、少なくとも次のような簡単な質問に役立ちます。
zhasper@berens:/media/Kindle/documents$ git help
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]
The most commonly used git commands are:
add Add file contents to the index
:
rm Remove files from the working tree and from the index
リポジトリからファイルを削除したいが、ファイルシステムに残したい場合(追跡されません):
bykov@gitserver:~/temp> git rm --cached file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"
リポジトリとファイルシステムからファイルを削除する場合は、次の2つのオプションがあります。
ファイルに変更がインデックスにステージングされていない場合:
bykov@gitserver:~/temp> git rm file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt"
ファイルに変更がインデックスにステージングされている場合:
bykov@gitserver:~/temp> git rm -f file1.txt
bykov@gitserver:~/temp> git commit -m "remove file1.txt"
git rm
今後はこのブランチのファイルのみを削除しますが、履歴に残り、gitはそれを記憶します。
git filter-branch
他の人がここで言及しているように、それを行う正しい方法はを使用することです。ブランチの履歴内のすべてのコミットを書き換えて、そのファイルを削除します。
しかし、それを行った後でも、reflog、リモート、タグなどに参照がある可能性があるため、gitはそれを記憶できます。
ワンステップで完全に消し去りたい場合は、git forget-blob
それは簡単です、ただやってくださいgit forget-blob file1.txt
。
これにより、すべての参照が削除され、git filter-branch
実行され、最後にgitガベージコレクターが実行git gc
されて、リポジトリ内のこのファイルが完全に削除されます。
rmコマンドを使用してローカルフォルダーからファイルを削除してから、変更をリモートサーバーにプッシュする場合の別の方法。
rm file1.txt
git commit -a -m "Deleting files"
git push origin master
According to the documentation.
git rm --cached file1.txt
When it comes to sensitive data—better not say that you removed the file but rather just include it in the last known commit:
0. Amend last commit
git commit --amend -CHEAD
If you want to delete the file from all git history, according to the documentation you should do the following:
1. Remove it from your local history
git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \ --prune-empty --tag-name-filter cat -- --all
# Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path to the file you want to remove, not just its filename
echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
git add .gitignore
git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
3. If you need to remove from the remote
git push origin --force --all
4. If you also need to remove it from tag releases:
git push origin --force --tags
The answer by Greg Hewgill, that was edited by Johannchopin helped me, as I did not care about removing the file from the history completely. In my case, it was a directory, so the only change I did was using:
git rm -r --cached myDirectoryName
instead of "git rm --cached file1.txt" ..followed by:
git commit -m "deleted myDirectoryName from git"
git push origin branch_name
Thanks Greg Hewgill and Johannchopin!
私の場合、数回のコミット後にgithubでファイルを削除しようとしましたが、コンピューターに保存しました
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch file_name_with_path' HEAD
git push --force -u origin master
その後、このファイルは無視されました
特定のファイルを削除するには
gitrmファイル名
追跡されていないすべてのファイルをディレクトリから1回のショットで再帰的にクリーンアップするには
git clean -fdx
GitHub for Windowsアプリケーションを使用している場合は、次の5つの簡単な手順でファイルを削除できます。
まず、ローカルリポジトリからファイルを削除します。
gitrm-rファイル名
または、ローカルリポジトリからのみ、ファイルシステムからファイルを削除します
git rm --cachedFile-Name
次に、変更をローカルリポジトリにコミットします。
git commit -m "unwanted files or some inline comments"
最後に、ローカルの変更を更新/リモートリポジトリにプッシュします。
git push
go to your project dir and type:
git filter-branch --tree-filter 'rm -f <deleted-file>' HEAD
after that push --force for delete file from all commits.
git push origin --force --all
For the case where git rm
doesn't suffice and the file needs to be removed from history: As the git filter-branch
manual page now itself suggests using git-filter-repo, and I had to do this today, here's an example using that tool. It uses the example repo https://example/eguser/eg.git
Clone the repository into a new directory git clone https://example/eguser/eg.git
Keep everything except the unwanted file. git-filter-repo --path file1.txt --invert-paths
Add the remote repository origin back :
git remote add origin https://example/eguser/eg.git
.
The git-filter-repo
tool removes remote remote info by design and suggests a new remote repo (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.
When happy with the contents of local, replace remote with it.
git push --force -u origin master
. Forcing is required due to the changed history.
Also note the useful --dry-run
option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.
提案されたオプションをたくさん試しましたが、どれも機能していないようでした(さまざまな問題をリストしません)。私がやったことは、うまくいきましたが、(私にとっては)シンプルで直感的でした:
リポジトリからファイルを削除した後、BFG Repo-Cleanergit rm
を使用して、リポジトリ履歴からファイルを完全かつ簡単に消去できます。
Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.
'変更されたファイル'リストを汚染したくない誤ってリポジトリに入れられたobjファイルとbinファイルがあります
彼らがリモートに行ったことに気づいた後、これをに追加して無視しました.gitignore
/*/obj
/*/bin
問題は、それらがすでにリモートにあり、変更されると、変更されたものとしてポップアップし、変更されたファイルリストを汚染することです。
それらの表示を停止するには、リモートリポジトリからフォルダ全体を削除する必要があります。
コマンドプロンプトの場合:
C:\repos\MyRepo
)C:\repos\MyRepo\SSIS
)git rm -r -f obj
git commit -m "remove obj folder"
13個のファイルが315222個の削除を変更したという警告メッセージが表示されました
次に、CMD行を検索する必要がなかったため、Visual Sstudioにアクセスし、同期を実行してリモートに適用しました。
If you need to remove files from a determined extension (for example, compiled files) you could do the following to remove them all at once:
git remove -f *.pyc
ローカルリポジトリではなくgitリポジトリにファイルする場合は、Webインターフェイスを介してgitリポジトリでファイルを開き、インターフェイスの右隅にある[削除]ボタンを見つけます。 ここをクリックして、インターフェースの削除オプションを表示します