Bird
Raised Fist0
Gitdevops~10 mins

Shallow clones with depth in Git - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Shallow clones with depth
Start: git clone command
↓
Check for --depth option
↓
Clone limited
↓
history only
↓
Create local repo with limited commits
↓
End
The git clone process checks if a depth option is given. If yes, it clones only recent commits up to that depth, creating a shallow clone. Otherwise, it clones the full history.
Execution Sample
Git
git clone --depth 3 https://github.com/example/repo.git
This command clones only the last 3 commits from the remote repository, creating a shallow clone.
Process Table
StepActionDepth OptionCommits ClonedResult
1Start clone command--depth 3N/APrepare to clone repo
2Check for depth option--depth 3N/ADepth option found: 3
3Fetch commits--depth 33Only last 3 commits fetched
4Create local repo--depth 33Local repo created with shallow history
5Clone complete--depth 33Shallow clone ready
💡 Clone stops after fetching 3 commits due to depth limit
Status Tracker
VariableStartAfter Step 2After Step 3Final
depthundefined333
commits_fetched0033
local_repo_stateemptyemptypartial historypartial history
Key Moments - 3 Insights
Why does the clone only have 3 commits instead of the full history?
Because the --depth 3 option limits the clone to the last 3 commits, as shown in execution_table step 3 where only 3 commits are fetched.
Can you push changes from a shallow clone?
Yes, but some operations may be limited because the history is incomplete. The shallow clone has only recent commits (execution_table step 4), so pushing might require fetching more history first.
What happens if you omit the --depth option?
The clone fetches the full commit history (execution_table step 2 'No' branch), resulting in a complete local repository.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, how many commits are fetched?
A0 commits
BAll commits
C3 commits
D1 commit
💡 Hint
Refer to the 'Commits Cloned' column at step 3 in the execution_table.
At which step does git confirm the depth option is used?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Check the 'Action' and 'Depth Option' columns in execution_table step 2.
If the --depth option was removed, how would the 'Commits Cloned' column change?
AIt would show 3 commits
BIt would show all commits
CIt would show 0 commits
DIt would show 1 commit
💡 Hint
Without depth, git clones full history as explained in key_moments and concept_flow.
Concept Snapshot
git clone --depth <n> <repo_url>
- Clones only the last n commits
- Creates a shallow clone with limited history
- Faster and smaller download
- Some git operations may be limited
- Omit --depth for full clone
Full Transcript
When you run git clone with the --depth option, git checks this option and fetches only the last specified number of commits from the remote repository. This creates a shallow clone with limited history, which is faster and uses less space. The execution table shows step-by-step how git processes the command: starting the clone, detecting the depth option, fetching only the last 3 commits, creating the local repository with partial history, and completing the clone. Variables like depth and commits_fetched track these changes. Beginners often wonder why the history is limited or if they can push changes; the key moments clarify these points. The visual quiz tests understanding of commit counts and steps where depth is recognized. Remember, omitting --depth clones the full history.

Practice

(1/5)
1. What does the --depth option do when used with git clone?
easy
A. It compresses the repository to save disk space.
B. It clones all branches including remote ones.
C. It creates a shallow clone with limited commit history.
D. It initializes a new empty repository.

Solution

  1. Step 1: Understand the purpose of --depth

    The --depth option limits the number of commits cloned, creating a shallow copy.
  2. Step 2: Compare with other clone types

    Unlike a full clone, a shallow clone saves time and space by copying recent commits only.
  3. Final Answer:

    It creates a shallow clone with limited commit history. -> Option C
  4. Quick Check:

    Shallow clone = limited history [OK]
Hint: Remember: --depth limits commit history depth [OK]
Common Mistakes:
  • Thinking --depth clones all branches
  • Confusing shallow clone with compression
  • Assuming it creates an empty repo
2. Which of the following is the correct syntax to clone a repository with a depth of 5 commits?
easy
A. git clone --depth=5 https://example.com/repo.git
B. git clone -depth 5 https://example.com/repo.git
C. git clone --depth 5 https://example.com/repo.git
D. git clone --deep 5 https://example.com/repo.git

