简体   繁体   English

如何从旧提交中恢复某些行?

[英]How can I revert certain lines from old commit?

Let's say that I have 2 files:假设我有 2 个文件:

  • App.js
  • Home.js

They are already tracked and exist in first commit.它们已经被跟踪并存在于第一次提交中。 And I add a new feature to App.js on second commit which is maybe a new function and have more commits afterwards and let's say I'm in 10th commit now.我在第二次提交时向App.js添加了一个新功能,这可能是一个新功能,之后有更多提交,假设我现在处于第 10 次提交。

What I would like to do is just remove that function in a new commit without rebasing only in App.js ;我想做的只是在新提交中删除该功能,而无需仅在App.js中进行变基; what should I do in order to achieve this?我应该怎么做才能实现这一目标?

Edit编辑

Function is a simple example it's pretty easy to delete those lines and have a new commit.函数是一个简单的例子,很容易删除这些行并进行新的提交。 What I want is something more smart and does not need a manual effort.我想要的是更聪明的东西,不需要人工。 I use VS Code and the GitLens extension;我使用 VS Code 和 GitLens 扩展; it would be great if there is feature on these tools.如果这些工具有功能那就太好了。

One way you could achieve this in the command line:您可以在命令行中实现此目的的一种方法:

  1. git revert <commit_number_with_lines_you_want_to_revert>
  2. git reset --soft HEAD~1 to take the reversion commit you just created out of history but keep the changes in your local filesystem git reset --soft HEAD~1将您刚刚创建的还原提交从历史记录中删除,但将更改保留在本地文件系统中
  3. git reset to unstage the reversion changes git reset取消暂存还原更改
  4. Use git add -i to do an interactive staging使用git add -i进行交互式暂存
    • Choose to patch on the files which have the lines you wish to revert选择修补包含您希望还原的行的文件
    • Walk through the patching dialog, selecting "yes" for those you wish to revert and skipping all others浏览修补对话框,为您希望恢复的那些选择“是”并跳过所有其他
    • If a patch is too large (meaning it contains both lines you want to revert and lines you do not wish to revert) you can "split" or "edit manually" (this is rarely needed, in my experience)如果补丁太大(意味着它包含您想要恢复的行和您不希望恢复的行),您可以“拆分”或“手动编辑”(根据我的经验,这很少需要)
    • When you are done adding the lines you wish to revert, quit the interactive session添加完要还原的行后,退出交互式会话
  5. Make a new commit with your staged reversion lines使用分阶段的还原行进行新的提交
  6. git reset --hard to undo all other local changes git reset --hard撤消所有其他本地更改

This looks like a lot of steps, but once you get used to the interactive session interface it tends to go pretty smoothly.这看起来像很多步骤,但是一旦您习惯了交互式会话界面,它往往会非常顺利。 While it is not as friendly a GUI as GitLens, since it walks you through each local change and allows you to add or skip changes with a single keystroke I would say it counts for relatively low "manual effort".虽然它不像 GitLens 那样友好的 GUI,但因为它会引导您完成每个本地更改并允许您通过单次击键添加或跳过更改,我会说它的“手动工作量”相对较低。

This is not the only approach;这不是唯一的方法; you could probably generate and manually edit a patch from a diff, and I'm sure other users might think up other recipes to accomplish this that may be more elegant or faster.您可能可以从 diff 生成并手动编辑补丁,我相信其他用户可能会想出其他方法来完成此操作,这些方法可能更优雅或更快。 But this should work for your needs.但这应该可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM