How can I git archive an arbitrary branch?

git checkout master
git archive stage | tar -czf archive.tar.gz htdocs
# archives master because it's checked out.

How can I archives the stage branch, regardless of the current I’m on?

  • How to handle with GitFlow and Subtasks
  • Git: Merge fork to main repo as a new branch
  • Why is jGit's RevWalk.reset() method misbehaving?
  • Phabricator hosted repository returning error 500 on clone
  • Resolving same conflicts again and again when doing rebasing more times
  • Maven Release Plugin Pushing Tags To Wrong Repo
  • Clone a git repo (in depth)
  • Migration from SVN to GIT due to approval mechanism
  • Protecting git deployment-specific files
  • Re-indexing repository in eclipse Ubuntu
  • GitLab Continuous Deployment to Azure Web App
  • Git rebase A-B-C-D: Parent A has child B and child C, and both B&C are parents of D
  • One Solution collect form web for “How can I git archive an arbitrary branch?”

    git archive creates a tar archive so you don’t need to pipe its output tar, indeed it’s not doing what you expect. You are creating an tar archive of the stage branch and piping it to a normal tar command which doesn’t use its standard input but just creates its own archive of htdocs in the working tree, independently of git.

    Try:

    git archive stage >stage.tar
    

    or, for a compressed archive:

    git archive stage | gzip >stage.tar.gz
    

    To just archive the htdocs subfolder you can do:

    git archive stage:htdocs | gzip >stage-htdocs.tar.gz
    

    or, to include the folder name in the archive:

    git archive --prefix=htdocs/ stage:htdocs | gzip >stage-htdocs.tar.gz
    

    or more simply:

    git archive stage htdocs | gzip >stage-htdocs.tar.gz
    
    Git Baby is a git and github fan, let's start git clone.