Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

git - 我可以删除git commit但可以保留更改(Can I delete a git commit but keep the changes)

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.

(在我能够完成正在使用的功能之前,我必须将当前分支切换为master来演示一些功能。)

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.

(但是仅使用“ git checkout master”保留了我也在开发分支中所做的更改,从而破坏了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?

(那可能吗?)

  ask by tanookiben translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(无需使用--hard--soft git reset即可将HEAD移至指定的提交,而无需更改任何文件。)

HEAD^ refers to the (first) parent commit of your current commit, which in your case is the commit before the temporary one.

(HEAD^是指您当前提交的(第一个)父提交,在您的情况下,它是临时提交之前的提交。)

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.

(请注意,如果您已经将错误的提交推送到其他人可能已将其撤出的地方,则此问题(几乎与每个git答案一样)可能会导致问题。)

Try to avoid that

(尽量避免这种情况)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...