Create a Git repository and make your first commit

Description: Create a folder named my-notes, make it a Git repository, create a file notes.txt with some text, and save it in a first commit with the message “Add notes”.

Hint:

  • Use git init inside the folder.
  • A file must be staged with git add before the commit.

Solution

Terminal output

$ mkdir my-notes
$ cd my-notes
$ git init
Initialized empty Git repository in /home/robin/my-notes/.git/
$ echo "My first note" > notes.txt
$ git add notes.txt
$ git commit -m "Add notes"
[main (root-commit) 6927e03] Add notes
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
$ git log –oneline
6927e03 (HEAD -> main) Add notes

How it works

  • git init creates the hidden .git folder, which makes the folder a repository.
  • git add stages the file, and git commit -m saves it with the message.
  • git log --oneline shows the new commit.

Read Git Init and Git Commit.


Learn more about the similar topics:
Tutorials
Git Introduction
Version Control System
Install Git
Git Configuration
Git Workflow
Git SSH Key
Git and GitHub
Exercises & Assignments
Create a Git repository and make your first commit
Interview Questions & Answers
What is Git?
Why SSH key is used in Git?
How to create SSH key and use for GitLab?
What is the difference between Git and GitHub?
What is a repository in Git?