Do you need to create a branch to check out a specific git revision?

后端 未结 4 646
孤独总比滥情好
孤独总比滥情好 2021-02-18 14:45

A common thing I\'d like to do is revert my working copy to a particular revision, do some testing, and then bring it back to the head of my current master. In the past I have

4条回答
  •  醉话见心
    2021-02-18 15:40

    Assuming you're already on a branch (which you always should be for changes you want to keep), you can just do

    git checkout 
    

    This will take you off the topic branch you were working on into (no branch), in which the working copy refers directly to a commit ID rather than a branch name as normal.

    Then to move back, simply:

    git checkout 
    

    A helpful way to think of it is this: git checkout never alters branches; it merely changes what your working copy is currently looking at (ie, HEAD), which might either be a branch (in which case commits will update the branch) or a random commit hash.

    As such, as long as the changes you want to keep are on a branch, you don't have to worry about git checkout losing them.

提交回复
热议问题