Git allowing me to switch branches without committing changes

后端 未结 5 792
遥遥无期
遥遥无期 2021-02-03 22:40

I am just learning Git, going through a tutorial. I am in branch seo_title and I have uncommitted changes to file mission.html. I did git checkout master expecting

5条回答
  •  遥遥无期
    2021-02-03 23:14

    The different behaviour you saw from the last time you tried to switch branches with local changes and now is due to different file changes.

    So, let's say we have a branch called 'readme' where you have committed some changes to a file, let's say README.md.

    Now, you have switched back to master. You do some work on other files (not README.md). Now you have local changes. If you try to switch back to the 'readme' branch without committing your changes, it will let you. Why? Because switching to the 'readme' branch won't override any of your local changes.

    If, however you make a modification to the README.md file on the master branch, then when you try to do a

    git checkout readme 
    

    you will encounter

    error: Your local changes to the following files would be overwritten by checkout: README.md
    Please, commit your changes or stash them before you can switch branches.
    

    because you have changes to README.md that would require a merge.

提交回复
热议问题