Alex Aparin
Alex Aparin

Reputation: 4512

What are the real benefits of stash command?

I came across a stash command in git and I really don't understand benefits of such command.

Suppose I created some temporary changes in foo_branch, and I want to checkout on bar_branch. My usual practise is to create commit with temp commit message in foo_branch, and then perform checkout on bar_branch. Then if I want to come back to foo_branch, I perform checkout and discard last commit (preserving changes) of foo_branch, then I continue to work.

In my opinion, stash command is just another way to do such thing.

What are the real benefits of stash command? Maybe is it just shortcut for such case?

Upvotes: 3

Views: 696

Answers (5)

Omar Essam El-Din
Omar Essam El-Din

Reputation: 1873

Commit is performed in two steps staging and actual commit

where Staging is like holding changes and storing them in Git before committing the changes

Staging really important incase when you made some changes and you still don't wanna commit it now and want to checkout to specific branch so this changes will deleted then we have to stage it (store locally in Git to commit Later)

Upvotes: 1

Jake Henningsgaard
Jake Henningsgaard

Reputation: 702

Here's a visual diagram of the lifecycle status of your files:

Git file lifecycle

Upvotes: -1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

I normally stash changes that I need often, and I don't want to redo them by hand every time.

Use Case: hardcoded api credentials for internal use. I want to use credentials of my user, so I change them once and stash the change. Next time I check out a branch and need to do some api queries, I just apply the stash. (yeah-yeah, credentials should be in env vars, I know).

Or you could do the same for, say, debug printing in certain places. They'll never let you commit that. But chances are, you'll need it again.

Upvotes: 1

piotrwest
piotrwest

Reputation: 2166

There is nothing wrong with your workflow. In git you can achieve many things using different set of commands.

IMO you can see stash as a commit made locally, without being attached to a particular branch. That is, in a situation when you started work you can stash your changes, fetch new changes, update your branch, make some changes, commit them and play around with git repository freely, and then, pop your saved work from stash. Or you could even pop your work to a different branch.

Upvotes: 2

Sven Marnach
Sven Marnach

Reputation: 601431

The stash command can save the current state of the index, the working dir and of untracked files. It's just a lot quicker to run git stash than it is to decide what changes to add to the staging area and commit with a commit message.

Moreover, a stash can be applied in a different position than where it was initially created. You could do that by temporarily commiting on the current branch, switching to a different branch, cherry-picking your commit, and then removing it from where it was originally created.

It's just a quick way of getting a clean working directory if you need to do something else.

Upvotes: 2

Related Questions