简体   繁体   English

"通过 1 次提交回滚本地和远程 git 存储库"

[英]Rolling back local and remote git repository by 1 commit

I've read the similar posts on this topic, and can't for the life of me figure out how to do this properly.我已经阅读了关于这个主题的类似帖子,但我一生都无法弄清楚如何正确地做到这一点。

I checked in about 1000 files that I don't want, and I'd rather not have to go through 1by1 and remove them all from the repo.我签入了大约 1000 个我不想要的文件,我宁愿不必通过 1by1 并将它们全部从存储库中删除。

  • I have a remote master Branch.我有一个远程master分支。
  • I have the local master Branch.我有本地master分支。

They are both at the same revision.他们都在同一个版本。

I want to rollback my remote by 1 commit.我想通过 1 次提交回滚我的遥控器。

Say my history on master is A--B--C--D--E .假设我在master上的历史是A--B--C--D--E
I want to rollback my local to D .我想将我的本地回滚到D
Then push it to remote so my current hash will be D both remote and local.然后将其推送到远程,这样我当前的哈希将是远程和本地的 D。

I'm having issues doing this.我在执行此操作时遇到问题。
I'm using Git Tower but am comfortable with the command line.我正在使用 Git Tower,但对命令行很满意。 Any help?有什么帮助吗?

UPDATE: Great comments below.更新:下面的评论很棒。 Using a reset seems to be partially discouraged especially if the repository is shared with other users.似乎部分不鼓励使用重置,尤其是在与其他用户共享存储库的情况下。 What's the best way to undo the previous commit's changes without using a hard reset ?在不使用硬重置的情况下撤消先前提交更改的最佳方法是什么 Is there a way?有办法吗?

If nobody has pulled your remote repo yet, you can change your branch HEAD and force push it to said remote repo:如果还没有人提取您的远程仓库,您可以更改您的分支 HEAD 并强制将其推送到所述远程仓库:

git reset --hard HEAD^ 
git push -f 

Set the local branch one revision back ( HEAD^ means one revision back):将本地分支设置回一个修订版( HEAD^表示回一个修订版):

git reset --hard HEAD^

Push the changes to origin:将更改推送到原点:

git push --force

You will have to force pushing because otherwise git would recognize that you're behind origin by one commit and nothing will change.您将不得不强制推送,因为否则 git 会通过一次提交识别出您落后于origin ,并且什么都不会改变。

Doing it with --force tells git to overwrite HEAD in the remote repo without respecting any advances there.使用--force告诉 git 覆盖远程仓库中的HEAD而不尊重那里的任何进展。

If you want revert last commit listen:如果你想恢复最后一次提交,请听:

Step 1:步骤1:

Check your local commits with messages使用消息检查您的本地提交

$ git log

Here's an updated version of the procedure which is safer.这是该过程的更新版本,更安全。

git reset --hard HEAD^ 
git push --force-with-lease

By entering bellow command you can see your git commit history -通过输入以下命令,您可以查看您的 git 提交历史记录 -

$ git log<\/strong> $混帐日志<\/strong>

Let's say your history on that particular branch is like - commit_A, commit_B, commit_C, commit_D.假设您在该特定分支上的历史记录就像 - commit_A、commit_B、commit_C、commit_D。 Where, commit_D is the last commit and this is where HEAD remains.其中,commit_D 是最后一次提交,这是 HEAD 所在的位置。 Now, to remove your last commit from local and remote, you need to do the following :现在,要从本地和远程删除您的最后一次提交,您需要执行以下操作:

Step 1: Remove last commit locally by -第 1 步:通过以下方式在本地删除最后一次提交 -

$ git reset --hard HEAD~<\/strong> $ git reset --hard HEAD~<\/strong>

This will change your commit HEAD to commit_C这会将您的提交 HEAD 更改为 commit_C

Step 2: Push your change for new HEAD commit to remote第 2 步:将新 HEAD 提交的更改推送到远程

$ git push origin +HEAD<\/strong> $ git 推送来源 +HEAD<\/strong>

This command will delete the last commit from remote.此命令将从远程删除最后一次提交。

PS this command is tested on Mac OSX and should work on other operating systems as well (not claiming about other OS though)<\/strong> PS这个命令在Mac OSX上测试过,应该也可以在其他操作系统上运行(虽然没有声称其他操作系统)<\/strong>

"

对于 Windows 机器,请使用:

git reset HEAD~1  #Remove Commit Locally

There are many way you can do this.有很多方法可以做到这一点。 Based on your requirement choose anything from below.根据您的要求,从下面选择任何内容。

1. By REVERTing commit: 1. 通过 REVERTing 提交:

If you want to REVERT all the changes from you last COMMIT that means If you ADD something in your file that will be REMOVED after revert has been done.如果您想恢复上次 COMMIT 的所有更改,这意味着如果您在文件中添加了某些内容,这些内容将在恢复完成后被删除。 If you REMOVE something in your file the revert process will ADD those file.如果您删除文件中的某些内容,则还原过程将添加这些文件。

