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

后端 未结 4 641
孤独总比滥情好
孤独总比滥情好 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:20

    You can create a branch at the the specific commit you want to checkout (from the manual page)

    git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
    

    so fill in the <start-point> as the SHA1 you want you new branch <branchname> to start at and you won't get your 'head' detached from an expanding branch.

    There will be other things you probably want to do to keep some of your working files in the state you desire....

    0 讨论(0)
  • 2021-02-18 15:27

    Yes, you can visit any arbitrary revision with "git checkout" as you describe. If you ask for an arbitrary revision rather than a branch, git will not have any obvious way to keep track of what changes you make, though. You can see where you were before by consulting the reflog ("git reflog show") - but in general, you would have been on a branch before so presumably just want to change back to that with "git checkout master" or similar.

    Note that this method won't automatically deal with uncommitted changes in your workspace--- either commit or stash your changes before moving between branches, or use "git checkout -m" to carry them around as you move (and be prepared to deal with merge conflicts if the changes you're carrying aren't trivial).

    I think recent git versions have introduced the shorthand "@{-1}" for "where I was before I last moved", which might actually be what you want in this case. (I haven't used it, just read about it in the release notes).

    0 讨论(0)
  • 2021-02-18 15:39

    If you want Git to show the tip of some line of changes, you need to have it branched or tagged. But even if you do not make a branch, none of the commits is lost. It stays in the repository as a garbage node (i.e. not reachable through any branch or tag), and is removed only some days/weeks later when you do "git gc".

    So if you want to have an easy access to the changes, it's best to create a temporary branch and work there. If you do commits outside a branch and then checkout another branch, then you will need to use the command "git reflog" or look into the logs (".git/logs" directory) to find out what was the hash of the changes that you lost. When you know the hash, you can do "git checkout hash" or "git checkout -b new_branch hash" and recover the tip.

    0 讨论(0)
  • 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 <revision to check out>
    

    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 <old branch name>
    

    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.

    0 讨论(0)
提交回复
热议问题