The Importance of Using Version Control To Increase Team Work Effectiveness
Have you ever involved in Software Development with a team? And have you ever faced any difficulty to maintain your code or telling your team if there are any updates? How do you come over it? Well, if you haven’t use any version control such as git, you have to!
What is Git?
Git is a distributed version control system for tracking changes in source code during software development. It first founded by Linus Torvalds which is the founder of Linux kernel to develop Linux together with other developers contributed in the development process. It is a free and open-source software distributed under the terms of the GNU General Public License.
Git stores information about the project’s progress on a repository. A repository has commits to the project or a set of references to the commits called heads. All this information is stored in the same folder as the project in a sub-folder called .git and will mostly be hidden by default in most systems. Git keeps track of the changes a couple of people make on a single project and then merges the code where people have worked on different parts into one project. This way, when someone introduces a bug, you can track down the code that introduced the bug by going through the commits. Commits must be made to a project to tell git that you’re satisfied with the changes you’ve made and want to commit the changes into the main branch called master by default.
You can then upload the code to GitHub or BitBucket where authorised users can either view, pull the code or push changes.
How Does Git Work?
To get started with Git, you need to download it to your machine. Head over to https://git-scm.com/ and download the version most compatibe with your system.
During the installation of Git, make sure you choose to run Git on the normal console window as well, this will enable you to run Git on your command prompt using the git command. I will show you the simplest usage of it.
Initialize a repository
git init
This will create a hidden folder called .git as you can see down below.
Suppose you have a simple code called demo.js like this:
function gitDemo(){
console.log("Git is such an amazing tool!");
}
Committing changes.
After that you can see to changes in your repository by run a command
git status
You can commit your file by add it first, run the command
git add demo.js
After running that command you can see that your file has been added to the changes and ready to be committed.
Create a commit using command:
git commit -m "A commit message"
Don’t forget to add remote repository in your local repository’s folder (you need to create it on your GitHub or GitLab first)
git remote add origin https://github.com/your_username/your_repository_name.git
Then you can push your code to your GitHub repository
git push origin master
Then enter your username and password.
What it does is, you will push all your committed files to your origin remote repository (which you set earlier) and push it to the master branch. If you push it successfully, you will see something like
And voila! You will see your code appears on your GitHub repository
Now you can clone it easily on other machine by running
git clone https://github.com/your_username/your_repository_name.git
What it does is you will clone the repository completely including all files and branches to your local machine.
Branching.
Now we get into the exciting part: branching.
Branching is basically where you separate your work for each steps of software development: adding features, fixing bugs, staging, or deploying to production.
Let’s assume you’re working on a huge project and want to add an experimental feature. You’re not sure if it’ll work or will be accepted, you can branch off your main tree by creating a new branch and working on that branch instead. Think of it hypothetically as how a tree has branches, but when you branch off from the main branch however, you can be satisfied with the experimental features and decide to merge the two branches. You can create as many branches as you want, pull the most current changes to your working branch, merge your code from you working branch to production branch, etc.
To see all branches and what branch you’re working on, you can run
git branch
And you will see something like:
Make sense, since we haven’t create any branches. Now, we’re going to make a new branch, run:
git branch <branch_name> orgit checkout -b <branch_name>
Now, your new branch should appeared
Now, to change your working branch to the branch you’ve created, run
git checkout <branch_name>
That’s it, that’s the basic of using git, you can improve your git skills by using it on your project, try to create more branches, merging two branches, go back to your last commit, fixing merge conflicts, etc.
Sure mastering git isn’t easy at all, but once you’ve adapted with it, you sure won’t face any fatal problem with your large project.
Thanks! :)