Git merge while discarding any previous changes(In local repository or from the last commit)

后端 未结 3 1637
闹比i
闹比i 2021-01-23 11:43

In Git, during a merge, is there a way that we can tell git to discard local changes in case of a conflict and apply the changes from the merged branch?

I mean if there

相关标签:
3条回答
  • 2021-01-23 12:24

    It sounds like you want to read about the merge strategies 'theirs' and 'ours'. When merging you can specify that either your current branch (ours) or the remote branch (theirs) is the correct one.

    0 讨论(0)
  • 2021-01-23 12:24

    If you want to ignore all local changes, and an additional merge commit you want to just move your branch to the remote HEAD.

    git log --oneline origin/master
    # assume the first sha is bbdfa17
    git reset --hard bbdfa17
    

    Now you are at the tip of the tree with no merge commits.

    0 讨论(0)
  • 2021-01-23 12:32

    Before trying to merge, you can discard the local changes yourself git reset --hard HEAD.
    You can replace HEAD by whatever commit hash you want.
    This will bring you the the clean state of the commit you're actually on, and you'd lose all your changes.

    If you want to keep them, you can stash them before with git stash, or move them to another branch:

    git checkout -b new_branch
    git add .
    git commit -m "My awesome commit"
    git checkout - # will bring you back to the last branch you were in
    
    0 讨论(0)
提交回复
热议问题