如何在 git 中将 master 移回几次提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23227927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:01:17  来源:igfitidea点击:

How do I move master back several commits in git?

gitgit-reset

提问by Hoytman

I have a git repository which holds a Drupal site. I spent the last day trying to build a feature using several different modules. I have given up on my current approach and have decided to try a different combination of modules. However, my repository has several commits on the master branch that contain this feature development process (I understand that I did not branch in an effective manner.) I want to get rid of the last three or four commits and set master to that point in my history (I don't want to merge my current work with anything, I just want it to go away.) How do I do this?

我有一个 git 存储库,其中包含一个 Drupal 站点。我花了最后一天尝试使用几个不同的模块构建一个功能。我已经放弃了我目前的方法,并决定尝试不同的模块组合。但是,我的存储库在包含此功能开发过程的 master 分支上有几个提交(我知道我没有以有效的方式进行分支。)我想摆脱最后三四个提交并将 master 设置为我的历史(我不想将我当前的工作与任何内容合并,我只想让它消失。)我该怎么做?

回答by merlin2011

In order to do it locally, you can do the following commands to go to master and move it to the old commit.

为了在本地完成,您可以执行以下命令转到 master 并将其移动到旧提交。

git checkout master
git reset --hard <old_commit_id>

If you then want to push it to the remote, you need to use the -foption.

如果您想将其推送到遥控器,则需要使用该-f选项。

git push -f origin master

回答by grisaitis

To point master 3 commits back:

指向 master 3 提交:

$ git reset --hard master~3

I recommend backing up your current master before doing this

我建议在执行此操作之前备份您当前的主服务器

$ git checkout -b master_backup

After moving master, see your tree of commits:

移动 master 后,查看您的提交树:

$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

回答by vonbrand

You can always do a git reset <commit>. Perhaps the easiest way to do this is use a graphical frontend, i.e. gitk.

你总是可以做一个git reset <commit>. 也许最简单的方法是使用图形前端,即gitk.

You should perhaps first do a git branch branch-for-failed-experimentso the work on the experiment isn't lost forever.

你也许应该先做一个git branch branch-for-failed-experiment这样实验的工作不会永远丢失。

Be careful, if you published the branch (i.e., if others could have work based on your to-be-deleted commits), they will be left stranded. Make sure they sync up with you.

请注意,如果您发布了分支(即,如果其他人可以根据您要删除的提交进行工作),他们将被搁置。确保他们与您同步。