Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - temporarily save current work

Tags:

git

I'm adding a feature on master branch. After changing several files, I realize that I need to check the output without my changes.

The way I can think of is:

  1. Commit current changes
  2. Check out and switch to a new branch
  3. In the new branch: git reset --hard HEAD^ so that I get back to the original code.
  4. Try the code and when I'm done, switch back to master branch and delete this new branch.

Is there a better way for this?

Can I save a snapshot of current changes and come back later when I am ready?

like image 826
Deqing Avatar asked Jul 10 '14 06:07

Deqing


Video Answer


2 Answers

You can simply use git stash, which will store away your change to the local working tree. Then do your things, and when you're done, git stash pop to get things back. See the documentation about stashing.

like image 113
Elwinar Avatar answered Sep 23 '22 15:09

Elwinar


This is exactly what stashing is designed for.

Basically:

  1. Use git stash save to save your changes in a side location (i.e., the stash)
  2. Check whatever you want, fix if needed, and commit
  3. Use git stash pop to return your changes to the working directory.
like image 39
Mureinik Avatar answered Sep 26 '22 15:09

Mureinik