Git allowing me to switch branches without committing changes

后端 未结 5 791
遥遥无期
遥遥无期 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:30

    Git will let you checkout other branches (or tags or SHA1 hashes) as long as the commit you're changing your work-tree to does NOT clobber your local uncommitted changes.

    In your case the branch master's work-tree would have had the same version of missing.html as that currently exists in the tip of your current branch you were switching from. Git does not need to touch the working copy (for missing.html at least) when changing branches here, and hence lets you keep your local modifications.

    If you indeed tried to checkout a commit where there was a different version of missing.html in the work-tree (than the one committed in your current branch), git would show an error message similar to this:

    $ git checkout some-other-branch
    error: Your local changes to the following files would be overwritten by checkout:
            missing.html
    Please, commit your changes or stash them before you can switch branches.
    Aborting
    

提交回复
热议问题