git stash -> merge stashed change with current changes

后端 未结 8 2266
故里飘歌
故里飘歌 2020-12-12 13:27

I made some changes to my branch and realized I forgot I had stashed some other necessary changes to said branch. What I want is a way to merge my stashed changes with the

相关标签:
8条回答
  • 2020-12-12 13:37

    Running git stash pop or git stash apply is essentially a merge. You shouldn't have needed to commit your current changes unless the files changed in the stash are also changed in the working copy, in which case you would've seen this error message:

    error: Your local changes to the following files would be overwritten by merge:
           file.txt
    Please, commit your changes or stash them before you can merge.
    Aborting
    

    In that case, you can't apply the stash to your current changes in one step. You can commit the changes, apply the stash, commit again, and squash those two commits using git rebase if you really don't want two commits, but that may be more trouble that it's worth.

    0 讨论(0)
  • 2020-12-12 13:51

    What I want is a way to merge my stashed changes with the current changes

    Here is another option to do it:

    git stash show -p|git apply
    git stash drop
    

    git stash show -p will show the patch of last saved stash. git apply will apply it. After the merge is done, merged stash can be dropped with git stash drop.

    0 讨论(0)
  • 2020-12-12 13:52

    tl;dr

    Run git add first.


    I just discovered that if your uncommitted changes are added to the index (i.e. "staged", using git add ...), then git stash apply (and, presumably, git stash pop) will actually do a proper merge. If there are no conflicts, you're golden. If not, resolve them as usual with git mergetool, or manually with an editor.

    To be clear, this is the process I'm talking about:

    mkdir test-repo && cd test-repo && git init
    echo test > test.txt
    git add test.txt && git commit -m "Initial version"
    
    # here's the interesting part:
    
    # make a local change and stash it:
    echo test2 > test.txt
    git stash
    
    # make a different local change:
    echo test3 > test.txt
    
    # try to apply the previous changes:
    git stash apply
    # git complains "Cannot apply to a dirty working tree, please stage your changes"
    
    # add "test3" changes to the index, then re-try the stash:
    git add test.txt
    git stash apply
    # git says: "Auto-merging test.txt"
    # git says: "CONFLICT (content): Merge conflict in test.txt"
    

    ... which is probably what you're looking for.

    0 讨论(0)
  • 2020-12-12 13:53

    Another option is to do another "git stash" of the local uncommitted changes, then combine the two git stashes. Unfortunately git seems to not have a way to easily combine two stashes. So one option is to create two .diff files and apply them both--at lest its not an extra commit and doesn't involve a ten step process :|

    how to: https://stackoverflow.com/a/9658688/32453

    0 讨论(0)
  • 2020-12-12 13:57

    you can easily

    1. Commit your current changes
    2. Unstash your stash and resolve conflicts
    3. Commit changes from stash
    4. Soft reset to commit you are comming from (last correct commit)
    0 讨论(0)
  • 2020-12-12 13:59

    The way I do this is to git add this first then git stash apply <stash code>. It's the most simple way.

    0 讨论(0)
提交回复
热议问题