git stash save local changes

Git Stash: Save Local Changes Without Commit in Git

Sometimes you may need to save local progress without commit or keep local changes without commit, in git. You can do this easily using git stash command. Here is how to save local changes without commit in git.


Git Stash: Save Local Changes Without Commit in Git

Here is how to save local changes without commit in Git. Just issue git stash command as shown below from your present working directory which contains unsaved unchanges.

$ sudo git stash

This will stash all changes you made and your repository will be restored to your last commit.

Now if you want to add back these changes, then run git stash apply.

$ sudo git stash apply

The above command will add back all the saved changes without committing them but also retain the stash content.

Also read : How to Convert Webpage into PDF in Python

Please note, you will still need to run git commit, if you want to commit the changes. git stash apply only restores the saved changes and does not commit them.

Also, please note that if you switch your branch after stashing your changes and then issue a git stash apply command then all the changes will be added to your new branch and not the old one.

Here’s an example. Let us say you are in dev1 branch, make some changes, switch to dev2 branch and run stash apply command.

$ /home/project (dev1)
$ sudo git stash
$ sudo git checkout dev2
$ /home/project (dev2)
$ sudo git stash apply

In this case, the stashed changes will be added to branch dev2 and not dev1.

Also read : How to Convert CSV to JSON in NodeJS

If you want to view the contents of your git stash, then run git stash list command.

$ sudo git stash list

As you can see it is really easy to save changes without committing them in git. But please be careful to apply stashed changes to the right branch.

Also read : How to Set Upstream Branch in Git

Leave a Reply

Your email address will not be published.