簡體   English   中英

從 Git 存儲庫中刪除推送的文件

[英]Remove pushed files from Git repo

我已經將兩個目錄推送到我的 Git Bitbucket 存儲庫,我現在希望刪除/刪除它們。

我應該如何做出這些改變? 我已經嘗試過:

git rm -rf --cached files 
git commit -a -m "Deleted files" 
git push origin master

我在這里偵察的是,它只從我的工作目錄中刪除了文件,但保留了它在 Bitbucket 中的原樣。

您可以通過以下命令還原用於推送這些目錄的提交

git reset --soft HEAD^

假設不需要的提交位於 HEAD 的頂部。

如果它在一些提交之前撒謊,試試這個

git reset --soft HEAD~3

這會還原 HEAD 中倒數第四次提交指定的更改。 (您需要根據不需要的提交位置更改數字“3”。)

並檢查git status 您應該能夠看到那些不需要的目錄作為您的本地更改,尚未提交。

謝謝!!

您可以通過向其推送一個空的本地分支來刪除遠程分支中的所有文件 -

假設你想刪除origin的分支trial ,那么你會做git push origin:trial

確保您確實刪除了文件

rm -rf directory-to-remove/

然后從 git 中刪除:

git rm -r --cached directory-to-remove/

最后提交並推送:

git commit -a -m "Deleted files"
git push origin master

假設您當前的分支是 master。

不久前我遇到了類似的問題,我使用交互式變基解決了它。

您要做的是在要更改的提交之前找到提交的提交哈希。 假設哈希是376c762c1b28f927595010e98e4ee82d6bc63de3

# first git checkout the correct branch
git rebase -i 376c762c1b28f927595010e98e4ee82d6bc63de3

然后,您將獲得提交哈希提交后所有提交的列表,以及提交消息的一部分。

pick 376c762 Commit with wrong file
pick e145ef2 Fourth before last commit
pick d969e5b Third before last commit
pick 7b92c09 Second before last commit
pick db1dea3 First before last commit
pick ff120d6 Most recent commit message

# Rebase 621d70a..ff120d6 onto 621d70a (6 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
...

您在這里可以做的是將提交之前的單詞更改為edit而不是pick 在我們的例子中:

edit 376c762 Commit with wrong file

然后保存文件並退出,如果您使用的是 nano, Ctr + XYEnter

然后,您將在提交后立即返回命令行。 您可以執行以下操作來撤消之前的提交。 這些文件和您將擁有未提交的更改。 提示將顯示您當前所在提交的提交哈希的前 7 個字符。

git restore HEAD^

然后您可以從提交中刪除/添加/更改文件,添加並提交更改。 要繼續變基,您可以執行以下操作:

get rebase --continue 

如果在以后的提交中更改了相同的文件,您可能會遇到沖突,您必須先修復並提交--continue 變基完成后,您將

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM