Error when changing to master branch: my local changes would be overwritten by checkout

后端 未结 6 1977
难免孤独
难免孤独 2020-11-30 19:20

This question is similar to this one, but more specific.

I have a project with two branches (staging and beta).

I develop

6条回答
  •  一向
    一向 (楼主)
    2020-11-30 19:52

    This error happens when the branch you are switching to, has changes that your current branch doesn't have.

    If you are seeing this error when you try to switch to a new branch, then your current branch is probably behind one or more commits. If so, run:

    git fetch
    

    You should also remove dependencies which may also conflict with the destination branch.

    For example, for iOS developers:

    pod deintegrate
    

    then try checking out a branch again.

    If the desired branch isn't new you can either cherry pick a commit and fix the conflicts or stash the changes and then fix the conflicts.

    1. Git Stash (recommended)

    git stash
    git checkout 
    git stash apply
    

    2. Cherry pick (more work)

    git add 
    git commit -m "Your message"
    git log
    

    Copy the sha of your commit. Then discard unwanted changes:

    git checkout .
    git checkout -- . 
    git clean -f -fd -fx
    

    Make sure your branch is up to date:

    git fetch
    

    Then checkout to the desired branch

    git checkout 
    

    Then cherry pick the other commit:

    git cherry-pick 
    

    Now fix the conflict.

    1. Otherwise, your other option is to abandon your current branches changes with:
    git checkout -f branch
    

提交回复
热议问题