Search code examples
gitgit-stash

Is it possible to combine stash pops in Git?


I'm working with Git on a project, and for reasons I don't need to get in to, I'm finding that I may have to make some changes to the code, then stash them as opposed to committing them. I may need these changes sometimes, but not all the time and don't necessarily want them to be permanent. However, the job could be big one and I'd like to try to save myself some problems.

Suppose I save multiple git stashes so my stash looks like this:

stash@{0}: First change
stash@{1}: Second change
...
stash@{10}: Last and final change

Is there a way to pop all of these changes in a single command? Or perhaps combine them together into something like stash@{11}: All previous changes together?

Or is this even a good idea at all?

EDIT: After some research, I realized that I really just should be branching and not using git stash in this way. Thanks for the answers!


Solution

  • Or, maybe make a branch? Branches are cheap in Git and won't be pushed to a remote repo unless you tell Git so. Example in practice.

    Create a branch named "my-hidden-feature" :

    git checkout -b my-hidden-feature
    

    Edit and commit things (for instance, a local configuration file) :

    touch test.config               # Create a new file
    git add test.config             # Add the new file
    git commit -m "Test commit"     # Commit your hidden feature
    

    Return to your original code and do some work :

    git checkout master             # Return to the main branch
    ...work here...                 # Do some work
    git commit -am "Work on master" # Commit your work
    

    Oh, are you in need of the hidden feature? Easy :

    git checkout my-hidden-feature  # Go to your hidden branch
    git rebase master               # Update the hidden feature with new commits from master
    ...do some stuff...             # Do some work on the hidden branch
    git commit -am "A new hidden commit"
    

    And, on and on!

    Ah, last but not least. If you're working from the command line, I highly recommend tools like oh-my-zsh that have a plugin that can display the current branch in your terminal. Very, very useful if you do not want to get lost in Git limbo.