简体   繁体   English

将提交的文件还原为master中的版本?

[英]Revert committed files to their version in master?

I modified a number of files and committed my changes. 我修改了许多文件并提交了我的更改。

I have noticed that there are 3 files that I shouldn't have modified, and I want them to go back to being what they were in master, but I don't want to reset to a previous commit because I don't want my other file changes being lost. 我注意到有3个文件我不应该修改,我希望它们回到原来的状态,但是我不想重置为之前的提交,因为我不想要我的其他文件更改丢失。 How do I accomplish this? 我该如何做到这一点?

If you did not push yet so your commit is only local for now, a simple process would be 如果你还没有推送,所以你的提交目前只是本地的,一个简单的过程就是

# restore these 3 files to their previous state
git checkout HEAD^ -- path/to/file1 path/to/file2 path/to/file3

# include these changes...
git add path/to/file1 path/to/file2 path/to/file3

# ... in last commit
git commit --amend

However, if you did already push , there are 2 basic scenarios : 但是,如果您已经推送过 ,则有两种基本方案:

1) Either you work alone on this repo, or share the repo but work alone on this branch (oftentimes the case for feature branches, but depends on your workflow) 1) 无论你对这个回购单独工作,或分享回购,但这一分支(常常为功能分支的情况下单独工作,而是取决于你的工作流程)

The above process is fine, just be aware that the next time you'll want to push to the remote, you'll have to use the -f flag in your push command to force-update the old ref. 上面的过程很好,只要知道下次你想要推送到遥控器时,你必须在你的push命令中使用-f标志来强制更新旧的ref。

2) or this is a shared branch, its history can't be changed without some amount of trouble 2) 或者这是一个共享分支,它的历史记录如果没有一些麻烦就无法改变

Then the process above, rewriting history, is to be avoided. 然后应该避免重写历史的上述过程。 Instead, you'll just have to omit the --amend parameter at the end, and it'll make a new commit which contains only this last modification of your files. 相反,你只需要在结尾省略--amend参数,它将进行一个新的提交,它只包含你文件的最后一次修改。 Less elegant than commiting it right in the first place but you did not change history and your files are fine. 不是优雅,而是首先提交它,但你没有改变历史记录,你的文件很好。

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

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