How to Make a Shallow Copy of a Git Repository Without History

  • Post category:Snippets

Sometimes you want to start a new project using the current state of an existing repository, but you don’t want to bring along all the commit history. This is called making a “shallow copy” of a repository. Here’s how you can do it in a few simple steps—including how to create a new GitHub repository for your fresh start.


Why Make a Shallow Copy?

  • You want a clean slate for a new project.
  • You don’t need the original commit history.
  • You want to reduce repository size for a quick start.

Steps to Create a Shallow Copy

1. Create a New Repository on GitHub

  • Go to GitHub and click the + icon in the top right.
  • Select New repository.
  • Enter a repository name and description.
  • Choose visibility (public or private).
  • Do not initialize with a README, .gitignore, or license (leave these unchecked).
  • Click Create repository.

2. Shallow Clone the Latest State Of The Repository You Want To Copy

Use the --depth 1 flag to clone just the latest commit:

git clone --depth 1 https://github.com/USERNAME/REPO_NAME.git
Bash

This gives you the latest files, but not the full history.


3. Remove the Git History

Navigate into the cloned directory and delete the .git folder:

cd REPO_NAME

rm -rf .git
Bash

Now, your directory is just a regular folder with the latest code.


4. Initialize a New Git Repository

Set up a new Git repository and connect it to your new GitHub repo:

git init

git add .

git commit -m "Initial commit"

git branch -M main

git remote add origin https://github.com/YOUR_USERNAME/NEW_REPO_NAME.git

git push -u origin main
Bash

(Optional) Make the New Repository a Template

If you want others to use your new repository as a template:

  • Go to your new repository on GitHub.
  • Click Settings > General.
  • Check the Template repository box.

Now, anyone can use the “Use this template” button to create their own copy.