• Home
  • DevOps
  • How to create a new Blank Repository and commit code in it

How to create a new Blank Repository and commit code in it

Last updated on May 27 2022
Suyash Borole

Table of Contents

How to create a new Blank Repository and commit code in it

Git Init

The git init command is the first command that you will run on Git. The git init command is used to create a new blank repository. It is used to make an existing project as a Git project. Several Git commands run inside the repository, but init command can be run outside of the repository.
The git init command creates a .git subdirectory in the current working directory. This newly created subdirectory contains all of the necessary metadata. These metadata can be categorized into objects, refs, and temp files. It also initializes a HEAD pointer for the master branch of the repository.

Creating the first repository

Git version control system allows you to share projects among developers. For learning Git, it is essential to understand that how can we create a project on Git. A repository is a directory that contains all the project-related data. There can also be more than one project on a single repository.
We can create a repository for blank and existing projects. Let’s understand how to create a repository.
Create a Repository for a Blank (New) Project:
To create a blank repository, open command line on your desired directory and run the init command as follows:
1. $ git init
The above command will create an empty .git repository. Suppose we want to make a git repository on our desktop. To do so, open Git Bash on the desktop and run the above command. Consider the below output:

devops 11
devops

The above command will initialize a .git repository on the desktop. Now we can create and add files on this repository for version control.
To create a file, run the cat or touch command as follows:
1. $ touch <file Name>
To add files to the repository, run the git add command as follows:
1. $ git add <file name>
Learn more about git add command visit Git Add.
Create a Repository for an existing project
If you want to share your project on a version control system and control it with Git, then, browse your project’s directory and start the git command line (Git Bash for Windows) here. To initialize a new repository, run the below command:
Syntax:
1. $ git init
Output:

devops 12
devops

The above command will create a new subdirectory named .git that holds all necessary repository files. The .git subdirectory can be understood as a Git repository skeleton. Consider the below image:

devops 13
devops

An empty repository .git is added to my existing project. If we want to start version-controlling for existing files, we have to track these files with git add command, followed by a commit.
We can list all the untracked files by git status command.
1. $ git status
Consider the below output:

devops 14
devops

In the above output, the list of all untracked files is displayed by the git status command. To learn more about status command, visit Git Status.
We can track all the untracked files by Git Add command.
Create a Repository and Directory Together
The git init command allows us to create a new blank repository and a directory together. The empty repository .git is created under the directory. Suppose I want to create a blank repository with a project name, then we can do so by the git init command. Consider the below command:
1. $ git init NewDirectory
The above command will create an empty .git repository under a directory named NewDirectory. Consider the below output:

devops 15
devops

In the above output, the directory and the repository both are created.
Hence we can create a repository using git init command. Two other commands are handy to start with git. They are Git Add, and Git commit.
Also, see various operations on the repository, see Git Repository.

Git Commit

It is used to record the changes in the repository. It is the next command after the git add. Every commit contains the index data and the commit message. Every commit forms a parent-child relationship. When we add a file in Git, it will take place in the staging area. A commit command is used to fetch updates from the staging area to the repository.
The staging and committing are co-related to each other. Staging allows us to continue in making changes to the repository, and when we want to share these changes to the version control system, committing allows us to record these changes.
Commits are the snapshots of the project. Every commit is recorded in the master branch of the repository. We can recall the commits or revert it to the older version. Two different commits will never overwrite because each commit has its own commit-id. This commit-id is a cryptographic number created by SHA (Secure Hash Algorithm) algorithm.
Let’s see the different kinds of commits.

The git commit command

The commit command will commit the changes and generate a commit-id. The commit command without any argument will open the default text editor and ask for the commit message. We can specify our commit message in this text editor. It will run as follows:
1. $ git commit
The above command will prompt a default editor and ask for a commit message. We have made a change to newfile1.txt and want it to commit it. It can be done as follows:
Consider the below output:

devops 16
devops

As we run the command, it will prompt a default text editor and ask for a commit message. The text editor will look like as follows:

devops 17
devops

Press the Esc key and after that ‘I’ for insert mode. Type a commit message whatever you want. Press Esc after that ‘:wq’ to save and exit from the editor. Hence, we have successfully made a commit.
We can check the commit by git log command. Consider the below output:

devops 18
devops

We can see in the above output that log option is displaying commit-id, author detail, date and time, and the commit message.
To know more about the log option, visit Git Log.

Git commit -a

The commit command also provides -a option to specify some commits. It is used to commit the snapshots of all changes. This option only consider already added files in Git. It will not commit the newly created files. Consider below scenario:
We have made some updates to our already staged file newfile3 and create a file newfile4.txt. Check the status of the repository and run the commit command as follows:
1. $ git commit -a
Consider the output:

devops 19
devops

The above command will prompt our default text editor and ask for the commit message. Type a commit message, and then save and exit from the editor. This process will only commit the already added files. It will not commit the files that have not been staged. Consider the below output:

devops 20
devops

As we can see in the above output, the newfile4.txt has not been committed.

Git commit -m

The -m option of commit command lets you to write the commit message on the command line. This command will not prompt the text editor. It will run as follows:
1. $ git commit -m “Commit message.”
The above command will make a commit with the given commit message. Consider the below output:

devops 21
devops

In the above output, a newfile4.txt is committed to our repository with a commit message.
We can also use the -am option for already staged files. This command will immediately make a commit for already staged files with a commit message. It will run as follows:
1. $ git commit -am “Commit message.”

Git Commit Amend (Change commit message)

The amend option lets us to edit the last commit. If accidentally, we have committed a wrong commit message, then this feature is a savage option for us. It will run as follows:
1. $ git commit -amend
The above command will prompt the default text editor and allow us to edit the commit message.
We may need some other essential operations related to commit like revert commit, undo a commit, and more, but these operations are not a part of the commit command. We can do it with other commands. Some essential operations are as follows:
• Git undo commit:
• Git revert commit:
• git remove commit:
So, this brings us to the end of blog. This Tecklearn ‘How to create a new Blank Repository and commit code in it’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Git and GitHub and build a career in DevOps domain, then check out our interactive, Version control with Git and GitHub Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Version Control with Git and GitHub

Version Control with Git and GitHub Training

About the Course

Tecklearn has specially designed this Git and GitHub Training Course to advance your skills for a successful career in this domain. The course will cover different components of Git and GitHub and how they are used in software development operations. The course consists of important concepts like: branching & merging, how to deal with conflicts, rebasing, merge strategies, Git workflows and so on. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Git.

Why Should you take Git and GitHub Training?

• Average salary of Git and GitHub Professional is $85k – Indeed.com
• Amazon, Google, Facebook, Microsoft, Twitter, & many other MNC’s worldwide use Git across industries.
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps
• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation
Git and GitHub
• What is version control
• Version Control System (VCS) Products
• Types of VCS
• What is Git
• Why Git for your organization
• Install Git
• Common commands in Git
• Working with Remote Repositories
• GitHub
• Git Installation
• Git Lifecycle
• GitHub (Push, Pull Request)
• GitHub Workflow

0 responses on "How to create a new Blank Repository and commit code in it"

Leave a Message

Your email address will not be published. Required fields are marked *