Search code examples
gitgithubgit-clonegit-checkoutgit-tag

Checkout a tag from a private GitHub repository


I need to clone a private repository from GitHub, but I only want to get a specific tag (so basically, cloning is actually the wrong term for it).

Now, the problem is that there are multiple options, and all of them don't really work out:

  • GitHub offers tagged versions as archives, but they are not accessible via curl or wget (at least I could not figure out how).
  • GitHub does not support archiving repositories.
  • I could run a git clone and then run a git checkout to get to the version specified by the tag, but then I download more than I need, I am in detached head state, and all the remaining stuff stays on disk. Of course, I could clean this up manually, but … well, lots of work for a trivial task.

What is the best way to achieve what I want to do?

Update

I think my question was not clear enough, hence I'm adding some more information: What I want is not only get to the revision marked by a tag, but I also want to remove the entire history. Basically, as if Git never existed, and all I ever had was this one single version of my code. Any hints?


Solution

  • I think you can do this with git clone --single-branch:

           --[no-]single-branch
               Clone only the history leading to the tip of a single branch, either
               specified by the --branch option or the primary branch remote’s HEAD
               points at. When creating a shallow clone with the --depth option, this
               is the default, unless --no-single-branch is given to fetch the
               histories near the tips of all branches. Further fetches into the
               resulting repository will only update the remote-tracking branch for the
               branch this option was used for the initial cloning. If the HEAD at the
               remote did not point at any branch when --single-branch clone was made,
               no remote-tracking branch is created.
    

    Note that this says you need a branch to be specified with --branch, rather than a tag. However, the documentation for --branch says:

           --branch , -b 
               Instead of pointing the newly created HEAD to the branch pointed to by
               the cloned repository’s HEAD, point to  branch instead. In a
               non-bare repository, this is the branch that will be checked out.
               --branch can also take tags and detaches the HEAD at that commit in the
               resulting repository.
    

    The last sentence says you can use --branch with a tag. The only thing I'm not sure of is whether you can both use --single-branch and pass a tag to --branch. I guess you will have to try that to confirm. Alternatively, you will have to create a branch in the remote repository as opposed to a tag.

    Update

    You now say you also want to destroy the entire history. Do this afterwards.

    Two ways:

    Living dangerously:

    git clean -xdf  # Clean out everything not in git
    rm -rf .git     # remove git
    git init .      # put it back
    git add .       # Add all the files
    git commit -a -m "Eternal sunshine of the spotless mind"
    

    Living less dangerously

    git rebase --root -i # needs recent version of git
    

    then change every line to begin with s to squash into the original commit.

    Also see How to squash all git commits into one?