Single working branch with Git

前端 未结 7 1151
予麋鹿
予麋鹿 2021-01-01 15:21

Bounty short description

Is there a portable way to use a single repository w/ multiple checkouts? As an alternative to ha

相关标签:
7条回答
  • 2021-01-01 16:09

    If I understand correctly, you are looking for the ability to CopyOut a specific branch head or commit on a branch, onto an independent directory, for investigation and comparison purposes, while still having your current development/bugfix branch still checked out as your main git 'reference'.

    You expect that the independently 'copied out' directory would be unlikely to be re-committed directly to git.

    An untested approach would be to use the base git command with its [--git-dir=<path>] [--work-tree=<path>] options, and to, through a careful script, adjust/correct your HEAD / Index as you do the CopyOut and step back to your current branch.

    The usual caveats apply..

    1. Create new work_dir
    2. Start git pointing to work_dir
    3. Save current HEAD
    4. Checkout required branch/commit (it should go into work_dir)
    5. Set HEAD back to saved value (This is a hack and may not work without steps to keep the index right!)
    6. close git to 'forget' the work_dir setting
    7. restart git back in your old directory and its associated branch

    This would definately need careful testing, and probably needs adjustment around steps 3 & 5, such that step 6-7 is true.

    The git work-tree option could give a mechanism for bring in changes from the copyout directory if it had bug fixes that needed committing into the appropriate branch.

    UPDATE --------------------------------
    The clunx instructions are based on a Windows user's view.

    # this is run from your main work dir (that contains .git)
    Copy .git/HEAD MyBackup
    # HEAD will contain the line similar to "ref: refs/heads/master"
    mkdir <path>
    # the path should be 'somewhere else', not in your existing directory
    # If the path doesn't exist the following checkout will fail
    Git --work-tree=<path> checkout <branch>
    # this will extract the latest commit on that branch to the work tree path
    # or use a commit sha1 instead of <branch>, if you want an exact commit
    # Note: your index will be updated to reflect the checked out files, as will HEAD.
    Copy MyBackup .git/HEAD
    # set the HEAD back to where it was
    Git reset
    # reset the index appropriate for the previous Head.
    # note we dropped the --work-tree parameter
    

    A similar technique can be used to grab any revisions from the extracted path by pointing --work-tree at the right place at the right time and making sure the index is correctly set. Don't forget the usual caveats [backup, backup, and backup].

    This worked for me on a test repo I have. Some of the actions were in Windows.

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