Sometimes I want to quickly save some code changes that I am not ready to commit into a real branch. For example, I might be in the middle of a long refactoring and there is a complete mess that I do not want to show up in version control history:
Git has the “stash” command which removes all uncommitted changes and saves them to a local data structure where they can be retrieve later. However, this comes at the disadvantage that stashed changes only live in your local environment – they cannot be synced to tools like GitHub or GitLab.
This is fine for small changes, but when you really need to make sure to keep your changes, there is a more permanent solution that also does not pollute your version control history.
Save Changes
You save your changes by committing them to a “dirty”, temporary branch (let’s call it temp_refactor
):
git checkout -b temp_refactor
git add .
git commit -m "some trash commit"
git push
Your changes are now reflected in the temp_refactor
branch and can be pushed to a remote repository.
Load Changes
When you are ready to continue, change to the branch you actually want your changes to end up at and do the following:
git checkout feature/my_normal_feature_branch
git checkout temp_refactor -- .
All your changes are now back as unstaged changes and you can continue your work.
Leave a Reply