How version control works? Best practices.

_1. What is VCM and git?

_1.1. VCM (Version control management) is a tool who takes care your software to be appropriately versioned, so that in case of necessity you can always turn back to previous version. It serves also teams to merge their changes and solve conflicts if necessary. There are many systems as VCS, SVN, mercurial, etc. offering such features, but since git appeared it has turned into a standard. And this is not without a reason, git offers much more. It is distributed, which means you can work also offline. Creating branches, repos, remotes, merging, reverting, etc. is a peanut. And the performance is amazing. So git is just the best VCS you can get.

_2. Install and configure git

_2.1. Install Since git is written in C there are platform dependent distributions. For the most popular platforms you can find appropriate packages on the official git site, which you can easily install. For Ubuntu Linux it is:

>apt-get install git

For Windows you have to download the installer and launch it or download the portable version.

_2.2. Configure Once you have installed your git you can configure it so that your commits can be distinguished:

>git config --global "my name"

>git config --global ""

_3. Create repository To create a repository you just have to navigate to the directory, you want to create it and execute:

>git init

Now you have created your local repository and you can commit and push into it. How to do it you can see in the chapter 8. If you want to couple it with remote repository, we will show you how in the chapter 5.

_4. Cloning a remote repository One of the first tasks, before you start working will be to clone a remote repository.

>git clone


>git clone

_5. Remotes Remotes are repositories hosted on servers like (Github)[] It is practical to have a remote repository, so if your laptop crashes you can always fetch your code from the host. If you have cloned a remote repository, you already have coupled your local repository with the remote one, but you can still add more remotes. If you have only local repository, as the one created in chapter 3, you can add a remote executing the following command:

>git remote add origin

_6. Fech and/or pull changes Fetch and pull are two operations in git which have to be well understood and distinguished. Fetch is getting the changes in all the remote branches you are tracking, but it is not merging them in the current one. Pull is fetching the changes only on the current branch and merging them into it. The syntax is pretty straight:

>git fetch

>git pull

_7. Create/switch (to) a branch One git strength is the easiness of creating branches. You just execute: >git branch myNewBranch Here we created myNewBranch from the current branch. If we want to start working on this branch, we will have to switch to it:

>git checkout myNewBranch

If you want to create a new branch and switch to it in one command, you can execute:

>git -b myNewestBranch myNewBranch

_8. Commit and push changes When you are done with your work, you have to commit and push your changes. Commit will save the changes in your local repository and push will send them to the remote one. Before commiting you have to stage your changes. It means you can select which changes should be committed. To stage all the changes, you can execute:

>git add --all

Or if you want to stage the changes in the current directory, you can execute:

>git add .

To commit you changes you just have to:

>git commit -m “my first commit”

Keep in mind, the message is mandatory. If you skip it, the default editor will be open and you will be forced to write one. Pushing the commits:

>git push

If you want to push to another remote:

>git push otherOrigin someBranch

_9. Tag, merge commits and fix merge conflicts To tag a particular commit you just execute:

>git tag myCoolTag

Tags are used when you want easy to find some particular commit. You can consider them as kind of bookmarks.

_10. Some helpful functions

_10.1 If you want to check if there are some unstaged and staged changes, you simply:

>git status

_10.2. To see the commits in your branch:

>git log

_10.3. To revert your changes to the last commit:

>git revert <commit hash>

Or if you want to revert the last one:

>git revert HEAD

One more commit back:

>git revert HEAD~1

_10.4. Another way to remove changes is reset:

>git reset --hard

You have to keep in mind revert creates a new commit and your previous commit remains in the history, so you can go back or cherry pick it whenever you want. On the other hand reset --hard simply puts the git pointer to some commit in the past and all the commits done later are lost forever.

_10.5. If you want to get the newest upstream changes, but you are not ready to commit, you can just stash your changes and later pop them, like this:

>git stash

>git pull

>git stash pop

_10.6. Common case is when you want to change the message or some code of your last commit. You just have to:

>git commit --amend

_11. IDEs All the git commands are done in the command line, but most of the people prefer to work with tools. One of the most popular tools for working with git on windows is tortoisegit. On linux i have never used an IDE, but i have seen many recommendations of GitKraken it is free for open source or early stage projects. Of course most of the modern development editors like eclipse, atom, vs code have integrated plug-ins for working with git.

_12. Git based cloud providers The most popular git cloud provider is still github even after it was accused by microsoft :-) But there is strong competition from Atlassian’s bitbucket and the new player gitlab I personally can recommend the gitlab, because of the planty features it offers. As bitbucket it offers not only version management, but also wiki, issue tracking, CI and much more. Bitbucket is also very nice tool i have used for couple of projects and i can strongly recommend.