jkj2000
jkj2000

Reputation: 1593

Best practice for applying a Git stash with conflicts?

I've never had a problem applying a stash before, but I think this upcoming session may be different. I was wondering if it's better to pull the latest from your origin, then apply the stash and deal with any conflicts, or apply the stash first, and then pull the latest from the origin? Any best practices one way or the other?

Upvotes: 2

Views: 154

Answers (1)

helmbert
helmbert

Reputation: 37994

There are multiple solutions to this. What's always worked best for me is creating a new branch from your stash. You can then work with that stash like any other branch that got somehow diverged.

  1. Start by creating a new branch from your stash:

    git stash branch <branch-name> <stash-name>
    

    Git will then checkout the commit on which the stash was originally created and then apply the stash (since the stash is applied on it's original commit, it is likely that you will not get any conflicts).

  2. Create a new commit from the changes in the new branch.

    git add .
    git commit -m 'This commit contains the changes from the stash'
    
  3. Resolve conflicts like you would with any other merge commit. Possible options would be merging the new branch or rebasing it on top of the current branch (and solving conflicts along the way).

    1. git checkout master && git merge <branch-name> or
    2. git rebase master

Upvotes: 2

Related Questions