Git- How to pull on a dirty workingtree

Git is a Distributed Version Control System. It is becoming very popular between developers thanks to its simplicity and speed.

Common version control operation, like commit changes, merge incoming changes or create different branches are typically very simple and not painful actions in git. However there can be some particular situations that become a little bit tricky to resolve.

One of these can be the following: imagine that you are working on a new feature on the local copy of the repository, at a certain point you asked to resolve an issue on the code. Maybe your local copy of the repository is not updated with remote version because while you are working on you feature, someone of your team has done some commits; so to update your local repository, you will run:

But you will receive an error, something like:

This happens because some file you are working on, not yet commited, have some conflicts with the commits that you are pulling from remote: in other words someone of your team modified the same file you are modifyng.

There are many solutions to avoid this problem. For example you can commit your change before pulling remote commits.

But maybe you don’t have finished  your work and you don’t want to still commit. In this case you can use an interesting git feature: the stashing (see in git doc).

By stashing you current work before pulling, you tell to git

Save my uncommitted changes in a stack, and revert my workingtree to last commit state.

Then, once you have stashed your work you can pull or even switch branch, do your fix or whatever you want, commit it and then get your previously stashed changes from the stack and continue the work on your new feature.

The commands to stash changes and then restore them are simple and self-explanatory: before pull or switch branch you can run the command

When you have finished with fixes or other, you can simply get your work back by running:

The stash command have a lot of options you can use. For detailed explanation of the stash behavior see official git documention.