Git stash can be used to save the local modifications to the working directory away and revert to a clean state.
Here are some sample commands to illustrate how git stash can be used in an imaginary scenario where two users attempt to update the same file simultaneously and push to the remote repository but the first user suceeds and the second user notices a new update already in the remote repository preventing him from pushing his update without resolving the conflict:
# Let's create a git bare repository (works as a remote repo)
# Linux
mkdir repo
# Windows
md repo
cd repo
git init --bare
# Let's say user1 clones the repo
cd ..
git clone repo user1
# Let's say user1 adds a file
cd user1
# Add file test.txt (Use a text editor to create an example file)
git add .
git commit -m "User1"
git push
# Let's say user2 clones repo
cd ..
git clone repo user2
# Now, user1 and user2 have identical content.
# Let's assume that both user1 and user2 edit the same file.
# Let's also assume that user1 was quick to push an update to the remote.
cd user1
# Edit test.txt - use a text editor
git add .
git status
git commit -m "User1 new update"
git push
# Let's say that in the meantime, user2 also makes a change and attempts to push an update (pull, commit, push)
cd ../user2
# Edit test.txt - use a text editor. Edit the same link as user1 to illustrate the issue easily.
# Let's say user2 is ready to commit and push. Perform a pull as the first step.
git pull
# Now you might see that pull fails.
# Keep aside local changes using git stash
# -u: Stash untracked files as well
git stash -u
git stash list
# Check status and note that the working directory is clean
git status
# Now that local changes are stashed away, git pull will run smoothly updating local content from remote repository.
git pull
# Let's get the stashed away content back
git stash pop
# Resolve conflicts now.
# If everything is OK, stage, commit and push.
git status
git add test.txt
# Commit and push
git status
git commit -m "User2 update"
git push
# If you want to delete what's in stash
git stash drop
git stash list