git init command: cancel, main, --bare --template options
This article covers a number of topics related to the git init
command.
We'll cover a brief description of what's in the .git
folder that git init
creates, changing the default main
branch, the --bare
and --template
options, and even how to undo an initialization.
Since Git is one of the pillars of modern programming, I'll post one article per major topic, starting with git init
.
1. What is git init?
The git init
command is short for git initialize, and is used to start versioning a directory with git.
When we start versioning in this way, we are said to have created a new git repository. Repositories contain a lot of metadata, including changes to files, branches, tags, and worker information.
When you create a new repo with the git init
command, it internally creates a .git
directory to store the various metadata needed for versioning.
The screenshot below shows the inside of the .git
directory created by the git init
command.
1.1. File and directory descriptions inside the .git directory
Here's a quick rundown of the files and directories inside the .git
directory.
config
: A file containing only the settings for the current repository. This includes the remote repository URL, whether it is bare or not, branch settings, etc. and overrides the global git settings. The default settings are
$ cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
HEAD
: A symbolic link to the branch you are currently working on. git keeps track of the branch you are currently working on through this file. The HEAD file takes the following values
$ cat HEAD
ref: refs/heads/main
description
: A file containing a short description of the current repository. Various git tools use the contents of this file to introduce the repository. If nothing is registered, it will have the value below.
$ cat descripton
Unnamed repository; edit this file 'description' to name the repository.
-
objects
: Everything you commit to git is stored as a git object. This folder acts as a DB for those objects, storing most things like blobs, trees, and commits using SHA-1 hashes. -
Refs
: Holds links to branches, tags, and remote branches. It is divided into three subdirectories: heads, tags, and remotes. -
info
: A directory containing additional information about the current repository, mainly to manage data to be excluded from versioning via theexclude
folder. It acts like a.gitignore
file. -
hooks
: A directory containing examples of hook scripts that IGT can run automatically when certain events occur. There are hook scripts for different scenarios, like pre-commit, pre-rebase, etc. If you want to run one of the scripts, simply remove the.sample
after the filename to make it an executable. Below are the scripts in the hooks directory.
This git init
command creates a new repository and makes room for the configuration files and data associated with it.
2. Create the main branch
Traditionally the default branch in git was master
, but for various reasons we started changing it to main
. Nowadays the main
branch is suggested by default without any configuration, but depending on the version you may need to set it manually. Therefore, in this section we will learn how to change the default branch.
The default branch can be changed in git's global settings. Locally, this setting only applies to your account. The command is as follows
$ git config --global init.defaultBranch main
After setup, if you have created a new repo using the git ini
t command, you can check the branch you are currently working on using the command below.
$ git branch
* main
If you haven't made your first commit yet, you can check it out with the following command
$ git symbolic-ref HEAD
refs/heads/main
3. The --bare option
Of the options available with the git init
command, the most popular is the --bare
option. This option is used to create a remote repository.
A remote repo is a repo created to allow multiple developers to contribute to and manage a single repo. They should be used in highly collaborative environments. Common services that host remote repositories include Github, GitLab, and BitBucket.
Consider the case of creating a new remote repo on Github. Without the --bare
option, our new remote repo will be created with data and settings specific to our local environment on the Github server.
What we want to manage is the source code in our local environment.
In this case, Github will use the --bare
option to create an empty shell repo with no settings and use it as a remote repo.
If you're not using a hosting service like the one above, and you want to set up your own git server and create a remote repo, you'll need to use the --bare
option. It's easy to use.
$ git init --bare my_remote_repo.git
As shown above, you can append an option. One caveat: be sure to name your bare repo with a .git
extension, as this is the convention for many remote repository addresses.
4) The --template option
The git init
command always creates the same file and directory structure. What if we wanted to add the same README.md
file to 10 different projects that our company creates? You'd have to go through the process of creating and modifying the file 10 times.
This is where the --template
option comes in, to help me create a new repo to my liking without having to go through the extra modification process.
As the name suggests, it creates a new repo according to your template, and all the files and directories we saw in Section 1.1 can be the target of the template.
4.1. Using the --template option
To create a custom template, start by copying the default template used by the git init
command. The template directory is located in different places depending on your operating system, so we recommend you use the following command to find it.
$ find / -type d -name "templates" 2>/dev/null
Of the many paths that come up in the search results, the templates
directory inside the git-core
directory is the one we're looking for.
On Mac OS, if you're using git, which is installed by default, it exists in the following path. The folders inside the templates look like this, which should be familiar to you since you've already seen them in Section 1.1.
Once you've copied this templates directory and added the files or folders you want, you can create a new repo in one of two ways.
First, you can use the --template
option, which will apply the template once.
This is useful if you want to use a template but need to change the information from project to project.
The command is as follows
$ git init --template=/path/of/template-dir
The second way is to register it in a global setting and use it permanently. If you create a repo with the same template every time, this will be useful. You can use the following command.
$ git config --global init.templateDir /path/of/template-dir
You can see that a new repo will be created using the template with the desired path, even if you don't give it a template option.
5. Cancel, remove and uninstall
Sometimes you need to undo the git init
command for version control.
This can be done by simply deleting the .git
directory that was created.
You will need to delete all files and folders under the directory, so type the following command.
$ rm -rf .git
If you are using the Windows command prompt, type the following command
$ rmdir /s /q .git
If you're using Powershell on Windows, you'll need to type the command below.
Remove-Item -Recurse -Force .git
6. Conclusion
In this article we have covered a number of topics related to the git init
command.
We've covered what's in the .git
folder, changing the default main
branch, the --bare
and --template
options, and even how to undo an initialization.
I hope this helps you get started creating new repositories with Git.
