简体   繁体   English

如何在 git 中恢复我的功能分支(远程)上的最后(最新)提交

[英]How to revert last(latest) commit on my feature branch(Remote) in git

I have pushed 2 commits in my feature branch(remote) but now I have to revert back the latest commit for the branch.我已经在我的功能分支(远程)中推送了 2 个提交,但现在我必须恢复该分支的最新提交。

eg commit A latest commit B first例如先提交 A 最新提交 B

here I want to delete commit A and want to leave changes as it was in commit B在这里我想删除提交 A 并希望保留更改,因为它在提交 B 中

I can see many solution in google but not sure what to do?我可以在谷歌中看到很多解决方案,但不确定该怎么做? Some says make the changes and again push the changes in remote.有人说进行更改并再次将更改推送到远程。 Some says to delete the last commit and rewrite the history.有人说要删除最后一次提交并重写历史记录。

I don't want to loose the changes from the first commit.我不想丢失第一次提交的更改。 and just want to delete the latest commit from remote branch.并且只想从远程分支中删除最新的提交。

One easy approach is to create a new commit cancelling the changes of commit A, using the git revert operation.一种简单的方法是创建一个新提交,使用git revert操作取消提交 A 的更改。

This method has the advantage of not altering the commit history, which makes it straightforward for other users to pull the feature branch.这种方法的优点是不改变提交历史,这使得其他用户可以直接拉取特性分支。

  • In your local repository, execute the following:在您的本地存储库中,执行以下命令:
git revert <sha-of-commit-A>
  • Enter a commit message like "undo changes of commit-A"输入一条提交消息,例如“撤消提交-A 的更改”
  • Then push the feature branch to the remote.然后将功能分支推送到远程。

The feature branch will then only contain the changes of commit-B. feature 分支将只包含 commit-B 的更改。

Reference article with more details.包含更多详细信息的参考文章

Note : I recommend Mehdi's answer as it is the safer approach, but I will keep my answer in case your preference is to remove the reverted commit from history.注意:我推荐 Mehdi 的回答,因为它是更安全的方法,但我会保留我的回答,以防您希望从历史记录中删除还原的提交。

This should be relatively straightforward, follow these steps (I will assume that the local feature branch is called localFeatureBranch ):-这应该相对简单,请按照以下步骤操作(我假设本地功能分支称为localFeatureBranch ):-

1- Create a local backup of your feature branch (in case things go wrong):- 1- 创建功能分支的本地备份(以防 go 出错):-

git checkout localFeatureBranch
git branch localFeatureBranchBackup

2- Now that you have a backup, delete the latest commit (it would be good to ensure first that you don't have any changes that need to be committed):- 2- 现在您有了备份,删除最新的提交(最好先确保您没有任何需要提交的更改):-

git reset --hard HEAD~1

The above will completely delete the last commit as well as any staged/unstaged changes, so please be careful when using the above command.以上将完全删除最后一次提交以及任何已暂存/未暂存的更改,因此使用上述命令时请小心。 A more forgiving command would be git reset --soft HEAD~1, but then you have to go to files one by one and unstage them/revert them.一个更宽容的命令是 git reset --soft HEAD~1,但是你必须 go 一个一个地文件并取消它们/恢复它们。

3- Now force push your changes to the remote branch:- 3-现在强制将您的更改推送到远程分支:-

git push origin localFeatureBranch --force

Force pushing a branch is not always recommended especially if there are multiple people working on the same remote branch, but if you are the only one working on this branch and you have already backed it up locally (which you should have in step 1), then it should be a harmless exercise.并不总是推荐强制推送一个分支,特别是如果有多个人在同一个远程分支上工作,但如果你是唯一一个在这个分支上工作的人并且你已经在本地备份了它(你应该在步骤 1 中),那么它应该是一种无害的运动。

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

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