Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo IntelliJ Smart Checkout

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?

like image 493
mlissner Avatar asked Aug 31 '15 19:08

mlissner


People also ask

How do I undo checkout in IntelliJ?

You can always undo the changes you've made locally before you commit them: In the Commit tool window Alt+0 , select one or more files that you want to revert, and select Rollback from the context menu, or press Ctrl+Alt+Z .

How do I undo changes in IntelliJ?

Revert changes For most recent changes, including refactorings, press Ctrl+Z or choose Edit | Undo from the menu.

What is IntelliJ smart checkout?

If you click Smart Checkout, IntelliJ IDEA will shelve uncommitted changes, check out the selected branch, and then unshelve the changes. If a conflict occurs during the unshelve operation, you will be prompted to merge the changes.

How do I undo a rebase in IntelliJ?

Open the repository folder in your terminal (linux, osx) or in the Git Bash (windows). Let's abort and start again, execute in the terminal: "git rebase --abort" . This command will revert your master to the HEAD state before you start the rebase.


3 Answers

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 it as a community wiki. Improvements very much welcome.

like image 130
2 revs, 2 users 94% Avatar answered Sep 21 '22 18:09

2 revs, 2 users 94%


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 
like image 25
NorbiPeti Avatar answered Sep 21 '22 18:09

NorbiPeti


I'm coming late to the party but I got a little scared after thinking I lost this morning's work due to an IntelliJ Smart Checkout. So I looked to see if there was a fix and this question came up first.

Sadly, my git stash list was empty (probably because I tried merging the conflicting files) so I couldn't use the suggestions given in the other answers.

I started looking in the local history (right click in editor > Local History > Show History) to at least get the changes whose locations I remembered. Then I did the same on the project root folder to try and get all the missing changes (I should have done that immediately) and noticed a change called Uncommitted_changes_before_Checkout_at_27_04_2022_12_06__Changes.xml. As you can see in the screen capture, this file is located in the .idea/shelf folder of your project.

It's associated with an almost eponymous folder "Uncommitted_changes_before_Update_at_27_04_2022_12_06_[Changes]" and both aren't deleted anytime soon after the Smart Checkout since I had an other pair dated from 20/04/2022.

The folder contains a shelved.patch file which regroups all your changes in the format shown on the screen capture (I assure you my morning's work wasn't just adding a console.log).

There must be a way to have IntelliJ properly rollback all those changes since it still has access to the shelved.patch but at least if you're in the same situation I was, you have the list of all your previously modified files so you can review the changes and add them back manually as needed. Or even go to the file and revert it using local history since multiline changes get messed up with the + and - in the patch file, indicating added or deleted lines.

like image 21
Batman Avatar answered Sep 20 '22 18:09

Batman