Solution

  1. Step 1: Recall correct git clone syntax for depth

    The correct syntax uses --depth with an equals sign or a space and the number, like --depth=5 or --depth 5. Both are valid.
  2. Step 2: Identify incorrect options

    git clone -depth 5 https://example.com/repo.git uses a single dash which is invalid. git clone --deep 5 https://example.com/repo.git uses an invalid flag --deep.
  3. Final Answer:

    git clone --depth=5 https://example.com/repo.git -> Option A
  4. Quick Check:

    Correct syntax = --depth=5 or --depth 5 [OK]
Hint: Use --depth=5 or --depth 5 for shallow clone [OK]
Common Mistakes:
  • Using '=' instead of space after --depth (both valid but some prefer equals)
  • Typing -depth instead of --depth
  • Using --deep instead of --depth
3. What will be the output of the following commands?
git clone --depth 2 https://example.com/repo.git
cd repo
git log --oneline
medium
A. Shows commits from all branches.
B. Shows the full commit history.
C. Shows no commits because depth is too low.
D. Shows only the last 2 commits in the log.

Solution

  1. Step 1: Understand shallow clone effect on history

    Using --depth 2 clones only the last 2 commits, so git log shows those.
  2. Step 2: Analyze git log --oneline output

    This command lists commits in short form, limited to the shallow clone's depth.
  3. Final Answer:

    Shows only the last 2 commits in the log. -> Option D
  4. Quick Check:

    Depth 2 clone shows 2 commits [OK]
Hint: Shallow clone limits log to depth commits [OK]
Common Mistakes:
  • Expecting full history in shallow clone
  • Thinking no commits show if depth is low
  • Assuming all branches' commits appear
4. You ran git clone --depth 3 https://example.com/repo.git but later need the full history. What should you do to fix this?
medium
A. Run git fetch --unshallow inside the cloned repo.
B. Delete the repo and clone again without --depth.
C. Run git clone --depth 0 to get full history.
D. Run git pull --depth=full to fetch all commits.

Solution

  1. Step 1: Understand how to convert shallow clone to full

    Inside the shallow clone, running git fetch --unshallow downloads the full commit history.
  2. Step 2: Evaluate other options

    Deleting the repo and cloning again without --depth works but is inefficient. Options C and D use invalid syntax or commands.
  3. Final Answer:

    Run git fetch --unshallow inside the cloned repo. -> Option A
  4. Quick Check:

    Use git fetch --unshallow to get full history [OK]
Hint: Use git fetch --unshallow to complete shallow clone [OK]
Common Mistakes:
  • Trying to reclone instead of fetching full history
  • Using invalid git pull options
  • Assuming --depth 0 is valid
5. You want to clone a large repository but only need the last 10 commits of the main branch and no other branches. Which command achieves this efficiently?
hard
A. git clone --depth=10 https://example.com/repo.git
B. git clone --depth 10 --single-branch --branch main https://example.com/repo.git
C. git clone --branch main https://example.com/repo.git
D. git clone --shallow-since=10days https://example.com/repo.git

Solution

  1. Step 1: Identify options to limit clone depth and branches

    Use --depth 10 to limit commits and --single-branch --branch main to clone only main branch.
  2. Step 2: Check other options for correctness

    git clone --depth=10 https://example.com/repo.git clones all branches shallowly. git clone --branch main https://example.com/repo.git clones full history of main. git clone --shallow-since=10days https://example.com/repo.git uses invalid flag.
  3. Final Answer:

    git clone --depth 10 --single-branch --branch main https://example.com/repo.git -> Option B
  4. Quick Check:

    Depth + single branch = efficient clone [OK]
Hint: Combine --depth with --single-branch for focused clone [OK]
Common Mistakes:
  • Not using --single-branch clones all branches
  • Using invalid flags like --shallow-since
  • Assuming --depth=10 clones only main branch
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard