Search code examples
gitcherry-pickgit-cherry-pick

Undo local changes caused by failed git cherry-pick -n <hash>


I'm trying to get several commits from my dev branch into my uat branch using "git cherry-pick -n hash" and then use just one git commit/push to send them to uat.

Let's suppose I have 5 commits I want, so I checkout and pull my uat branch and starts to cherry-pick from dev. The first 4 cherry-picks are successful, but the 5th fails with merge conflicts. How do I return to the previous state (after the 4th "git cherry-pick -n 4th_hash" ?

"git reset --merge" undo all changes (including the first 4 ones that I want). "git cherry-pick --abort" says that there is no cherry-pick in progress.

Is there some way to accomplish this? I'm trying to create a bash script to do it automatically.


Solution

  • I am not sure how you want to do it automatically in a script if you have conflicts. But regardless to it. If you want to save previous state I suggest you remove the -n flag. So perform:

    git cherry-pick <hash>
    

    For each of the commits you want, and once you done squash them together.

    git reset --soft HEAD~5 
    git commit
    

    Where 5 is the number of cherry-picks you just did. So it can work just the same with a script.