2.2 Git Basics: Creating a repository, first commit
|Table of Contents
- Create a Git repository
- Working area, index and repository
- Adding and saving a file to git: what happens
Create a Git repository
To create a Git repository, navigate to the directory that you want to make a repository and use the following command:
git init
This will create a new repository in the current directory. You can verify that the repository was created by using the following command:
ls -a
You should see a new directory called .git, which contains all of the necessary files for the repository.
Working directory, index and repository
When you make changes toyour code, you make them in the working directory. To add and commit those changes to the repository, you need to use the index and the following commands:
git add filename
This will add the changes made to the file to the index. To commit those changes to the repository, use the following command:
git commit -m "commit message"
This will create a new commit with the changes made to the file in the repository.
Adding and saving a file to git: what happens
When you add and save a file to git, git stores the file in the repository as a blob object. This object contains the contents of the file and a header with metadata about the file. Git also creates a tree object, which represents the directory structure of the repository at that point in time.
When you make changes to the file and commit them to the repository, git creates a new blob object for the new version of the file and links it to the previous version of the file in the repository. Git also creates a new tree object to represent the new directory structure of the repository.
Conclusion
Creating a Git repository and making your first commit is a fundamental step in using Git for version control. Understanding the working directory, index, and repository is crucial to effectively managing your changes and collaborating with others on your project.