Switch to another branch without changing the workspace files

前端 未结 8 1434
深忆病人
深忆病人 2021-01-30 06:26

I cloned a git repository from GitHub, made some changes and some commits; I made quite a lot and all are quite dirty, so they\'re not suitable for a pull request. Now I created

8条回答
  •  感情败类
    2021-01-30 07:08

    Why not just git reset --soft ?

    Demonstration:

    mkdir myrepo; cd myrepo; git init
    touch poem; git add poem; git commit -m 'add poem'  # first commit
    git branch original
    echo bananas > poem; git commit -am 'change poem'  # second commit
    echo are tasty >> poem  # unstaged change
    git reset --soft original
    

    Result:

    $ git diff --cached
    diff --git a/poem b/poem
    index e69de29..9baf85e 100644
    --- a/poem
    +++ b/poem
    @@ -0,0 +1 @@
    +bananas
    $ git diff
    diff --git a/poem b/poem
    index 9baf85e..ac01489 100644
    --- a/poem
    +++ b/poem
    @@ -1 +1,2 @@
     bananas
    +are tasty
    

    One thing to note though, is that the current branch changes to original. You’re still left in the previous branch after the process, but can easily git checkout original, because it’s the same state. If you do not want to lose the previous HEAD, you should note the commit reference and do git branch -f after that.

提交回复
热议问题