【问题标题】:Remove folder including content from a pull request in Git从 Git 中的拉取请求中删除包含内容的文件夹
【发布时间】:2019-08-03 20:42:54
【问题描述】:

我提出了拉取请求,现在我必须在合并之前删除 .vscode 文件夹。

如何从我的分支中删除该文件夹,然后再次推送没有该文件夹的版本?

.vscode 文件夹在我的 .gitignore 中,但经常被忽略。我必须手动删除它。

【问题讨论】:

    标签: git github version-control versioning pull-request


    【解决方案1】:

    (假设您是功能分支的“所有者”,就像在许多工作流程中一样。)

    # start from your feature branch
    git checkout <feature-branch>
    
    # undo last commit but keep changes in the working tree (and index)
    git reset --soft HEAD^
    
    # get your unwanted folder out of the index
    git reset HEAD -- path/to/folder
    
    # redo your commit, this time without the folder
    git commit -m "Useful message"
    
    # push to the remote to replace the old ref, thus needing --force
    git push -f origin HEAD
    

    此时,远程只需要刷新页面(使用新的分支引用更新您的拉取请求),您将被设置为合并您的分支,这次没有“坏”文件夹。

    【讨论】:

    • @Codehan25 是的,这就是“强制”推送的原因。我假设你确实已经推送了。
    【解决方案2】:

    你可以手动删除文件夹,git commit --amend 到你现有的提交,git push -f orign YOUR_BRANCH

    仅供参考,请确保 .vscode/ 在您的 .gitignore 中,需要以 / 结尾。

    【讨论】:

    • 文档说“如果提交只存在于您的本地存储库中并且尚未推送到 GitHub,您可以使用 git commit --amend 命令修改提交消息。在命令行上,导航到包含您要修改的提交的存储库。在您的文本编辑器中,编辑提交消息并保存提交。 - 所以我的提交也在远程仓库中。 .vscode 像 .vscode/ 和 /.vscode/ 一样在我的 .gitignore 中,但它会被忽略 :(
    • 由于你的提交也在远程仓库上,这就是为什么我们需要 git push -f origin YOUR_BRANCH 来强制推送,这样它就会覆盖前一个。关于忽略文件,不妨试试github.com/github/gitignore/blob/master/Global/…
    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 2021-08-16
    • 2017-01-20
    • 2021-07-27
    • 1970-01-01
    • 2014-05-11
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多