11

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?

Deqing
  • 14,098
  • 15
  • 84
  • 131
  • Use [`git stash`](http://git-scm.com/book/en/Git-Tools-Stashing), or did I miss something? – Elliott Frisch Jul 10 '14 at 06:10
  • You don't actually need a new branch to do your original plan; just `git checkout HEAD^` – M.M Jul 10 '14 at 06:17
  • possible duplicate: http://stackoverflow.com/q/2944469/1484957 – drs Jul 10 '14 at 13:29
  • possible duplicate of [git: How to create a branch of my current work, but stay on my original branch](http://stackoverflow.com/questions/13917636/git-how-to-create-a-branch-of-my-current-work-but-stay-on-my-original-branch) – random Jul 11 '14 at 03:24

2 Answers2

13

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.

Elwinar
  • 9,103
  • 32
  • 40
  • 1
    But git stash pop could cause merge conflict. I don't want to merge, I just want to all modify restore to working index. – nuclear Jan 20 '21 at 08:32
7

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.
Mureinik
  • 297,002
  • 52
  • 306
  • 350