git-clone@ubuntu:~$ git clone --depth 1 https://github.com/cameronmcnz/my-github-repo.git
In the world of continuous integration builds and continuous delivery pipelines, it’s not unusual to require a depth 1 git clone in your DevOps routines.
When you perform a dept 1 git clone operation, the only thing you pull back from the remote repository is the latest commit on your specific git branch of interest. By default, it’s the master branch that gets cloned and checked out, but you can alter this behavior through the use of the –branch switch and providing a branch name after it.
Why use a depth 1 git clone?
A git clone depth 1 operation isn’t something a typical software developer will need to do. When programming, it is useful to have a complete git history, along with the ability to inspect any git branch being used for parallel feature development. 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, and that’s what a depth 1 git clone provides.
How to do a git clone of depth 1
The list of steps to perform a git clone of depth 1 are:
- Copy the clone URL of the remote repository
- Include the –depth 1 switch in the git clone operation
- Optionally specify the name of the branch to clone with the –branch switch
- Run the shallow git clone command in the terminal window
- Perform a git log –oneline operation to verify the clone depth
It’s worth noting that the depth 1 clone operation only pulls done one branch. If the remote repository contains other branches, you will not be able to check them out locally without 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 https://github.com/cameronmcnz/my-github-repo.git develop-folder depth1@ubuntu:~$ git clone --depth 1 --branch hotfix https://github.com/cameronmcnz/my-github-repo.git hotfix-folder depth1@ubuntu:~$ git clone --depth 1 --branch release https://github.com/cameronmcnz/my-github-repo.git release-folder depth1@ubuntu:~$ git clone --depth 1 --branch master https://github.com/cameronmcnz/my-github-repo.git master-folder
The depth 1 git clone is not an operation a software developer will normally have to pull out of their bag of DevOps tricks. But when writing 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 having to run future Git clean up operations.