Cheat sheet: How to switch GIT branches with changes


git | en

In case you want to switch to new branch for bugfix.

1. Stash

This one “stashes” changes removing them from currently changed files.

// To create stash
git stash -m "Just like commit messages"

// To restore latest stash
git stash pop
// OR
git stash apply

2. Temporary commit

Just like described, just make commit with message like “TO REVERT”.

I suggest to break syntax as well 😁, just type in any file(not as comment) something like “PLEASE RESET TEMP COMMIT”, so you when you will switch back to this branch you will see an error that will remind you(hopefully) of your temp commit.

// Commit your changes
git commit -m "TEMP COMMIT, TO REVERT"

// Revert changes so you can work on files again
git reset --soft HEAD~1 // this will undo 1 latest unpushed commit and move this to changed files

3. Worktrees

I haven’t used this one, this one allows to have several branches checked out in different folders with some safeguards built in.

Can’t explain this better than there