Create Git Repository (git init)
What Will We Learn?
What is Git repository, it’s types and how to create your own.
What is Git Repository?
Literally, repository is a place where files are stored in a certain organized way and when you initialize this repository using Git, it introduces more features like history tracking, file retrieval, collaboration, etc. And this can now be called as Git Repository.
Local Repository
The repository you create and initialize in your computer using git init. This means local repository exists in your own machine which allows you to work offline and commit changes. This distributed nature of Git makes you more powerful as you never need central repo or machine to be online just to work locally.
Remote Repository
Generally, the copy of your local repository stored in another machine accessible via internet can be called as remote repository. And more often than not, remote repository is hosted on platforms like GitHub, GitLab, Bitbucket, etc. powered by Git. While working solo, you may not need remote repository but for collaborative and team projects, it’s essential and almost mandatory. Also you can keep backup of your local repository in these hosting platforms for free. So, it’s not bad idea you create one today.
Creating Git Repository
Creating Local Repository
Nothing complicated on creating Git repository. Go to the root of your project and just initialize with git init:
cd your_project_directory
git initWarning
Git creates a hidden .git directory which is your local repository used by Git to track things. You should never edit it manually in any serious project unless you are Linus Torvalds — the creator of Git.
Creating Remote Repository
The exact steps to create remote repository is platform specific. GitHub is the most popular one. You should check the docs of the platform you choose to host your project.
What’s Hidden Inside .git Directory?
You may be curious:
What treasure is hidden inside
.gitdirectory that you warned not to touch it?
If you list the contents of .git directory (using ls -l), you can see something similar:
# stripped for readability
-rw-r--r-- HEAD
-rw-r--r-- config
-rw-r--r-- description
drwxr-xr-x hooks
drwxr-xr-x info
drwxr-xr-x objects
drwxr-xr-x refsIn short:
HEAD: points to current branch.
cat HEADgives usref: refs/heads/masterwhich means current branch ismaster.config: is the configuration specific to this local repository.
description: is description of your repository.
hooks: contains scripts which are triggered before or after majour Git events like
git commit, etc.info: contains a single file named
excludeby default which acts like a private.gitignorewhich is never versioned and pushed.objects: stores Git Objects
refs: stores references to commits