git: Switch branch and ignore any changes without committing

后端 未结 15 2131
别跟我提以往
别跟我提以往 2020-11-28 00:37

I was working on a git branch and was ready to commit my changes, so I made a commit with a useful commit message. I then absentmindedly made minor changes to the code that

相关标签:
15条回答
  • 2020-11-28 00:59

    switching to a new branch losing changes:

    git checkout -b YOUR_NEW_BRANCH_NAME --force
    

    switching to an existing branch losing changes:

    git checkout YOUR_BRANCH --force
    
    0 讨论(0)
  • 2020-11-28 01:02

    If you want to discard the changes,

    git checkout -- <file>
    git checkout branch
    

    If you want to keep the changes,

    git stash save
    git checkout branch
    git stash pop
    
    0 讨论(0)
  • 2020-11-28 01:03

    Note that if you've merged remote branches or have local commits and want to go back to the remote HEAD you must do:

    git reset --hard origin/HEAD
    

    HEAD alone will only refer to the local commit/merge -- several times I have forgotten that when resetting and end up with "your repository is X commits ahead.." when I fully intended to nuke ALL changes/commits and return to the remote branch.

    0 讨论(0)
提交回复
热议问题