我可以删除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? 那可能吗?


#1楼

参考:https://stackoom.com/question/14B3G/我可以删除git-commit但可以保留更改


#2楼

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 尽量避免这种情况


#3楼

One more way to do it. 另一种方法。

Add commit on the top of temporary commit and then do: 在临时提交的顶部添加提交,然后执行以下操作:

git rebase -i

To merge two commits into one (command will open text file with explicit instructions, edit it). 要将两个提交合并为一个(命令将使用明确的指令打开文本文件,对其进行编辑)。


#4楼

There are two ways of handling this. 有两种处理方法。 Which is easier depends on your situation 哪个更简单取决于您的情况

Reset 重启

If the commit you want to get rid of was the last commit, and you have not done any additional work you can simply use git-reset 如果您要删除的提交是最后一次提交,并且您还没有做任何其他工作,则可以简单地使用git-reset

git reset HEAD^

Takes your branch back to the commit just before your current HEAD. 将您的分支带回到当前HEAD之前的提交。 However, it doesn't actually change the files in your working tree. 但是,它实际上并不会更改工作树中的文件。 As a result, the changes that were in that commit show up as modified - its like an 'uncommit' command. 结果,该提交中的更改将显示为已修改-就像“取消提交”命令一样。 In fact, I have an alias to do just that. 实际上,我有一个别名可以做到这一点。

git config --global alias.uncommit 'reset HEAD^'

Then you can just used git uncommit in the future to back up one commit. 然后,您将来可以使用git uncommit备份一个提交。

Squashing 压扁

Squashing a commit means combining two or more commits into one. 压缩提交意味着将两个或多个提交合并为一个。 I do this quite often. 我经常这样做。 In your case you have a half done feature commited, and then you would finish it off and commit again with the proper, permanent commit message. 在您的情况下,您已经提交了一半完成的功能,然后您将其完成,然后使用适当的永久提交消息再次提交。

git rebase -i <ref>

I say above because I want to make it clear this could be any number of commits back. 我在上面说是因为我想说清楚,这可以是任意数量的提交。 Run git log and find the commit you want to get rid of, copy its SHA1 and use it in place of <ref> . 运行git log并找到要删除的提交,复制它的SHA1并用它代替<ref> Git will take you into interactive rebase mode. Git将带您进入交互式变基模式。 It will show all the commits between your current state and whatever you put in place of <ref> . 它将显示您当前状态与您放置的所有<ref>之间的所有提交。 So if <ref> is 10 commits ago, it will show you all 10 commits. 因此,如果<ref>是10次提交,它将显示所有10次提交。

In front of each commit, it will have the word pick . 在每次提交的前面,都会有单词pick Find the commit you want to get rid of and change it from pick to fixup or squash . 找到您要删除的提交,并将其从pick更改为fixupsquash Using fixup simply discards that commits message and merges the changes into its immediate predecessor in the list. 使用fixup只是丢弃提交消息,并将更改合并到列表中其直接前身。 The squash keyword does the same thing, but allows you to edit the commit message of the newly combined commit. squash关键字执行相同的操作,但是允许您编辑新组合的提交的提交消息。

Note that the commits will be re-committed in the order they show up on the list when you exit the editor. 请注意,当您退出编辑器时,提交将按照它们在列表中显示的顺序重新提交。 So if you made a temporary commit, then did other work on the same branch, and completed the feature in a later commit, then using rebase would allow you to re-sort the commits and squash them. 因此,如果您进行了一次临时提交,然后在同一分支上进行了其他工作,并在以后的提交中完成了该功能,那么使用rebase将允许您对提交进行重新排序并将其压扁。

WARNING: 警告:

Rebasing modifies history - DONT do this to any commits you have already shared with other developers. 变基修改历史记录-不要对您已与其他开发人员共享的任何提交执行此操作。

Stashing 藏匿

In the future, to avoid this problem consider using git stash to temporarily store uncommitted work. 将来,为避免此问题,请考虑使用git stash临时存储未提交的工作。

git stash save 'some message'

This will store your current changes off to the side in your stash list. 这会将您当前的更改存储在存储列表的侧面。 Above is the most explicit version of the stash command, allowing for a comment to describe what you are stashing. 上面是stash命令的最明确的版本,允许使用注释来描述要存储的内容。 You can also simply run git stash and nothing else, but no message will be stored. 您也可以简单地运行git stash而不执行其他任何操作,但是不会存储任何消息。

You can browse your stash list with... 您可以使用...浏览藏匿清单

git stash list

This will show you all your stashes, what branches they were done on, and the message and at the beginning of each line, and identifier for that stash which looks like this stash@{#} where # is its position in the array of stashes. 这将向您显示所有存储区,它们完成了哪些分支,消息以及每行的开头,以及该存储区的标识符,看起来像此stash@{#} ,其中#是其在存储区数组中的位置。

To restore a stash (which can be done on any branch, regardless of where the stash was originally created) you simply run... 要恢复存储(可以在任何分支上完成,而不管存储最初在何处创建),只需运行...

git stash apply stash@{#}

Again, there # is the position in the array of stashes. 再次,#是存储阵列中的位置。 If the stash you want to restore is in the 0 position - that is, if it was the most recent stash. 如果要还原的存储区位于0位置-也就是说,它是最近的存储区。 Then you can just run the command without specifying the stash position, git will assume you mean the last one: git stash apply . 然后,您可以运行命令而无需指定存储位置,git会假设您的意思是最后一个: git stash apply

So, for example, if I find myself working on the wrong branch - I may run the following sequence of commands. 因此,例如,如果我发现自己在错误的分支上工作,则可以运行以下命令序列。

git stash
git checkout <correct_branch>
git stash apply

In your case you moved around branches a bit more, but the same idea still applies. 在您的情况下,您在分支机构周围移动了一些,但是同样的想法仍然适用。

Hope this helps. 希望这可以帮助。


#5楼

是的,您可以删除提交而不删除更改:git reset @〜


#6楼

In my case, I already pushed to the repo. 就我而言,我已经推送到了仓库。 Ouch! 哎哟!

You can revert a specific commit while keeping the changes in your local files by doing: 您可以通过执行以下操作来还原特定的提交,同时将更改保留在本地文件中:

git revert -n <sha>

This way I was able to keep the changes which I needed and undid a commit which had already been pushed. 这样,我可以保留我需要的更改,并取消已经推送的提交。

发布了0 篇原创文章 · 获赞 137 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105341631