Every developer around the world has heard about Git. Most of them even know that it’s a version control system (VCS), but what exactly does it mean? And more important – should we use it? And how? I’ll try to throw some light on it in a few paragraphs below.
How did it start?
Let’s begin with a short historical note: Git was created in 2005 by Linux inventor – Linus Torvalds as an alternative to previously used VCS software, with the aim to help to develop Linux kernel. But with time has become the most recognizable and bulletproof VCS for many companies around the world.
What means VCS?
Version Control System like Git helps you to track changes someone made in the project. It’s creating a history of changes – Git gathers every version of the file you saved (via special git commands) letting you know who made it, when and why. You can always compare files in the project between each version, show differences, deleted or added lines etc.
The most important feature of Git is strong support for working on branches. That helps to develop a project in a non-linear and secure way. Every developer has his own version of the project locally, on which he works offline. Usually, one task is “placed” on one branch. Even if there are many people working on the same task, just different features, each works independently. In other words: development is distributed. Then when their parts of the task are ready they can merge their work and finally export the branch to be merged also with the main one.
In addition, Git is compatible with existing systems and protocols: allows to push repositories via SSH, HTTP or FTP and works on Linux macOS and Windows.
Git is known for its efficiency in the handling of large projects – it’s fast and scalable. Many open source projects are using it as a necessary tool: a.o. various Linux-connected (Debian, Ubuntu), but also Gimp, Perl, Ruby on Rails, jQuery, Reddit or Facebook.
GitHub is a place where millions of developers host their projects in repositories. Until January 2019 free version allowed to have only projects open to the public, but recently they have changed their policies giving access to unlimited free private repositories for individual users (with some restrictions to 3 contributors for a project). There are still some paid plans, which allow having more complex private repositories. GitHub service provides wide options for distributed version control as well as code and task management.
Some other known hosting services for version control based on Git are Bitbucket and GitLab.
Should we use it?
I guess that you already figured out that the answer is positive. Throughout the development, you should always use Git. From day one – when you just start development create a repository and push your changes there.
Why is it so important?
When you push your code to GitHub/Bitbucket it’s stored in a centralized cloud, which gives you and your team-mates opportunity to use it from any place they want to. It’s available even if your hard drive fails – the code is backed up and safe!
Another advantage is version control – every version committed and pushed to the repository is available to you. You may revert to the code from a couple of months ago if there is a need.
Not to mention that working in a team, even remotely, with Git is easier than ever. All team members can collaborate on the project without disturbing the work of others. They can work on the same files and simply merge them together when their part of work is done.
You can work with the code offline, saving commits is fast and you can easily undo mistakes. With branching code is separated into tasks, so you don’t mix up things. By using GitHub you can contribute to many open source projects, get code review and in the end be a better developer.
How to start?
The first step is to create a git repository while being in a project folder. By typing in a command line:
you will make a repository, and a hidden .git file will be added to your current folder. Inside the file will be placed all internal data stored by git.
If you are joining to an already existing project you will probably need to download all files. Usually, the easiest way to get them is by cloning a repository from GitHub (or Bitbucket etc) with:
git clone https://github.com/example.git
command, where https://github.com/example.git is the name of the desired project repository.
Now, every time you change something in the project (add a file, some lines of codes, remove anything) when typing in a command line:
you will be informed that there are some changes in project and Git will list the distinct files.
The best practice, before starting some changes is to create a branch on which you will work on your task. Type:
git checkout -b <my-branch-name>
(ex. git checkout -b develop) so all your files will be saved in that branch, without interfering/changing anything in the original project (by default stored on a branch called master).
After every part of work done, before going home from work or turning off your computer you can save a version of the project you’ve been working on by adding changes files with:
git add <file-path>/<file-name>
for a significant file, or simply one of below:
git add --all
git add .
for all changed files. It will stage them, so they are ready to be committed.
git commit -m “Text explaining why and what you have changed”
you will “save” all your files as a commit. -m – stays for a message and is not obligatory, although recommended.
Each commit has its unique number, date and (usually) carry a message, which helps to navigate through them when working in a bigger group of developers and managing the differences.
Committing will save the version of your project files only on your local computer. To export it to a remote repository (for example to the place from which you have cloned it) you should use a command:
This is an abbreviation for longer command:
git push <remote> <branch-name>
where remote means that you are exporting it to the remote origin branch (in opposite to local one), and branch name is the one you have commit on, in our scenario, it would be git push origin develop.
After a successful push your coworkers will be able to import your changes, see your commits (and merge them automatically into their projects) using a command:
If you are secure that your changes are correct and want to add them to the main project files you should merge your branch with the main one. First change (hop) to the desired (main) branch with:
git checkout master
So you changed branch to the main one (called master in our case). You won’t find any changes in files here. All the changes are saved on my-new-branch. You may see these while using a command:
git diff <branch-name> <other-branch-name>
In our case git diff master my-branch.
When you are sure you want to have it also on main just merge the branch with the master by typing:
git merge my-branch
Now all your changes are stored both on master and my-branch. If no longer needed you may delete my-branch with a command:
git branch -d my-branch
Another extremely useful command while working in a team is:
git fetch --all
which imports all remote files and branches to our local repository.
While working, you may find a situation that you will have a file which was accidentally changed but it shouldn’t be – there is a git solution for it. To remove all the changes and not bring any updates in that specific file you should type a command:
git checkout <file-name>
Another command to reset the changes, yet more powerful is:
git reset --hard <commit-nr>
With it, you will undo all changes (uncommitted, staged and committed) and get back to the previous version of files (committed with that significant number). Be careful with that command, it’s potentially dangerous, so check previously with git status what have you changed. Here you will find some more info.
A quick sum-up of all commands used above, with a real-life example:
git clone https://github.com/git/git.git
git checkout -b develop
git add dest/styles.css
git checkout main.js
git commit -m “Add a file with styles”
git push origin develop
git checkout master
git diff master develop
git merge develop
git branch -d develop
git reset –hard e4bbc3f
In addition to some of Git commands you may also add flags. Feel free to check it out on the Git official website, where you will find many more functionalities and whole documentation. Or try that useful webpage, which helps you to get to know Git better.
Git is a de facto standard nowadays used by most of the IT companies, helping programmers to work in teams easily and secure the code versions.