git: Switch branch and ignore any changes without committing

git: Switch branch and ignore any changes without committing

Asked on January 10, 2019 in Git.
Add Comment


  • 4 Answer(s)

    Switch branch and ignore any changes without committing

    Anyother way ,

    Answered on January 10, 2019.
    Add Comment

    To discard the changes,

    git checkout -- <file>
    git checkout branch
    

    To keep the changes,

    git stash save
    git checkout branch
    git stash pop
    
    Answered on January 10, 2019.
    Add Comment

    Here the alternative given below code:

    git stash save
    git checkout branch
    // do something
    git checkout oldbranch
    git stash pop
    
    Answered on January 10, 2019.
    Add Comment

    You need a clean state to change branches. The branch checkout will only be allowed if it does not affect the ‘dirty files’ (as Charles Bailey remarks in the comments).

    Otherwise, you should either:

    • stash your current change or
    • reset --hard HEAD (if you do not mind losing those minor changes) or
    • checkout -f (When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. )
    Answered on February 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.