You can REVERT the very last COMMIT.您可以 REVERT 最后一次提交。 Like:喜欢:

1.git revert head^
2.git push origin <Branch-Name>

Or you can revert to any previous commit using the hash of that commit.Like:或者您可以使用该提交的哈希恢复到任何先前的提交。例如:

1.git revert <SHA>
2.git push origin  <Branch-Name>

2. By RESETing previous Head 2.通过重置前一个头

If you want to just point to any previous commit use reset;如果您只想指向任何先前的提交,请使用重置; it points your local environment back to a previous commit.它将您的本地环境指向之前的提交。 You can reset your head to previous commit or reset your head to previous any commit.您可以将您的头部重置为之前的提交或将您的头部重置为之前的任何提交。

Reset to previous commit.重置为先前的提交。

1.git reset head^
2.git push -f origin <Branch-name>

Reset to any previous commit:重置为任何先前的提交:

1.git reset <SHA>
2.git push -f origin <Branch-name>

Trade of between REVERT & RESET: REVERT 和 RESET 之间的交易:

Why would you choose to do a revert over a reset operation?为什么您会选择还原而不是重置操作? If you have already pushed your chain of commits to the remote repository (where others may have pulled your code and started working with it), a revert is a nicer way to cancel out changes for them.如果您已经将提交链推送到远程存储库(其他人可能已经提取了您的代码并开始使用它),那么恢复是取消更改的更好方法。 This is because the Git workflow works well for picking up additional commits at the end of a branch, but it can be challenging if a set of commits is no longer seen in the chain when someone resets the branch pointer back.这是因为 Git 工作流可以很好地在分支结束时获取额外的提交,但是当有人重置分支指针时,如果在链中不再看到一组提交,这可能会很有挑战性。

I solved problem like yours by this commands:我通过以下命令解决了像您这样的问题:

git reset --hard HEAD^
git push -f <remote> <local branch>:<remote branch> 

You can also do this:你也可以这样做:

git reset --hard <commit-hash>
git push -f origin master

and have everyone else who got the latest bad commits reset:并让其他所有获得最新错误提交的人重置:

git reset --hard origin/master

If you have direct access to the remote repo, you could always use:如果您可以直接访问远程存储库,则始终可以使用:

git reset --soft HEAD^

This works since there is no attempt to modify the non-existent working directory.这是有效的,因为没有尝试修改不存在的工作目录。 For more details please see the original answer:有关详细信息,请参阅原始答案:

How can I uncommit the last commit in a git bare repository? 如何取消提交 git 裸存储库中的最后一次提交?

I just wanted to remove last commit from remote and clear commit history also.我只是想从远程和清除提交历史记录中删除最后一次提交。 The following worked like a charm以下工作就像一个魅力

git reset --hard HEAD^ 
git push -f 

The way to reset the head and do the revert to the previous commit is through重置头部并恢复到先前提交的方法是通过

$ git reset HEAD^ --hard
$ git push <branchname> -f

But sometimes it might not be accepted in the remote branch:但有时它可能不会在远程分支中被接受:

To ssh:<git repo>
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'ssh:<git repo>'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

then the other way to do is那么另一种方法是

git revert HEAD
git push <remote branch>

This works fine.这工作正常。

NOTE: remember if the git push -f <force> failed and then you try to revert.注意:请记住如果git push -f <force>失败,然后您尝试恢复。 Do a git pull before, so that remote and local are in sync and then try git revert .执行git pull之前,使远程和本地同步,然后尝试git revert
Check with git log to make sure the remote and local are at same point of commit with same SHA1..检查git log以确保远程和本地在相同的提交点具有相同的 SHA1..

git revert 
A --> B --> C -->D
A--> B --> C --> D --> ^D(taking out the changes and committing reverted diffs)

on local master在本地主机上<\/h1>
git reflog -- this will list all last commit eg Head@{0} -- wrong push Head@{1} -- correct push git checkout Head@{1} . -- this will reset your last modified files git status git commit -m "reverted to last best" git push origin\/master<\/code><\/pre>

No need to worry if other has pulled or not.无需担心其他是否拉动。

Done!完毕!

"

If you only want to remove the last commit from the remote repository without messing up with your local repository, here's a one-liner:如果您只想从远程存储库中删除最后一次提交而不弄乱您的本地存储库,这里有一个单行:

git push origin +origin/master~:master

This uses the following syntax:这使用以下语法:

git push <remote> <refspec>

Here, <remote> is origin , and <refspec> has the following structure:这里, <remote>origin<refspec>具有以下结构:

+origin/master~:master

Details can be found in git-push(1) .详细信息可以在git-push(1)中找到。 The preceding + means "force push this ref", and the other part means "from origin/master~ to master (of remote origin )".前面的+表示“强制推送这个 ref”,另一部分表示“从origin/master~master (of remote origin )”。 It isn't hard to know that origin/master~ is the last commit before origin/master , right?不难知道origin/master~origin/master之前的最后一次提交,对吧?

for me works this two commands:对我来说,这两个命令起作用:

git checkout commit_id
git push origin +name_of_branch

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

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