Git Archives

Nilesh Parab
5 minute Written by Nilesh Parab on
Git Archives Photo by Catarina Carvalho on Unsplash

Commands you need when an archive is to be shared with someone either online or offline.

Offline

Usually we used to zip the folder which would then complain while unzip with long path error. This happens because because of hidden .git folder & related files which are also zipped.

Path too long

:punch: Enter ‘git archive’

archive is useful to create an archive from your local / remote repository.

git archive --format=zip --output=archive-20201128 HEAD

Online

I ran into a case where my colleage needed codebase to start work but did not have an Git account yet. A solution was to share my credentials but then that does sound good.

:rainbow: Personal Access Tokens

So rather sharing Git credentials, I created a Personal Access Token with only the access I wanted to give using roles. The token could then be used with Git commands or REST API.

I tried this with GitHub but other providers like BitBucket, GitLab also support these.

Here is what you need to do:

  • Login to your GitHub account
  • Navigate to Settings -> Developer Settings -> Personal access tokens
  • Click Generate new token
  • Add a good name to your token that could also indicate purpose
  • Select the roles you want (GitHub explains briefly each one of those)
  • Click Generate token
  • Copy the generated token (Note: this is the only time you will see it, else you need to create new)
  • You can update the roles later or delete the token if not needed
  • You can then select respective GitHub API to do your task, like I needed an archive of my code
    • I did not set any specific role to the token
    • I referred GitHub REST API to download archive
    • Then passed the token to Authenticate API request via Personal access tokens
    • This is the command I finally ran:
      curl -L -u <username>:<personal-token> -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/<org-or-username>/<repository>/zipball/<branch> > <output-zip-archive>.zip
      
  • You can use the personal token as a password as well

:warning: Keep assigned roles to mininum & remember to delete the tokens if you are not using them

Nilesh Parab
About Nilesh Parab I love building things using Java & JavaScript. Professionally I am more focussed in eCommerce. Music. Motorcycling.