Git Init: A Walk Through How to Create a New Repo
Git is one of the major players when it comes to technology. It appeared back in 2005, when I was still a kid and didn’t know a thing about computers. Maybe you can relate to that. But as we grew with time and (hopefully) matured, so did Git.
Git has proven itself to be an extremely effective solution for simplifying version control. It is mostly (but not exclusively) used for software development. Git stores a bunch of changes made to the working tree and unites them all in a node called commit. Commit after commit, we start noticing real change in our repository, which lists the files being tracked by Git in a specific project.
If we start thinking a little bit philosophically, everything that exists had to start somewhere, right? The same is true of Git repositories. Today we’ll discuss the creation of a repository and the command that does it: Git Init.
So, Git Init Starts a Repository. Tell Me More!
As previously and philosophically mentioned, Git Init is the command that boots a repository off the ground. It has the power to start it and set it up with some basic configurations. Like everything else, you will only need to create a repository once in each project; therefore, you’ll only need to run this command once as well.
Behind the scenes, Git Init creates a .git folder in the root directory of your project. That folder is hidden; therefore, you’ve probably never seen it in your projects. It indicates both that that project folder is a Git repository and that it contains files with all the information about the repository. Fair warning: Unless you know what you’re doing, do not touch any file of that folder in any specific way. It’s better to pretend that the folder does not exist at all.
There are a few ways to create a repository:
Using the git init command (with or without its flags)
Creating a remote repository using GitHub, Bitbucket or other server first and cloning it locally
Copying and pasting an existing folder that is a Git repository and changing those specific settings
How Should I Create a Repository, Then?
All right! Let’s start from the bottom of that list and make our way to the top. Then we can explore the Git Init command in more detail.
Copying and Pasting? A Template?
The copying and pasting approach is the most dangerous one. I would suggest you only use it if you want to keep two versions of the same project in your machine. Just keep in mind that both versions will be attached to the same remote repository. If you just want to copy all code from this repository and start a new project with it, you should be looking for the git init --template. Read along to find out more about it.
The main difference between copying and pasting and the git init --template approach is simple: The git init --template approach will create a brand-new repository locally, with the possibility for you to connect to any remote repository that you specify. All files and directories will be copied unless they are hidden (hidden files’ and directories’ names start with a dot).
Remote Repository First
Creating a remote repository first is one of the best options to create a repository because most servers show a nice step-by-step guide on how to set up things locally. From this point, you have four options:
You already have a local repository and you just need to connect them both. In this case, check the ...or push an existing repository from the command line section in the image below.
You don’t have a local repository and you want to create it on your own. In this case, check the ...or create a new repository on the command line section in the image.
Maybe you just want to get going, and what is done is fine. In this case, you can use git clone <URL the server gave you>, and there’s a brand-new repository connected to the one in your machine.
You can import the code from another remote repository. This option might not be available on other servers. In GitHub’s case, it will prompt you to introduce a URL to another Git repository and it will do the heavy lifting for you. You’ll have a brand-new copy of the other repository you just copied in the remote repository you’re in. Now, you just need to clone it using the git clone command.
Git Init Ramifications
The git init approach has some of its own ramifications:
You can just type git init in your command line and let it create the repository on its own. It’s quite easy and simple, but then you’ll need to connect it on your own to a remote repository.
You can name the default branch while starting the repository. It comes as a default of master or main, but you can use git init -b <branch name> to override it and name it whatever you want to.
You can also choose to copy a different local repository you have on your machine. That can be achieved using the command git init --template=”.path/to/your/project” (keep the double quotes). This will create a new repository in which all non-hidden files and directories will be copied from the source project to the destinations while creating a new repository locally with no remote attachments.
Is There Anything Else I Should Be Aware of While Using Git Init?
I’m glad you asked! There are a few tips for you to take into consideration while using git init:
If you want to run a simple git init command in an already initialized repository, it won’t change anything you already have there. If you did it accidentally, don’t worry. Nothing changed.
You can add an option parameter at the end of the command to specify where you want the command to run. The default value is . and an example could be this: git init ./my-app. In this case, the repository will be started inside the folder my-app instead of the one you’re in.
If you have two versions of the same project in your machine, remember they won’t sync with each other. This is the same as having two projects on two separate computers. Make sure this is what you want to do.
Git Init itself might allow you to move the repository to another place if you want to do so. In this case, moving the repository means the .git folder will have a symbolic link to the folder where the repository information actually is. It won’t move your project folder.
Putting It All Together
Like the universe itself, every repository for every project we create has a beginning. Git Init helps us bring our projects to life.
Stay up to date
We'll never share your email address and you can opt out at any time, we promise.