简体   繁体   English

需要回到git中的上一步提交,如何?

[英]Need to go back to prev commit in git,How?

I have modified my source directory by deleting certain files and done a commit . 我已经通过删除某些文件修改了源目录并进行了commit And I did a push origin master to remote host, github. 然后我push origin master送到远程主机github。

Now I came to know that I need those files for proper functioning. 现在我知道我需要那些文件才能正常运行。 Now I need a way to go back my prev commit and then push to my remote host. 现在,我需要一种方法来返回上一个commit ,然后push送到远程主机。

Is it possible to do so? 有可能这样做吗? I'm very new to git, I'm confused with it. 我对git很陌生,对此感到困惑。

Thanks in advance. 提前致谢。

Since you've already pushed, I would recommend against Nikhil's solution and recommend that you do a "git revert" on your latest commit like so git revert HEAD and then push. 由于您已经推送了,所以我建议您反对Nikhil的解决方案,并建议您对最新提交执行“ git revert”,例如git revert HEAD然后推送。 The revert command will create a new commit that undoes the effect the specified commit and adds it to your repository. revert命令将创建一个新的提交,以撤消指定提交的影响,并将其添加到您的存储库中。

git revert creates a new commit that undoes one or more previous commits. git revert创建一个新的提交,该提交撤消一个或多个先前的提交。 This is usually the best way to undo commits that have already been pushed. 通常,这是撤消已经推送的提交的最佳方法。

For example, this command would revert everything from the commit abc1234 up to and including the latest commit ( HEAD ): 例如,此命令将还原从提交abc1234到最新的提交( HEAD )的所有内容:

git revert abc1234..HEAD

You could also use git reset , but this command changes history and would cause problems for anyone else who is using the repository. 您也可以使用git reset ,但是此命令会更改历史记录,并且会对使用存储库的其他任何人造成问题。 In general you should only use this command if you want to discard changes that haven't been pushed (and that you're sure you won't want to come back to in future). 通常,仅当您要放弃尚未推送的更改(并且您确定将来不希望再返回)时才应使用此命令。

Try on of these, 试试这些

git reset --hard SHAsumOfYourCommit
git reset --hard HEAD [your current head point]
git reset --hard HEAD^ [your previous head point]

OR you can Delete the last commit 或者您可以删除最后一次提交

Let's say we have a remote myrepo with branch master that currently points to commit dd61ab32, you can remove the last commit byusing the command. 假设我们有一个带分支master的远程myrepo,该远程myrepo当前指向提交dd61ab32,您可以使用以下命令删除最后一个提交。

git push myrepo +dd61ab32^:master

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

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