简体   繁体   English

git恢复master中已删除的文件,保持功能正常运行

[英]git revert deleted files in master, keep feature working files intact

While working in a local 'myfeature' branch, I deleted some files and committed them locally. 在本地“ myfeature”分支中工作时,我删除了一些文件并在本地提交了它们。 Along the way, those commits were then merged with remote origin/master. 在此过程中,这些提交随后与远程源/主机合并。 I needed to get those deleted files back, so I did a git pull for the latest on master and cut a 'hotfix' branch where I: 我需要找回那些删除的文件,所以我做了一个git pull以获取master上的最新消息,并在其中切出一个“ hotfix”分支:

git revert <commit id>

in reverse chronological order of those bad commits and got those deleted files back. 按照错误提交的时间顺序,将那些已删除的文件找回。 I'm basing on the git-flow branching model so correct me if I'm wrong, I should now merge the 'hotfix' into master, as well as any 'release' branches, develop, and any other 'feature' branches? 我基于git-flow分支模型,所以如果我错了,请纠正我,现在应该将“修补程序”合并到master,以及任何“发布”分支,development和其他“功能”分支中吗?

Do I need to do anything to my currently still in-progress 'myfeature' branch where I want to keep those said files deleted? 我需要对仍在进行中的“ myfeature”分支执行任何操作,以使这些文件被删除吗? Will those files stay deleted once I merge 'myfeature' or will those files resurrect when I merge? 合并“ myfeature”后,这些文件会保持删除状态,还是合并后这些文件会复活?

I'm not using git-flow, just the model. 我没有使用git-flow,只是使用了模型。

About getting back deleted files : 关于找回已删除的文件:

If your history looks like this : 如果您的历史记录如下所示:

* aa1234 last commit
* bb2345 commit
* cc3456 commit
* dd4567 deleted fileC dirD/fileD & made other changes
* ee5678 deleted filA  dirB/fileB & made other changes
* ff6789 commit

and you want to get back file A,B,C,D as they were when you deleted them, you can check them out one by one, using git checkout <hash> -- <path/to/file> : 并且您想要像删除它们一样恢复文件A,B,C,D,则可以使用git checkout <hash> -- <path/to/file>逐一git checkout <hash> -- <path/to/file>它们:

git checkout ee5678 -- fileA
git checkout ee5678 -- dirB/fileB
git checkout dd4567 -- fileC dirD/fileD

That way, you keep your other modifications safe. 这样,您就可以确保其他修改的安全。

About the flow : I didn't quite get which flow you are using, but if you create a commit whose action is to recreate the files you have deleted, then yes, merging this commit in other branches will apply the same action. 关于流程:我不太了解您正在使用的流程,但是如果您创建的提交的操作是重新创建已删除的文件,那么可以,将此提交合并到其他分支中将应用相同的操作。

If you revert commits then yes, all changes of that commit are reverted, but other than that nothing else. 如果您还原提交,那么可以,该提交的所有更改都将还原,但除此之外没有别的。 So if you bad changes are isolated within commits, you can simply revert those while leaving the good commits in place. 因此,如果您将不好的更改隔离在提交中,则只需还原这些更改,而将好的提交保留在原处。

If you however have commits where good and bad actions are mixed, then you would also lose those good actions when reverting them like that, so I'd suggest you not to do that. 但是,如果您将好的和坏的动作混在一起进行提交,那么当您那样还原它们时,您也会丢失这些好的动作,所以我建议您不要这样做。

Instead you should do partial reverts. 相反,您应该执行部分还原。 In general you have more control over the reverts, when doing them with the -n flag. 通常,使用-n标志进行还原时,您可以更好地控制它们。 That way Git won't create a reverting commit for you and you can manipulate the changes first and then commit it yourself. 这样,Git不会为您创建还原提交,您可以先操纵更改然后自己提交。 In addition this allows you to make multiple reverts at once. 另外,这允许您一次进行多个还原。 So ideally when there are multiple commits that together introduced a change you no longer want, you can revert them all in a single commit to fix it. 因此,理想情况下,当有多个提交共同带来了您不再想要的更改时,您可以在单个提交中将它们全部还原以进行修复。

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

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