git 如何恢复到 origin 的 master 分支的文件版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1817766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:25:55  来源:igfitidea点击:

How to revert to origin's master branch's version of file

git

提问by mrblah

I'm in my local computer's master branch of a cloned master-branch of a repo from a remote server.

我在本地计算机的 master 分支中,它是来自远程服务器的 repo 的克隆 master 分支。

I updated a file, and I want to revert back to the original version from the remote master branch.

我更新了一个文件,我想从远程主分支恢复到原始版本。

How can I do this?

我怎样才能做到这一点?

回答by gahooa

Assuming you did not commit the file, or add it to the index, then:

假设您没有提交文件,或将其添加到索引中,则:

git checkout -- filename

Assuming you added it to the index, but did not commit it, then:

假设您将其添加到索引中,但未提交,则:

git reset HEAD filename
git checkout -- filename

Assuming you did commit it, then:

假设你确实提交了,那么:

git checkout origin/master filename

Assuming you want to blow away all commits from your branch (VERY DESTRUCTIVE):

假设你想从你的分支中删除所有提交(非常具有破坏性):

git reset --hard origin/master

回答by Venkat.R

I've faced same problem and came across to this thread but my problem was with upstream. Below git command worked for me.

我遇到了同样的问题并遇到了这个线程,但我的问题是upstream. 下面的 git 命令对我有用。

Syntax

句法

git checkout {remote-name}/{branch} -- {file/path.js}

git checkout {remote-name}/{branch} -- {file/path.js}

Example

例子

git checkout upstream/develop -- public/js/index.js

git checkout upstream/develop -- public/js/index.js

回答by commonpike

If you didn't commit it to the master branch yet, its easy:

如果您还没有将其提交到 master 分支,则很简单:

  • get off the master branch (like git checkout -b oops/fluke/dang)
  • commit your changes there (like git add -u; git commit;)
  • go back the master branch (like git checkout master)
  • 离开主分支(如git checkout -b oops/fluke/dang
  • 在那里提交您的更改(例如git add -u; git commit;
  • 返回主分支(如git checkout master

Your changes will be saved in branch oops/fluke/dang; master will be as it was.

您的更改将保存在分支 oops/fluke/dang 中;主人会原样。