Learn to initialize a local Git repository for tracking changes and collaborating on projects.
In this lesson, you will learn how to initialize a local Git repository for your project. This step is essential for tracking changes and collaborating with teams.First, open your terminal, navigate to your project folder, and run the following command:
Copy
Ask AI
$ git initInitialized empty Git repository in /Users/lydiahallie/Desktop/myproject/.git/
This command creates a hidden .git directory containing all of Git’s tracking information for your project.To confirm that the repository was created correctly, list all files in the directory (including hidden ones) with:
Copy
Ask AI
$ ls -a. ...git
Next, add a new file to your project. In this example, we will create a simple text file called story1.txt that contains a short sentence. Execute the following commands:
Copy
Ask AI
$ touch story1.txt$ echo "This is a beautiful story" >> story1.txt
Even though you have created the file, Git is not yet tracking it. To check the current status of your repository, run:
Copy
Ask AI
$ git statusOn branch masterNo commits yetUntracked files: story1.txtnothing added to commit but untracked files present
The status output indicates that you are on the default branch (master) and that story1.txt is untracked. Untracked files are those that Git recognizes but has not been added to the staging area.
Before committing, always add your new or modified files to the staging area with git add.
To start tracking your new file, add it to the staging area:
Copy
Ask AI
$ git add story1.txt
With the file now staged, you’re ready to commit your changes. Always include a concise and descriptive commit message when committing. For example:
Copy
Ask AI
$ git commit -m "Added first story"
This commit records the current state of your project in your local Git repository. It serves as a reference point for you and your teammates, allowing you to revert to this state if necessary.By following these steps, you have successfully initialized a Git repository and made your first commit. This foundation is crucial for efficient version control and collaboration throughout your project’s lifecycle.