简体   繁体   English

使用 Github 和 VS Code 检查之前的提交

[英]Checking out previous commit with Github and VS Code

I accidentally made a commit that I should not have to the master of my project in GitHub. I am now trying to checkout to a previous commit using VS Code.我不小心做了一个提交,我不应该在 GitHub 中提交我的项目的主人。我现在正在尝试使用 VS 代码检出以前的提交。 I got the URL of that commit.我得到了那个提交的 URL。 I do have the remote set up in VS Code but cannot figure out how to do this.我确实在 VS Code 中设置了远程,但不知道如何执行此操作。

My thought was to go to checkout to and enter the URL of the commit.我的想法是 go 签出并输入提交的 URL。 Then I would switch to the master branch and commit this.然后我会切换到 master 分支并提交它。 But that gives me an error "Can't push refs to remote" .但这给了我一个错误"Can't push refs to remote"

1- Open the terminal in your VS (make sure terminal openned in your project) 1- 在你的 VS 中打开终端(确保终端在你的项目中打开)

2- write: 2-写:

$ git log

It will show you list of recent commits copy the the commit ID you want to back to it.它将向您显示最近提交的列表,将您要返回的提交 ID 复制到它。 (Any commit above will be reset) (上面的任何提交都将被重置)

$ git reset [commitID]

Now you have your changes not committed.现在您没有提交更改。

You can checkout now to the branch your work is in and stage your changes and commit them.您现在可以签出您的工作所在的分支并暂存您的更改并提交它们。


If the issue is in your remote (as you mentioned in comments).如果问题出在您的遥控器上(正如您在评论中提到的那样)。

There is more than one way.有不止一种方法。 First sync your local with you remote Then the same approach with the git log to get the commit id.首先将本地与远程同步,然后使用 git 日志以相同的方法获取提交 ID。 Use git revert to fix your unwanted commit使用 git revert 修复不需要的提交

$ git revert [commitID] 

... ...

Removing a commit from the history on a remote is not easy;从远程历史记录中删除提交并不容易; it doesn't want to let you do it.它不想让你这样做。 On the branch where the problem is, working on your machine, you can fetch , then pull , then reset --hard back to before the unwanted commit, then push — but you will have to push with force, to get past the remote's reluctance to let you do this.在问题所在的分支上,在你的机器上工作,你可以fetch ,然后pull ,然后reset --hard回到不需要的提交之前,然后push — 但你必须用力推动,以克服远程的不情愿让你这样做。

I found this question by searching for checking out a previous commit within VSCode If you want to do this within vscode there is a good extensions for this: https://marketplace.visualstudio.com/items?itemName=donjayamanne.githistory我通过在 VSCode 中搜索检查以前的提交发现了这个问题如果你想在 vscode 中这样做,有一个很好的扩展: https://marketplace.visualstudio.com/items?itemName=donjayamanne.githistory

under "more" in the commit list (see below screenshot) you can checkout a specific commit在提交列表的“更多”下(见下面的截图)你可以签出一个特定的提交

在此处输入图像描述

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

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