How to stash my previous commit?

前端 未结 5 1717
感动是毒
感动是毒 2021-01-30 08:29

I\'ve got the following situation on my git log:

commit 111  <-- need to push it to the repository

commit 222  <-- need to stash this one

..         


        
5条回答
  •  醉话见心
    2021-01-30 08:38

    An alternative solution uses the stash:

    Before:

    ~/dev/gitpro $git stash list
    
    ~/dev/gitpro $git log --oneline -3
    
    * 7049dd5 (HEAD -> master) c111
    * 3f1fa3d c222
    * 0a0f6c4 c333
    
    1. git reset head~1 <--- head shifted one back to c222; working still contains c111 changes
    2. git stash push -m "commit 111" <--- staging/working (containing c111 changes) stashed; staging/working rolled back to revised head (containing c222 changes)
    3. git reset head~1 <--- head shifted one back to c333; working still contains c222 changes
    4. git stash push -m "commit 222" <--- staging/working (containing c222 changes) stashed; staging/working rolled back to revised head (containing c333 changes)
    5. git stash pop stash@{1} <--- oldest stash entry with c111 changes removed & applied to staging/working
    6. git commit -am "commit 111" <-- new commit with c111's changes becomes new head

    note you cannot run 'git stash pop' without specifying the stash@{1} entry. The stash is a LIFO stack -- not FIFO -- so that would incorrectly pop the stash@{0} entry with c222's changes (instead of stash@{1} with c111's changes).

    note if there are conflicting chunks between commits 111 and 222, then you'll be forced to resolve them when attempting to pop. (This would be the case if you went with an alternative rebase solution as well.)

    After:

    ~/dev/gitpro $git stash list
    
    stash@{0}: On master: c222
    
    ~/dev/gitpro $git log -2 --oneline
    
    * edbd9e8 (HEAD -> master) c111
    * 0a0f6c4 c333
    

提交回复
热议问题