CodeBlanks Logo Icon CodeBlanks

git archive

Learn to create a repository archive with git archive.

What is git archive?

git archive exports files from a specific commit or branch in a Git repository as a single compressed archive file (tar or zip). Unlike cloning or copying, it does not include the .git history directory in the archive.

git archive --output=proje.tar HEAD

This gives you a clean snapshot of just the source code, ideal for distribution to clients or deployment servers.

Archive Output and Target File

The --output (or -o for short) option is used to specify the archive output. Git can automatically recognize the archive format from the file extension.

git archive -o proje.tar HEAD
git archive --output=kaynak.tar.gz HEAD

Additionally, if the --prefix=proje/ flag is added, files are extracted inside a subfolder when uncompressed.

Practice Create HEAD Archive

Archive the latest commit (HEAD) in the repository with the --output=surum.tar parameter. Then list the archive contents using tar -tf surum.tar.

Starter code

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "v1.0.0" > surum.txt
git add surum.txt
git commit -q -m "Ilk surum"

# HEAD arsivini olustur ve icerigini listele:

Sample solution

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "v1.0.0" > surum.txt
git add surum.txt
git commit -q -m "Ilk surum"

git archive --output=surum.tar HEAD
tar -tf surum.tar

Expected output

surum.txt

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main difference between <code>git archive</code> and a plain folder copy?

  • It excludes .git history and produces a clean snapshot Correct answer
  • It archives all branches simultaneously
  • It automatically deletes the remote repository
  • It only exports the .git directory

Explanation: `git archive` excludes the `.git` directory and commit history, creating a clean snapshot package of only the current files.

2 Archive the HEAD Commit

Question
git archive --output=yedek.tar ____
Answer
git archive --output=yedek.tar HEAD

Explanation: HEAD is specified as the target reference to archive the content of the latest commit.

3 Archive Basics

Term Meaning
git archive Packages repo snapshot
HEAD Current commit to archive
--output Specifies output file path
.git klasörü Not added to archive file

All lessons in Advanced