英文:
How to undo a commit in GitHub?
问题
你好,我是GitHub的新手。
我已经完成了以下的流程:
- 第一次提交
- 我在代码中做了很多更改
- 第二次提交
现在我想撤销第二次提交,因为我在第二次提交后错误地进行了不必要的更改。
有人可以告诉我如何回到第二次提交之前吗?
基本上,我想恢复我在步骤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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论