DEV Community

tetiana chupryna
tetiana chupryna

Posted on

Brief intro to git

What is Git? Git is a distributed version control system that also has a hidden superpower of a time machine. Git is an ultimate tool that allows different people to collaborate on software projects and also access the whole project history and see how it evolves with time.

Important concepts in the git universe are “commit”, “branch” and “remote”. Let’s talk about them!

“Commit” is kind of a snapshot of the project in a moment of time. It contains changes made to the project files as well as some meta information: author of the commit, date and time, description, and other things. Every commit is unique and it has a signature which is SHA-1 checksum hash (simply speaking, it’s a long 40 character string that depends on the content of the commit). If any part of the commit is changing, even a small one like the time of the commit, the whole checksum is changing as well.

To create a commit on an existing project, you need to do the change on the project file first. Then you need to add that file to the pre-commit stage wit a command

git add {file}
Enter fullscreen mode Exit fullscreen mode

Now you’re ready to commit:

git commit -m {“Description of the change”}
Enter fullscreen mode Exit fullscreen mode

This -m flag tells git to add a message description to your commit. Commit messages should be written with precision. They should be clear, explicatory, and eloquent (if possible). Commit messages are really valuable, as a fly that is inside of amber, they stay in time and can be really valuable for future bug-hunter and code archeologists.

Those “commit” objects don’t live in the world by themselves, they are based on “what was there before”. Commits are arranged in chains that are called “branches”. Every project can have many branches and this allows us to have non-linear development. If you like sci-fi you can see this as a multiverse with branches represent different scenarios of project development. However, in practice, it means that many people can work independently on the same project. Sure, at some point in time, if they changed the same files, they have to “resolve conflicts” (which is another concept used in git) and define what will be the final version of the file that will contain changes made by different people. However, before that moment people can work on their tasks without messing with the code of their colleagues.

There could be many branches in the project, but every project has a special “default” branch which is a single source of truth for the project. Returning to sci-fi analogies, if branches represent what the future might look like, that “default” branch represents what is the present. Usually, this branch can be called “master”, as the analogy from sound recording “master-copy”. Sometimes it called “trunk” which is a great name if we think of git project as a tree data structure.

When you start working on a new task, you need to create a new branch first. That can be done with the next commands:

git branch {name-of-your-branch}
Enter fullscreen mode Exit fullscreen mode


git checkout -b {name-of-your-branch}
Enter fullscreen mode Exit fullscreen mode

Command checkout is used to switching between branches but with the flag -b it can create a new branch.

The name of a branch should describe the feature that is developed there. Some people also add their name to the name of the branch. That can be helpful to identify your branches in a big repository (another concept used by git which is basically just a project).

At this point in time, we talked about development happening on a local machine. But the whole point of git is collaboration. We need some central storage for the repository so people can download (clone) the project to their local environment and continue development from their cozy homes. The repo that is not at your machine is called “remote” and you can connect to it to get (pull) changes. There are different websites that provide the infrastructure for storage git repos and enable collaboration on the projects. You may know Bitbucket, GitHub, GitLab, SourceForge, and many more.

Usually, when you work on an existing project, you start with cloning it to your machine so it already connected to a remote. What is left is to pull fresh changes from the main branch with the command git pull, create a new branch, do changes that you need, commit them and then push this new branch to the remote. You can do it with the next command:

git push
Enter fullscreen mode Exit fullscreen mode

Yes, that’s easy. Now you’re ready to open your first request to merge that changes to the main codebase. And that’s where the journey begins!

Top comments (0)