0

I have a stable master branch, and began working on a dev branch.

Suppose in dev I change a few files, but then want to temporarily switch back to master -- in my case, to run the master code in a debugger to check some code blocks. Then, I'll switch back to dev to continue my work, but in the meantime, I'm not ready to commit dev.

In this case, I thought staging the dev files via git add src/modified_files* before git checkout master would do the trick. But when I compiled the master code, I found that I was mistaken (i.e. the modified files still existed, and were not swapped with the master files).

What is the proper way to do this?

7
  • 1
    git stash. then switch to master. when you switch back to dev run stash pop Commented May 9, 2018 at 19:42
  • git add doesn't change your working tree. It prepares the next commit.
    – axiac
    Commented May 9, 2018 at 19:46
  • 1
    @JeffCharter Perfect. That worked. Suggest making it an answer. Thanks. Commented May 9, 2018 at 19:47
  • 1
    You can safely commit your changes on dev then switch to master. When you switch back to dev, run git reset HEAD~1 and Git restores the status of your repo as it is now. It's better than using a stash because a branch is always visible but a stash can be easily forgotten.
    – axiac
    Commented May 9, 2018 at 19:48
  • Possible duplicate of Git. How to save changes in wrong branch
    – phd
    Commented May 9, 2018 at 20:09

1 Answer 1

1

> git stash

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

git-stash documentation

then, when you're ready to go back,

> git stash pop

Remove a single stashed state from the stash list and apply it on top of the current working tree state

git-stash pop documentation

Not the answer you're looking for? Browse other questions tagged or ask your own question.