git: Switch branch and ignore any changes without committing

后端 未结 15 2061
别跟我提以往
别跟我提以往 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:41

    None of these answers helped me because I still had untracked files even after reset and stash. I had to do:

    git reset --hard HEAD
    git clean -d -f
    
    0 讨论(0)
  • 2020-11-28 00:43

    Close terminal, delete the folder where your project is, then clone again your project and voilá.

    0 讨论(0)
  • 2020-11-28 00:44

    If you have made changes to files that Git also needs to change when switching branches, it won't let you. To discard working changes, use:

    git reset --hard HEAD
    

    Then, you will be able to switch branches.

    0 讨论(0)
  • 2020-11-28 00:45

    Easy Answer:

    is to force checkout a branch

    git checkout -f <branch_name>
    

    Force checking out a branch is telling git to drop all changes you've made in the current branch, and checkout out the desired one.

    or in case you're checking out a commit

    git checkout -f <commit-hash>
    


    "thought that I could change branches without committing. If so, how can I set this up? If not, how do I get out of this problem?"

    The answer to that is No, that's literally the philosophy of Git that you keep track of all changes, and that each node (i.e. commit) has to be up-to-date with the latest changes you've made, unless you've made a new commit of course.


    You decided to keep changes?

    Then stash them using

    git stash
    

    and then to unstash your changes in the desired branch, use

    git stash apply
    

    which will apply you changes but keep them in the stash queue too. If you don't want to keep them in the stash stack, then pop them using

    git stash pop
    

    That's the equivalent of apply and then drop

    0 讨论(0)
  • 2020-11-28 00:46

    To switch to other branch without committing the changes when git stash doesn't work. You can use the below command:

    git checkout -f branch-name

    0 讨论(0)
  • 2020-11-28 00:47

    git checkout -f your_branch_name

    git checkout -f your_branch_name
    

    if you have troubles reverting changes:

    git checkout .
    

    if you want to remove untracked directories and files:

    git clean -fd
    
    0 讨论(0)
提交回复
热议问题