git checkout carries unstaged files to the new branch

后端 未结 3 601
感动是毒
感动是毒 2021-01-24 17:24

I have been trying to set-up repository with gitlab CE, as a part of the set-up created a repo and been playing around with it, when I encountered, that after I make some modifi

3条回答
  •  青春惊慌失措
    2021-01-24 17:48

    git checkout carries unstaged files to the new branch

    Its not a bug.

    The is how git behave.


    In the following diagram you can see the 3 states.

    Git has three main states that your files can reside in:

    • committed
    • modified
    • staged

    They are all shared between your branches. but when you checkout branch you change the HEAD so you end up with the staging area && working directory shared between your repository even when you checkout branches.


    How to checkout different branch with clean working directory and stage area?

    If you wish to checkout clean branch without any "leftovers" in your working directory and staging are you can create a new worktree which will result in shared view of your repository (all the content is shared) but with a different working directory and staging area.


    From git v2.5

    git worktree add 
    

    Now do whatever you want in any of your branches. It will create 2 separate working folders separated from each other while pointing to the same repository.

    Using wortree you don't have to do any clear or reset in order to remove all your staged and untracked content.

    Here is demo of how to do it:


提交回复
热议问题