简体   繁体   English

GIT:回到旧提交并与master合并

[英]GIT: move back to an old commit and merge that with master

Given a project with 2 files: fileA and fileB . 给定一个包含2个文件的项目: fileAfileB Both were once added to a git-repository. 两者都被添加到git-repository中。 Then I added fileA to .gitignore (and flushed the git-cache), so that in the current master only fileB is present. 然后我将fileA添加到.gitignore (并刷新了git-cache),以便在当前主服务器中只存在fileB I would like to clone this repository to another machine now, but fileA is missing. 我想现在将此存储库克隆到另一台机器,但缺少fileA。

The next thing I did was to create a branch from the commit where both files were present. 我接下来要做的是从提交中创建一个分支,其中存在两个文件。 The big question now is: how do I get back to the master commit without losing that file again? 现在最大的问题是:如何在不丢失该文件的情况下返回主提交?

Sorry for the title. 对不起,标题。 I was trying to be descriptive, but .. 我试图描述,但..

If all you need to do is restore one file, you can do that with git checkout : 如果你需要做的就是恢复一个文件,你可以使用git checkout来做到这一点:

$ git checkout <revision_where_fileA_exists> fileA
$ git add fileA
$ git commit -m "Restored fileA"

You will also have to edit your .gitignore so that it isn't ignoring fileA anymore. 您还必须编辑.gitignore以便它不再忽略fileA

I think Josh's answer is probably what you want for this situation, but if you run into again, you have a couple options... The classic merge or pull will always work, you just need to specify -f if you are trying to undelete files that are in your ancestry. 我认为Josh的回答可能就是你想要的这种情况,但是如果你再次遇到,你有几个选择...经典的合并或拉动将始终有效,你只需要指定-f如果你试图取消删除你祖先的文件。 Otherwise, git will complain. 否则,git会抱怨。

$ git checkout master
$ git pull -f . <branch>:master

That command should auto-commit your change if you just had a file deletion. 如果您只删除文件,该命令应自动提交您的更改。 You can also explore the following comands for more revert specific features: 您还可以浏览以下命令以获取更多还原特定功能:

$ git reset 
$ git revert

Reset is really used if there is a small change that you want to back out that doesn't need to pollute the logs, but it is quite powerfull, look into the docs. 如果您想要退出的一个小的更改不需要污染日志,那么确实使用了重置,但它非常强大,请查看文档。

Revert is for undoing things that have happened in the past. Revert用于撤消过去发生的事情。 More similar to "backing out" a change. 更类似于“退出”变更。

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

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