git stash -> merge stashed change with current changes

后端 未结 8 2267
故里飘歌
故里飘歌 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:59

    As suggested by @Brandan, here's what I needed to do to get around

    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
    

    Follow this process:

    git status  # local changes to `file`
    git stash list  # further changes to `file` we want to merge
    git commit -m "WIP" file
    git stash pop
    git commit -m "WIP2" file
    git rebase -i HEAD^^  # I always use interactive rebase -- I'm sure you could do this in a single command with the simplicity of this process -- basically squash HEAD into HEAD^
    # mark the second commit to squash into the first using your EDITOR
    git reset HEAD^
    

    And you'll be left with fully merged local changes to file, ready to do further work/cleanup or make a single good commit. Or, if you know the merged contents of file will be correct, you could write a fitting message and skip git reset HEAD^.

    0 讨论(0)
  • 2020-12-12 14:03

    May be, it is not the very worst idea to merge (via difftool) from ... yes ... a branch!

    > current_branch=$(git status | head -n1 | cut -d' ' -f3)
    > stash_branch="$current_branch-stash-$(date +%yy%mm%dd-%Hh%M)"
    > git stash branch $stash_branch
    > git checkout $current_branch
    > git difftool $stash_branch
    
    0 讨论(0)
提交回复
热议问题