Creating your first git repository
Creating a Git Repository and Basic Commands
Last updated: 2/11/2025
Creating Your First Git Repository
In this lesson, you’ll learn how to create a new Git repository, add files to it, and start tracking changes. By the end of this lesson, you'll have your first Git repository set up and ready for version control.
What is a Git Repository?
A Git repository (repo) is a directory where all the files for your project, along with the history of changes to those files, are stored. Git tracks every change you make to your files, so you can later view, compare, or revert to earlier versions if necessary.
There are two types of Git repositories:
- Local Repository: This is the repository stored on your own computer.
- Remote Repository: This is a repository stored on a remote server, like GitHub, GitLab, or Bitbucket, where others can access and collaborate on your project.
In this lesson, we’ll focus on creating a local Git repository.
Step 1: Create a New Directory for Your Project
Before creating your Git repository, you need a project directory. You can create a new directory or use an existing one.
-
Open your terminal (or Command Prompt on Windows).
-
Navigate to the location where you want to create your project folder. For example:
cd path/to/your/folder
-
Create a new directory (if you don't have one already) by running the following command:
mkdir my-first-git-repo
-
Navigate into the newly created directory:
cd my-first-git-repo
Step 2: Initialize the Git Repository
Once you have your project directory set up, the next step is to initialize a Git repository inside it.
-
Inside the project directory, run the following command:
git init
This command initializes an empty Git repository in the directory. Git creates a hidden .git
folder that will store all the information about your project’s history and configuration.
You should see a message similar to this:
Initialized empty Git repository in /path/to/your/project/.git/
Now your project directory is a Git repository, and you can start using Git to track changes.
Step 3: Add Files to the Repository
Next, let’s add some files to your repository. If you don’t have any files yet, you can create a simple text file to get started.
-
Create a new file in your project directory. For example, you could create a file called
README.md
with the following command:touch README.md
-
Open the file in your text editor and add some content. For example:
# My First Git Repository This is my first project using Git.
-
Save the file.
Step 4: Stage the Changes
Before committing your changes, you need to stage the files. Staging allows you to select which changes will be included in the next commit.
To stage your README.md
file, run the following command:
git add README.md
If you want to stage all files in your directory (including new files), you can use:
git add .
This command stages all changes in the current directory, including new files, modifications, and deletions.
Step 5: Commit the Changes
Now that your changes are staged, the next step is to commit them. A commit is a snapshot of your files at a particular point in time. Each commit has a message that describes the changes made.
To commit your staged changes, run the following command:
git commit -m "Initial commit with README file"
The -m
flag allows you to add a commit message directly in the command line. The message should be a short, descriptive summary of the changes you've made.
You should see an output similar to this:
[master (root-commit) 1a2b3c4] Initial commit with README file
1 file changed, 1 insertion(+)
create mode 100644 README.md
This means that your commit has been successfully created and stored in your Git repository.
Step 6: Check the Status of Your Repository
You can check the current status of your repository at any time by running:
git status
This command will show you the following:
- Files that have been modified but not yet staged.
- Files that are staged and ready to be committed.
- Files that are untracked (new files that haven’t been added to Git yet).
For example:
On branch master nothing to commit, working tree clean
If there are changes to commit, Git will show those as well.
Step 7: View the Commit History
To view the history of your commits, you can run the following command:
git log
This will display a list of all commits in your repository, along with details such as the commit ID, author, date, and commit message. For example:
commit 1a2b3c4d5e6f7890abcdef1234567890abcdef12 Author: Your Name <your_email@example.com> Date: Mon Feb 12 12:34:56 2025 -0700 Initial commit with README file
You can use the arrow keys to scroll through the history and press q
to exit.
Conclusion
Congratulations! You've just created your first Git repository. You've learned how to:
- Create a new directory for your project.
- Initialize a Git repository inside your project folder.
- Add files to your repository.
- Stage and commit changes.
- Check the status of your repository and view the commit history.
In the next lesson, we'll explore how to work with remote repositories and collaborate with others using Git.