如何在GitHub中撤销一个提交?

huangapple go评论70阅读模式
英文:

How to undo a commit in GitHub?

问题

你好,我是GitHub的新手。
我已经完成了以下的流程:

  1. 第一次提交
  2. 我在代码中做了很多更改
  3. 第二次提交

现在我想撤销第二次提交,因为我在第二次提交后错误地进行了不必要的更改。
有人可以告诉我如何回到第二次提交之前吗?
基本上,我想恢复我在步骤2中所做的更改。

英文:

Hello I am new in GitHub.
And I have done the following process

1)first commit
2)I make lot of changes in code
3)second commit

Now I want to undo the second commit because mistakenly I have done different unwanted changes after the second commit.
Can anyone tell me how can I go before the second commit.
Basically I want to restore changes which I have made in step 2.

答案1

得分: 1

你有不同的方法可以做到这一点。这取决于你的情况,哪种方法对你最合适。

通过执行这个命令,它会在提交历史中添加一个新的提交(你可以使用git log来查看)。如果你运行这个命令,根据你描述的情况,它可能会是这样的:

commit <hash> (HEAD -> master, origin/master, origin/HEAD)
Author: Some Name <some-email@email.com>
Date:   some date

    Some message you wrote
    
commit <hash>
Author: Some Name <some-email@email.com>
Date:   some date

    Initial commit

一旦你运行了 git revert 命令,如果再次运行 git log,你会看到一个新的提交。

这个命令将从日志历史中移除提交。使用 --soft 参数,将会将那个提交中的文件准备好以便重新提交。如果使用 --hard 而不是 --soft,那么文件将被丢弃。一旦你修复了需要的内容并进行了提交,并且想要进行推送,就必须使用 --force--force-with-lease 参数执行该命令(文档中详细解释了它们的工作原理)。在进行推送之前运行 git status,你会看到一条消息,提示你需要进行拉取并推送,因为存在一些分歧。这就是为什么你需要添加提到的参数。

英文:

You have different ways of do that. It depends on your case what is best for you.

Doing this, it will add a new commit on the commit history (you can see that using git log. If you run that command, based on what you described, it would be something like

commit <hash> (HEAD -> master, origin/master, origin/HEAD)
Author: Some Name <some-email@email.com>
Date:   some date

    Some message you wrote
    
commit <hash>
Author: Some Name <some-email@email.com>
Date:   some date

    Initial commit

once you run the git revert command, you would see a new commit if you run git log again

this other command will remove the commit from the log history. Using the --soft param, will put your files that where in that commit ready for being commited again. If you use --hard instead of --soft, the files will be discarded. Once you fix whatever you need and make the commit and want to make the push, the command will have to be executed with the param --force or --force-with-lease (in the docs is well explained how they work). If you run git status before doing the push, you would see a message saying that you have to make pull and also push due to some divergence. That's why you will need to add the param mentioned

huangapple
  • 本文由 发表于 2023年5月28日 04:54:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定