Let’s say you worked on a wrong branch (which happens all the time to me), and now you want to copy those changes to the correct feature branch.
git actually has that covered with simple
git stash command.
Documentations for the stash is pretty clear:
git stashwhen you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the
In other words, changes made on the current branch will be cleared, and stored in the stash. You can see what’s in the stash with
git stash list. If you’re not certain what’s in the stash you can inspect it with
git stash show.
Most common workflow would look like this:
git stash git checkout correct-feature-branch git stash apply
However, if you created new files on a dirty branch, then you’ll have to add extra flag to
git stash, i.e.
git stash --include-untracked.