我可以删除git commit但保留更改吗? [英] Can I delete a git commit but keep the changes?

查看:96
本文介绍了我可以删除git commit但保留更改吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个开发分支中,我对代码库进行了一些更改.在我能够完成正在使用的功能之前,我必须将当前分支切换为master来演示一些功能.但是仅使用"git checkout master"保留了我在开发分支中所做的更改,因此破坏了master中的某些功能.因此,我要做的是使用临时提交"提交消息在开发分支上提交更改,然后为演示结帐母版.

In one of my development branches, I made some changes to my codebase. Before I was able to complete the features I was working on, I had to switch my current branch to master to demo some features. But just using a "git checkout master" preserved the changes I also made in my development branch, thus breaking some of the functionality in master. So what I did was commit the changes on my development branch with a commit message "temporary commit" and then checkout master for the demo.

现在,我已经完成了演示,然后又可以在开发分支上工作了,我想删除我所做的临时提交",同时仍然保留所做的更改.有可能吗?

Now that I'm done with the demo and back to work on my development branch, I would like to remove the "temporary commit" that I made while still preserving the changes I made. Is that possible?

推荐答案

就这么简单:

git reset HEAD^

注意:某些shell将^视为特殊字符(例如,某些Windows shell或 ZSH与),因此在这种情况下,您可能必须引用"HEAD^".

Note: some shells treat ^ as a special character (for example some Windows shells or ZSH with globbing enabled), so you may have to quote "HEAD^" in those cases.

git reset会移动您的HEAD指向指定的提交,而不更改任何文件. HEAD^是指当前提交的(第一个)父提交,在您的情况下,它是临时提交之前的提交.

git reset without a --hard or --soft moves your HEAD to point to the specified commit, without changing any files. HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one.

请注意,另一个选择是照常进行,然后在下一个提交点运行:

Note that another option is to carry on as normal, and then at the next commit point instead run:

git commit --amend [-m … etc]

而是 edit 最新提交,效果与上述相同.

which will instead edit the most recent commit, having the same effect as above.

请注意,如果您已经将错误的提交推送到其他人可能将其撤出的地方,则此问题(几乎与所有git答案一样)可能会导致问题.尝试避免这种情况

Note that this (as with nearly every git answer) can cause problems if you've already pushed the bad commit to a place where someone else may have pulled it from. Try to avoid that

这篇关于我可以删除git commit但保留更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