In the previous post, we discussed about Git terminology. In this post, lets quickly discuss how to create a Git repository.
Before we jump onto creating a Git Repository, lets first understand what is repository. A Git repository is a virtual storage of your project. It allows you to track and save versions of your code, which you can access when needed. It saves this data in a directory called .git, also known as a repository folder.
Git uses a version control system to track all the changes made to the project and save them in the repository. You can copy or delete the existing repositories or create new repositories for ongoing projects.
Creating a Git Repository -
In this example, I'm going to create a Git Repository on a windows server.
1. Lets create a directory that is going to store all our repositories. I've named it as repos.
2. Once it is created, navigate to it.
3. Create a directory named MyGitRepos and navigate to it once created.
4. Now how to turn regular directory into git repository? You need to run the command git init
5. git init transforms current directory into git repository and it adds .git subdirectory in the current working directory. .git contains all of the necessary git metadata for the new repositories.
This .git directory is hidden directory. You would need to run the below command to view the hidden directory in the windows.
6. Navigate to the .git directory and you will see below files in this directory. These are the files Git needs to start tracking the changes made to the project files. The repository only starts tracking project versions once you commit changes in Git the first time.
7. Now to check the state of the working directory and the staged snapshot run the git status command.
8. Now lets create a one text file in our working directory.
9. Run the git status command again to check the state of working directory now.
Before I explain about the untracked files, let me first explain the difference between working directory and a local repository. Working directory is directory with our source control files under Git control. In this case it MyGitRepo. When we initialize our Git repository Git is tracking the difference between working directory and a local repository. Our local repository has nothing in it because that's when we initialize our local repository with git init.
When we did git init Git took picture of status of our working directory. When test.txt were added it took a new screenshot. Git finds this screenshot is different than the local repository screenshot. So whenever we type git status there is comparison going on between snapshots of local repository and working directory.
Since local repository has nothing in it and working directory has a file, we see untracked files here. Now Git does not know if we want to add these files or ignore them. If we want to add a file, you might have noticed that its giving us helpful tip to add file using command git add.
This is a staging area and staging is a step before the commit process in git.
10. Now the last step is commit the file using git commit command. The git commit command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to.
I hope you will find this post informative.
Thank you for reading!
*** Explore | Share | Grow ***
Comentarios