How to Unshallow a Git Repository & Restore Full History| Sabbirz | Blog

How to Unshallow a Git Repository & Restore Full History

Git unshallow Clone

Git Hacks: Switch Between Shallow and Full Clones Instantly

Weโ€™ve all been there. Youโ€™re racing against a deadline โฐ, or you're stuck on a server with internet speeds from the 90s ๐Ÿข. You need that massive repository right now, so you pull out the oldest trick in the book: the Shallow Clone.

When you eventually need to git blame or merge complex branches, you'll hit a wall. Don't panic! ๐Ÿ˜ฑ You don't need to re-clone. Here is how to correctly perform a shallow clone and "unshallow" it to restore full history without losing your work.


๐ŸŽ๏ธ Step 1: The Need for Speed (How to Shallow Clone)

First things firstโ€”if you haven't done it yet, here is how you create a shallow clone. This is standard practice in CI/CD pipelines (like GitHub Actions or GitLab CI) to save bandwidth and build time.

To clone only the latest commit (head) of a repository, use the --depth flag:

git clone --depth 1 <repository-url>

Why do this?

  • Speed: Downloads only the files you need right now.
  • Space: Saves disk space by ignoring years of commit history.
  • Bandwidth: perfect for slow connections.

But as useful as this is, it creates a "truncated" history. โœ‚๏ธ


๐Ÿ” Step 2: Confriming You Have a Shallow Clone

Not sure if your repo is shallow? Git leaves a tell-tale sign. Run this command in your terminal:

git log

git log command to check if shallow cloned

If you only see one or two commits, look closely at the commit hash. If you see the tag grafted next to the commit ID, you're in a shallow repo.

commit e7fc206b... (grafted, HEAD -> main)

That (grafted) tag is Gitโ€™s way of saying, "I chopped the history off here to save space." It treats this commit as the pseudo-root of your repository.


๐Ÿช„ Step 3: The Magic Fix (How to Unshallow)

So, you need the history back? Maybe you're moving from a quick deployment to full-on development. To restore the history, do not re-clone. ๐Ÿšซ

We just need to ask Git to go back to the server and fetch the missing pieces of the timeline.

Run this simple command:

git fetch --unshallow

git fetch --unshallow command of git

โณ What happens next?

Depending on the size of your project (looking at you, massive monorepos), this might take a while. Git is effectively downloading every commit, branch, and tag that ever existed in the project's history.

Once it finishes, run git log again. The (grafted) tag will be gone, and youโ€™ll see the beautiful, endless timeline of your project. โœจ


๐Ÿ”ง Troubleshooting: "Permission Denied" ๐Ÿ›‘

Sometimes, standard users run into a wall. You might hit this error:

git permission denied error

error: cannot open '.git/FETCH_HEAD': Permission denied

๐Ÿง Why does this happen?

This usually occurs if you originally cloned the repository using sudo (a big no-no for standard development) or if your folder permissions got messed up during a copy operation. Your current user simply doesn't have the keys to the .git kingdom.

๐Ÿ’Š The Fix

You need to reclaim ownership of the folder. Run this command to take back what is yours:

# Replace $USER with your actual username if usually required,
# but most shells handle this variable automatically.
sudo chown -R $USER:$USER .

Now, try the git fetch --unshallow command again. It should work like a charm! ๐Ÿ€


๐Ÿ“š Summary

Shallow clones (--depth 1) are powerful tools for DevOps engineers and developers working with massive codebases like Linux or Chromium. They save time and resources.

But when you need to dig deep, git fetch --unshallow is your best friend. It transforms your lightweight snapshot back into a fully functional version control powerhouse.

Happy Coding! ๐Ÿ’ป๐Ÿš€

Related posts