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

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

问题描述

在我的一个开发分支中,我对代码库进行了一些更改。在我能够完成我正在开发的功能之前,我必须将当前的分支切换到主控以演示某些功能。但只是使用git checkout master保留了我在开发分支中所做的更改,从而破坏了master中的一些功能。因此,我所做的是在提交消息临时提交的情况下在我的开发分支上提交更改,然后为演示签出master。



现在我完成了演示并返回到我的开发分支上,我想删除我做的临时提交,同时仍保留我所做的更改。这可能吗?

解决方案

简单如此:

  

git reset - hard - soft 移动您的 HEAD 指向指定的提交,而不更改任何文件。 HEAD ^ 引用您当前提交的(第一个)父提交,在您的情况下为临时提交之前的提交。



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

  git commit --amend [-m ... etc] 

而不是编辑 >最近的提交,具有与上面相同的效果。



请注意,如果您已经推送了错误(与几乎每个git答案一样),都会导致问题承诺一个地方,其他人可能已经从中拉出来。尽量避免那个

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?

解决方案

It's as simple as this:

git reset 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]

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

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天全站免登陆