英文:
git remove single line from file in git history
问题
I accidentally committed a line of code that was not meant to be part of the commit.
class Engine {
    x: X
    protected _events: Event // <-- this line was accidentally included
    y: Y
the committed version needs to instead be
class Engine {
    x: X
    y: Y
I have already made more commits afterwards, so doing git reset and manually re-staging is not tenable.
Following another post, I attempted this command
git filter-branch --tree-filter 'sed -i "/protected _events: Event/ d" src/engine/Engine.ts' -- --all
but receive the following error
WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...
fatal: bad revision '_events:'
What is going wrong?
英文:
I accidentally committed a line of code that was not meant to be part of the commit.
class Engine {
    x: X
    protected _events: Event // <-- this line was accidentally included
    y: Y
the committed version needs to instead be
class Engine {
    x: X
    y: Y
I have already made more commits afterwards, so doing git reset and manually re-staging is not tenable.
Following another post, I attempted this command
git filter-branch --tree-filter 'sed -i "/protected _events: Event/ d" src/engine/Engine.ts' -- --all
but receive the following error
WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...
fatal: bad revision '_events:'
What is going wrong?
答案1
得分: 3
filter-branch 用于在提交中从文件中删除一行?这远不止是过度杀伤力的选择(不要提起上游建议使用 git filter-repo 而不是 filter-branch... 仍然是一个过度杀伤力的选择)
只需运行 git rebase -i the-commit-i-want-to-change~(确保使用短横线!!!)。将第一个提交的操作(应该是您想要修改的提交)设置为 edit。保存并退出。Git 将停在该提交上。修改文件并将其保持为您想要的样子。
git add the-file
git commit --amend --no-edit
git rebase --continue
现在您拥有了您想要的历史记录。免责声明:我假设您正在处理一条直线分支,至少在您想要修改的提交之后。
英文:
filter-branch for removing a line from a file on a commit? That's more than an overkill (not to bring up that upstream recommends to use git filter-repo instead of filter-branch these days... still a big overkill)
Just run git rebase -i the-commit-i-want-to-change~ (make sure to use the pigtail!!!). Set the action for the first commit (which should be the one you want to modify) to edit. Save an exit. Git will stop on that commit. Modify the file and leave it the way you want it to be.
git add the-file
git commit --amend --no-edit
git rebase --continue
And now you have history the way you wanted. Disclaimer: I am assuming the branch you are working on a straight-line branch, at least past the commit you want to modify.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论