Start Git repository and first commit

Basic operations to start working with Git repositories. Creation of repositories locally, via git init and file commit via git commit.

In previous articles we have covered the initial steps that must be taken to be able to use Git, or rather, “get started with it”. However, we have not yet started working with repositories and monitoring the state of our software through its various versions.

If you have arrived here and do not know what this version control system is, we recommend you access the article where we explain . If you’ve already taken those first steps, keep reading.

Here’s how to create your first software repository and how to push files to the repository so they start being tracked by Git and have their various states stored over time by the version control system.

Open a console to work with Git

Once Git is installed, we are going to work with the command console to really learn how to use this system. There are several GUI applications for working with Git, but they are not really the best options for learning Git. That is why we recommend you learn by command line, as we explain in these articles.

If we are on Windows, we can open “Git Bash” (see article mentioned above) and if we are on Linux / Mac, we simply have to open a terminal of the operating system.

Once inside your command console, we are going to create a directory where we are going to put all our repositories. Although you can really have them spread out in whatever folders you want, that’s no big deal.

Note: If you don’t know what a repository is, we can clarify that it is simply like a shelf in your library where you keep software. It is a place, a folder, where you store all the files of a project and when you work with Git you have the possibility of having not only the current state of your files, but also the state they have gone through in all their versions throughout the life of that software.

Ways to get started with Git

To work with Git or GitHub (we know the differences because we already explained it in ) we have two ways of working:

  1. Work locally, in a repository that creates me on my machine.
  2. from Github (or other repository hosting) to bring us the complete repository locally and start working with that project.
See also  .gitignore file

We are going to choose for the moment option 1) that will allow us to start from scratch and with which we will be able to better appreciate what are the basic operations with Git. In this sense, any operation you perform with Git has to start by working locally, so you have to start by creating the repository on your own machine. Even if your goals are simply to push that repository to Github so that other people can access it through remote repository hosting, you still have to start working locally.

Create a folder for your project and place files

So, being in a folder on your computer where we have said that we are going to create all our repositories, you can create a specific folder for your first project with Git. You can create your project folder with the file explorer or if you want by command line, it doesn’t matter.

Once your folder is created you can create files inside. Since we are talking about software development, the files that you will put inside your folder will contain the code of your application, web page, etc.

Note: To create the files, in a typical developer workflow you will use your preferred code editor: Eclipse, Sublime Text, Komodo, Notepad++, Gedit, PhpStorm, etc. As you prefer. Also, of course, you can create your file on the command line and edit it if you’re on Linux with the familiar Vim or any other text-only interface editor.

Initialize the Git repository

To create your Git repository, where we monitor the files of a project, you have to perform a previous operation. It is the initialization of the Git repository that we want to create in your project folder and it is a one-time operation for this project.

Note: You will have each project in a different folder and it will be an independent repository. This initialization operation of your repository, therefore, will have to be carried out only once for each project that you want to control its versions through Git.

So first of all (before committing any files to the repository), I create the Git repository with the “git init” command.

See also  API development manual

git init

Once you have initialized the repository, you will see that a folder called “.git” has been created inside your project. If you see that folder in your project, the repository has been successfully initialized and you already have that folder converted to a Git software repository.

Files or folders on Linux/Mac that start with a dot are hidden in the file system. To see them you will have to execute the command “ls -la” in the terminal.

The .git folder is where all the information related to the repository will be stored. It will be obvious, but it should be said that this folder should never be deleted and we should not directly access its contents either, since we are always going to communicate with the repository through commands.

Save the files in the repository (commit)

Once you create your first files, you can start working with Git, pushing those files to the repository. Keep in mind that, even though the files are in your project folder and you have started the Git repository previously, the version control system is not monitoring them, so at the level of your Git repository it is still as if they were not there.

This action of saving the files to the repository is called, in Git jargon, making a “commit”. In Spanish it would be “make a confirmation” of the code. In this case, you are doing the commit locally, because you are sending the files to your local repository that you have on your machine.

A commit in Git is done in two steps.

Add the files to the staging area

We start by adding the file or files to a temporary intermediate area called “Index Area” or “Staging Area”, which is an area that Git uses where the files are saved that will later be committed.

Any file you want to send to the index zone you do with the “add” command.

git add file-name

Once added you could see that you really have that file in the “staging area”, through the “status” command.

git status

You will see that, among the things that appear as output from that command, it tells you that you have your file added as “new file”. It also tells you that these changes are ready to commit.

See also  Javascript libraries based on the Web Components standard

Many times we want to send several files to the staging area and we want to do it in one go, so it is typical to tell Git to move all the changes made to the index area, indicating “.” instead of the file name.

git add .

In this way, any file modification or file creation that has been made in the working directory would be sent to the staging area.

Commit files with commit

As a second step we have to send the files from the Index zone to the repository, which is called the “commit” itself. You do it with the following command:

git commit -m “message for commit”

With this we already have our first file (or files if we have committed all at once) inside the repository. From here we will be able to maintain and control the changes that are made to this file (or others that we have included through the commit).

A commit commits all the files that were in the staging area. The number of files that have been modified does not matter, when we commit, all of them are sent to the repository.

If you do a “git status” command again you can see that there is nothing to push to the repository. That means the Index zone is clean and all changes are pushed to Git.

Up to this point we have been able to learn how to create our repository and push the first changes through the commit operation. You can now test it on your system to start experiencing Git. In the following installments we will continue exploring the workflow with Git, and improving this first approach with new commands that allow you to do more things and make your work easier by summarizing the operations already mentioned in fewer steps.

Video tutorial of the first steps with Git

Below you can see a video that we have summarized in this article and where you can see all these operations live and explained step by step.

Loading Facebook Comments ...
Loading Disqus Comments ...