How and when to perform a git clone depth 1 example
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 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:
- 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; and
- 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.
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 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.