In a previous post I had written out about the init command and what it does. This time around I wanted to focus a little more on what the clone command does and how it functions. This is one that is probably more known and more frequently used by many, and was one of the first commands I learned to use. At least I thought I had. There are a few things that I didn’t know at the time which have helped so much over the last three years.

To be honest I hadn’t really thought about reading the documentation until I got curious about how git really worked under the hood. I tried to read the doc page for the command but really only glossed over it. Biggest reason being there wasn’t too much going on and there really isn’t a great deal with the command.

The part that really most intrigued me was in how that command actually works internally. The processes that are run was what really drew my attention. I think it was the almost simplicity of what happens when you run git clone and then watch things happen before your eyes. At the basic level it does only four things but it’s inside of those that other steps are taken as well. The core steps are:

  • git init
  • git remote add origin [url]
  • git fetch --all
  • git checkout --track origin/main

Super simple when you look at, right? It makes sense that it would do those all of those things for us. The one take away for me was when reading the documentation there really are quite a few options that you can pass when cloning. Having the ability to clone a local repo can be useful in some cases. The first one that comes to mind would be when you have several changes that you know should be working but aren’t so you clone the repo down again to start over again. So rather than having to download things all over again it can be copied over which is what the clone command can do.

Again, this was something I found super neat and it’s because the curiosity does often times get the best of me. For that reason I have been trying to read the source code more and more. I need to find the time to read the Derrick Stolee’s series he wrote out here:

It is really well written and I did learn more because of it. For now I’ll stick to reading the documentation and some of the source code to get a better understanding. Hopefully this helps somebody else down the line.


One response to “How the clone command does magic”

  1. I am very excited to keep up with you and your journey through git 🙂 Very easy to read and comprehend – keep up the great work Jose!