Git is a tool to collaborate in the edition of text files. In this post we will see how to create repositories.
Introduction
A repository is a special folder in which we can perform Git operations.
We have two scenarios when creating a repository: to use a directory without existing data and to use a directory with pre-existing data. Later we will see how to link our repository with a Git server like GitHub.
Using a directory without existing data
With the Git Bash application:
- We navigate to the path where we are going to create the repository folder (using “ls” to show files and directories and “cd” to enter a directory).
- We create the repository folder with “mkdir” or with the system file explorer and we enter the folder.
And now, we launch this command to create the repository:
git init
This way the repository will have been created locally and we can see how now the command line has changed.
To check the status of the repository we launch:
git status
It tells us that there is nothing to upload.
We create a “HelloWorld.txt” file with the program that we want and we add some text.
We launch these commands:
git add .
git commit -m "First commit with a new file"
This will have added the file to the stage and then it will have taken a photo to commit.
To check that this “commit” has been generated, we launch:
git log
Using a directory with existing data
Another example of using Git is to use a directory with pre-existing text files.
We navigate to the directory where we have our files and enter:
git init
This will initialize the repository at that location.
Then we launch:
git add .
git commit -m "First commit with existing files"
To check that we have done the commit correctly, we enter:
git log
Link the local repository with GitHub
In either of the previous two cases, we need to link the local repository to a repository that is located on a server. This way other collaborators will be able to make changes.
To link the local repository, first we need to create it in our GitHub account. We only need to create it, without adding any files.
In Git Bash, we navigate to the corresponding folder, and we launch:
git remote add origin git@github.com:username/repository_name
git push -u origin master
This way the two repository are connected and if we make any change in one of them we can synchronize the other.
So, what do you think ?