Mellisa Lee
Mellisa Lee

Reputation: 41

git stash to save current changed file

I made a couple of changes in branch A. For some reason I have to stop developing this feature in branch A. I have another task for which I need to create a new branch. I know that by doing git stash and pop, I can save the current changes and restore it later while I can do whatever I want in the newly created branch. My concern is that is it safe to do so? I have more than 20 files been changes and I don't want to lose anything.

Upvotes: 1

Views: 383

Answers (3)

Rahul Gole
Rahul Gole

Reputation: 31

Yes it is safe to use stashcommand here

To be doubly sure about your work you can use

git stash apply

Instead of using git stash pop you can use git stash apply . What git stash pop does is that it throws away the stash after using it. But git stash apply keeps the stash in the stash list for later use if required. And in case you want to remove that most recent stash, you can do it by git stash drop.

Upvotes: 0

Tim Jarvis
Tim Jarvis

Reputation: 18815

Yes - its perfectly safe, and a very common thing to do.

Its also probably almost as easy to just create a branch with your changes, branching in git is a very lightweight operation, and will probably give you a little more flexibility than a stash.

For me personally I always will create a branch for work that will eventually get pushed - and I'll stash things that are essentially throwaway - This is just my personal take on it, your milage may/will vary.

Upvotes: 1

theNelsen
theNelsen

Reputation: 61

git stash should be considered safe; it will store the uncommited / unpushed files locally and allows you to get ready for making additional changes on branch A. The stash is stored locally, so there's a risk of dead computer = dead stash.

Consider if it would be appropriate to commit / push, then move to a different branch. If you're working on something that can be separately deployed, it makes more sense to commit and push to branch A. If you want to take the current code with you into branch B (to deploy the previous code and the new code to be written on branch B as one unit), then do the stash.

Upvotes: 2

Related Questions