从Git历史中删除文件中的单行。

huangapple go评论48阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月17日 16:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定