0
0
Gitdevops~5 mins

Shallow clones with depth in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you only need the latest part of a project history, not everything. Shallow clones let you copy just the recent changes from a Git repository. This saves time and space when you don't need the full history.
When you want to quickly get the latest version of a project without downloading all past changes.
When your internet connection is slow and you want to save bandwidth by downloading less data.
When you are working on a small fix and don't need the full project history.
When running automated builds or tests that only need the current code state.
When you want to save disk space on your computer by avoiding the full repository history.
Commands
This command copies the Git repository but only downloads the latest commit. The --depth 1 flag tells Git to make a shallow clone with just the most recent history.
Terminal
git clone --depth 1 https://github.com/git/git.git
Expected OutputExpected
Cloning into 'git'... remote: Enumerating objects: 1000, done. remote: Counting objects: 100% (1000/1000), done. remote: Compressing objects: 100% (800/800), done. Receiving objects: 100% (1000/1000), 2.5 MiB | 1.2 MiB/s, done. Resolving deltas: 100% (500/500), done.
--depth 1 - Limits the clone to the latest commit only, making it shallow.
Change directory into the cloned repository to start working with the files.
Terminal
cd git
Expected OutputExpected
No output (command runs silently)
Shows the commit history in a short format. Since this is a shallow clone, you will see only one commit.
Terminal
git log --oneline
Expected OutputExpected
e83c516 Initial commit
--oneline - Shows each commit in one line for easy reading.
This command fetches more history, increasing the depth to 5 commits. It adds more recent commits to your shallow clone.
Terminal
git fetch --depth=5
Expected OutputExpected
From https://github.com/git/git * [new ref] refs/heads/master -> origin/master Updating e83c516..a1b2c3d Fast-forward ...
--depth=5 - Increases the shallow clone depth to include 5 commits.
Check the commit history again. Now you will see up to 5 recent commits because of the increased depth.
Terminal
git log --oneline
Expected OutputExpected
a1b2c3d Fix typo in README 9f8e7d6 Add new feature 7d6c5b4 Update docs 4c3b2a1 Improve tests e83c516 Initial commit
--oneline - Shows each commit in one line for easy reading.
Key Concept

If you remember nothing else from this pattern, remember: shallow clones let you save time and space by downloading only recent commits instead of the full history.

Common Mistakes
Cloning without the --depth flag when only recent history is needed.
This downloads the entire repository history, wasting time and space.
Always add --depth 1 or another depth number to limit the clone size.
Trying to push changes from a shallow clone without fetching full history.
Git may reject pushes because the shallow clone lacks full commit context.
Fetch full history or unshallow the clone before pushing changes.
Assuming shallow clones have all branches and tags.
Shallow clones may miss some branches or tags because they only fetch limited commits.
Fetch specific branches or tags explicitly if needed after cloning.
Summary
Use git clone --depth 1 to get only the latest commit and save time and space.
Check your shallow clone history with git log --oneline to see limited commits.
Increase depth later with git fetch --depth=N to get more history if needed.