News Stay informed about the latest enterprise technology news and product updates.

How and when to perform a git clone depth 1

git-clone@ubuntu:~$ git clone --depth 1

In the world of continuous integration builds and continuous delivery pipelines, it’s not unusual to require a git clone depth 1 in most DevOps routines.

When developers perform a git clone depth 1operation, the only thing they pull back from the remote repository is the latest commit on the specific git branch of interest. By default, it’s the master branch that gets cloned and checked out. However, developers can alter this behavior with the –branch switch and providing a branch name after it.

Why use a git clone depth 1?

A git clone depth 1 operation isn’t something a typical software developer will need to do. It’s useful for developers to have a complete git history, along with the ability to inspect any git branch used for parallel feature development when programming. But continuous integration builds, and automation scripts rarely need a commit history. They usually only need one commit on a specific branch, along with any associated git submodules. Hence, that’s what a git clone depth 1 provides.

How to do a git clone of depth 1

A developer can perform a depth 1 git clone in five easy steps:

  1. Copy the clone URL of the remote repository;
  2. Include the –depth 1 switch in the git clone operation;
  3. Optionally specify the name of the branch to clone with the –branch switch;
  4. Run the shallow git clone command in the terminal window; and
  5. Perform a git log –oneline operation to verify the clone depth.

Developers should be aware that the depth 1 clone operation only pulls down one branch. If the remote repository contains other branches, they won’t be able to check them out locally without a pathspec error.

git clone depth 1 pathspec

After a git clone depth 1 operation, attempts to checkout an alternate branch will trigger a pathspec error.

Shallow clone to a directory

For continuous integration routines that need to perform a depth 1 git clone for multiple branches at the same time, one option is to clone each branch into a separate folder.

depth1@ubuntu:~$ git clone --depth 1  --branch develop develop-folder
depth1@ubuntu:~$ git clone --depth 1  --branch hotfix hotfix-folder
depth1@ubuntu:~$ git clone --depth 1  --branch release release-folder
depth1@ubuntu:~$ git clone --depth 1  --branch master master-folder

The git clone depth 1 isn’t an operation that a software developer will normally have to pull out of their bag of DevOps tricks. But when a developer writes build scripts or continuous integration pipelines, a shallow git clone of the highest order can really come in handy and relive the DevOps team of running future Git clean up operations.