Git keep staged changes

How do I keep my staged changes without committing and work on other issue’s where these changes are not required ?

I have created a branch say b1 and made some changes and staged that changes. Now I had an urgent issue which I need to work on but I don’t require the staged changes here. So I switched to branch b2 and fetched and pulled from upstream. But the staged changes still exists in the b2 branch

  • Git merges all the time
  • Simplify git routine 'fast cherrypick to another branch'
  • Detect when the remote tracked branch has been force updated in Git
  • Removing duplicate db migrations from git repository
  • Pushing files from one Git repository to another
  • NetBeans git init ignore directories dist and build even if they are not in .gitignore
  • Adding retrospective tags in Git?
  • TortoiseGit trouble: git did not exit cleanly (exit code 128)
  • Git merge and resolve any conflicts to current branch
  • Pushing from git into a subversion repository
  • Why does git cherry-pick not continue?
  • Merge branch to a different branch from the starting branch
  • 3 Solutions collect form web for “Git keep staged changes”

    Do not stage, but stash the changes. You can then later apply them again to your working copy:

    git stash
    git checkout -b urgent-issue
    git commit
    git checkout -
    git stash pop
    

    An alternative to the answer given by @knittl is to actually go ahead and make a temporary commit (keep reading):

    git commit -m 'WIP'
    git checkout urgent-issue
    

    Once you have committed your work, your working directory will be clean and you can switch branches as you need. When you want to return to your current branch, you can continue the work and amend the temporary commit you made.

    git checkout feature   # return to original branch
    # work work ... complete the commit
    git commit --amend -m 'Finished the work I started earlier'
    

    I usually prefer making a temporary commit over a stash when I have critical work for several reasons. First, it eliminates the need to have to sort through the stash stack looking for the stash I want to apply (and it easy to let the stack pile up with old junk). Second, because I know that I have a commit with unfinished work, it forces discipline on me to complete the feature.

    # No need to stage
    # put your current changes in stash
    git stash
    
    # create and checkout to b1 branch
    git checkout -b b2
    
    # resolve urgent issue and push changes 
    
    # now get back to branch b1
    git checkout b1
    
    # Apply stashed changes back
    git stash pop
    
    Git Baby is a git and github fan, let's start git clone.