Switching a branch after aborting current changes in git

前端 未结 9 1386
情书的邮戳
情书的邮戳 2021-01-30 17:00

I cloned a git repo and then started playing around in its master branch. After a while, I want to ignore the changes I just made (without committing them), and switch to a diff

相关标签:
9条回答
  • 2021-01-30 17:06

    You can discard changes you made in a specific file:

    git checkout somefile.txt
    

    And then jump smoothly to the branch:

    git checkout gh-pages
    
    0 讨论(0)
  • 2021-01-30 17:07

    Blockquote Option 2 did not work for me, it just repeats the error.

    The same for me. After git reset --hard + git checkout I was still getting the same error.

    I have fix it by adding --force on second command, right after hard reset.

    git reset --hard
    git checkout -f <branch name>
    
    0 讨论(0)
  • 2021-01-30 17:08

    You can ignore all uncommitted changes.

    git reset --hard HEAD

    0 讨论(0)
  • git add -A git stash

    this will clear all the changes you made (Only those that are not commited)

    0 讨论(0)
  • 2021-01-30 17:13

    Option 1

    git checkout -f gh-pages
    

    Option 2

    git reset --hard     # beware: don't make that a habit
    git checkout gh-pages
    
    0 讨论(0)
  • 2021-01-30 17:24

    If you have unstaged file, try:

    git checkout -- .
    

    Or

    git checkout -- filename
    
    0 讨论(0)
提交回复
热议问题