CodeBlanks Logo Icon CodeBlanks

Specific branch

Learn to archive a specific branch.

Archiving Branches

git archive is not limited to HEAD by default. You can package files from any branch by specifying its name at the end of the command.

git archive -o ana-dal.tar main
git archive -o test.tar staging

You can easily export a snapshot of any branch without switching your current working branch.

Archiving Tags and Subdirectories

Along with branch names, Git tags or commit references can also be used. It is also possible to export only a specific subdirectory of the repository.

git archive -o v1.0.tar v1.0.0
git archive -o belgeler.tar main docs/

This allows you to isolate and package only release documents or library components from large repositories.

Practice Archive a Specific Branch

You are currently on the gelistirme branch. Without switching your branch, archive the kararli branch with --output=kararli.tar kararli. Then list the contents with tar -tf kararli.tar.

Starter code

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "kararli surum" > kararli.txt
git add kararli.txt
git commit -q -m "Kararli commit"
git branch kararli

git checkout -q -b gelistirme
echo "yeni kod" > test.txt
git add test.txt
git commit -q -m "Gelistirme calismasi"

# Hedef dali arsivle ve listele:

Sample solution

terminal
git init -q
git config user.name "Dev"
git config user.email "dev@example.com"
echo "kararli surum" > kararli.txt
git add kararli.txt
git commit -q -m "Kararli commit"
git branch kararli

git checkout -q -b gelistirme
echo "yeni kod" > test.txt
git add test.txt
git commit -q -m "Gelistirme calismasi"

git archive --output=kararli.tar kararli
tar -tf kararli.tar

Expected output

kararli.txt

Questions and answers

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

1 What should be done to archive the <code>main</code> branch while on the development branch?

  • Only HEAD can be archived, branch names are not allowed
  • Pass main as the target reference to git archive Correct answer
  • Run git checkout main first, then git clone
  • Delete the development branch and clone again

Explanation: `git archive` accepts any commit, tag, or branch name as reference; you can run `git archive -o out.tar main` without switching branches.

2 Archive the v2 Branch

Question
git archive --output=surum2.tar ____
Answer
git archive --output=surum2.tar v2

Explanation: The target branch name is added directly as a reference argument at the end of the command.

3 Reference Types

Term Meaning
main Snapshot of the main branch
v1.0.0 Archive of the tagged release
HEAD Reference to the latest commit
main docs/ Archives a specific subfolder

All lessons in Advanced