Undo Intellij Smart Checkout

房东的猫 提交于 2020-06-10 02:28:48

问题


Intellij has a feature that's very cool in theory, called Smart Checkout. This feature kicks in when you're changing branches and you have files in the current branch that you've modified but haven't committed.

Instead of forcing you to commit, stash or shelve your changes, it stashes them for you, switches branches, then runs stash pop in the new branch.

I guess this is what you'd want sometimes, but I ran this when switching to the wrong branch.

So, now my master branch is all full of changes that belong in another branch, some files are reporting merge conflicts, and I have all kinds of pain.

What I want to accomplish is:

  1. Cleanly remove the changes from the master branch.
  2. Move them back to the branch where I was working.

Is there a way to do this?


回答1:


Here's at least a partial answer that works if you had a merge conflict during the stash pop. As mentioned in my question, stash is used by the Smart Checkout feature to stash your local changes, and then to apply them to the new branch after it is checked out.

The way Intellij does this is by using stash in the branch you're presently in, and then using stash pop in the branch you're switching to.

When the changes are stashed, they get put onto a stack of stashed changes, at the top. Then, when stash pop runs, those changes are popped off the stack and applied.

At least, in most cases, that's what happens. If there's a merge conflict, however, Intellij informs you of such and the stash is kept. You can see the stack of stashes by running:

git stash list

If the stash you want is still listed, what you can do is simply checkout the branch you were originally on. Reset it, then do stash apply, which is like stash pop, but doesn't remove the stash from the list. So:

git checkout $original-branch
git reset HARD
git stash apply

Then, if all is well, you can remove the stash with:

git stash drop

Since this answer is pretty rough and only covers one situation, I'm marking as a community wiki. Improvements very much welcome.




回答2:


As the merge failed because of conflicts, the stash isn't removed from the stack, preserving the uncommitted changes. In this case, the git stash list command should show the stash.

First force checkout the original branch. This ignores the merge conflicts.

git checkout -f $original-branch

Then reset the working directory to remove any changes made by the attempted merge.

git reset --hard HEAD

After that, apply the stashed changes. This doesn't remove the stash from the stack.

git stash apply

Confirm that you have all the needed changes and then remove the stash.

git stash drop


来源:https://stackoverflow.com/questions/32318824/undo-intellij-smart-checkout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!