Setup Your Git Repository Init

Setup Your Git Repository Init

Setup Your Git Repository Init

A simple way of setting up your Git repository

Photo by Jason Ng on Unsplash

This article is a continuation of my other article about the list of Git commands that I use almost on a daily basis. In this article, I would like to deep dive into the one Git command that you will encounter a lot when you have just started your fresh project.

The hint is actually already on the title, which is git init. If you have read my other article about the list, you would have a rough idea what this command is all about. However, if you haven’t, perhaps you might one to read it for a broader perspective. If you just got started with Git yet you don’t know a thing about it because you were so eager to jump straight into your new project and heard about Git a lot, you are still at the right place. Just stay calm and keep reading on.


The Git Init

A Git repository is a storage place for your project that can be run with Git commands. You can do a lot of things with the commands. However, in order to initiate the creation of the repository, you would need the git init command. This command can also be used to reinitialize an existing one. Additionally, you can also just initialize your existing project directory.

Basically, the git init command converts your directory into a Git repository. How do you know when it is (re)initialized? You will see .git sub-directory created. It will also automatically create a new branch called the “master” branch. To check the branch name, simply run git branch or read my other article about it. :D

The .git sub-directory contains all of the necessary Git metadata for the new repository. This metadata includes sub-directories for objects, refs, and template files. A HEAD file is also created which points to the currently checked out commit.

Don’t be overwhelmed yet, as this command is simple to use despite all the theories above. Here are the steps:

  1. Create your folder (if you don’t have any intended project directory yet).
  2. Navigate to it (if you haven’t done so).
  3. Initialize your folder.

And voila! It is done. Now you have your local repository. Alright, here is the example:

$ mkdir myNewProject
$ cd myNewProject/
$ git init

Then, spot your .git directory! Really simple.

Otherwise, you can also use this command to automatically create a directory and initialize it subsequently:
 $ git init [NEW_DIRECTORY]

Example:

$ git init NewProject

A new “NewProject” directory will be created under your current working directory and then a .git subdirectory will be created inside it.

From now on, you can just run your terminal and type the Git commands and they will all work magically just like that. However, one thing to note is that you have to navigate into the directory with the initialized Git. If you are outside of the folder and try to run the Git command, it won’t work. So, always ensure that you are in the initialized repository before you run all your Git commands.

P.S. There are flags or options that come with this command like -q or --quiet or --bare. You can read more about them on the Git documentation itself, or you can run the help command (I mentioned it briefly on my other article too ;)). However, to get started, git init itself suffices.

If you really want to change your default branch name (which is “master” by default), you can use this flag:
--initial-branch=<branch-name>

Example:

For the normal one:
 $ git init --initial-branch=trunk

This will create your initial default branch as “trunk” instead of “master”.
 There is also a short form for it:
 $ git init -b trunk

If you are sick of the “master” default branch and just want to set other names by default every single time, you can go to set its init.defaultBranch config to set this up:

$ git config --global init.defaultBranch trunk

With this, all your new repos would be created with the initial default branch “trunk” instead of “master”. Usually people are fine with just “master”. Who knows you are not.


Conclusion

For this command, everybody would have encountered it first hand whenever they want to start using any of the Git commands. The reason being one directory needs to be initialized first before other commands can be used! This command in particular, however, is very simple and straight-forward. So, don’t worry about it.

That’s it for this article. I will probably write more about Git commands as I understand how it feels when a newbie just gets started and doesn’t know where to look at, and I would like to help you all to ease into the use of Git commands so that you feel less overwhelmed.

So, if you have anything more to add or you spot something that I am missing, feel free to comment down below. If you feel this article can help other people to learn, feel free to share for them to learn too!
 
 Thanks for reading and keep on learning~

You can support me by tipping me (optional) through: Medium link

Or directly by clicking on either of these:

Ko-fi (recommended):

Get me a coffee:

Buy Me A Coffee

Comments

Popular posts from this blog

10 Best Practices for Naming REST API Endpoints

9 HTTP Methods You May Want To Know

4 Ways To Perform Python String Formatting