What is “version control”, and why should you care? Version control is a system that records changes to a file or set of files over time so that we can recall specific versions later.
We not only want to record changes, but also who made the changes, and when. Managing revisions at this level requires a Version Control System (VCS).
A VCS is a software, which helps for the team of developers to manange the changes to source code over time. It inclouds tools saving the state of a project, viewing the history of changes.
Using a Version Control System helps the developer to work on the same code base at the same time. For example one developer works on the repository of the app while another developer creates the new user interface for the loaded data.
VCS have tools for preventing conflicts. It can happen, that a developer working on a code and it is incompatible with the changes what was made by another developer.
- Git is designed for coordinating work among programmers, but it can be used to track changes in any set of files.
- Git was created by Linus Torvalds in 2005 for development of the Linux kernel.
- Rather then having only one single place for the full version history of a project, every developer’s working copy of the code is also a repository that can contain the full history of all changes.
If you haven’t installed on you computer Git yet, then open this link below.
Here you should select which operation system you have.
I use Windows, so here I gonna scroll to the “Installing on Windows” section, and there is a download link, click on it.
A window will pop up, and immediatelly the downloader window also with the latest version. Select a folder, where you want to save the downloader.
Double click on the download file, and install the Git on you computer.
- Click on the Next button and you will accept the licence.
- Select a destination folder.
- We will use the default selected components, so click again on the next button.
- Select Start Menu Folder, then Next
- Choosing the default editor used by Git: Use Vim – Next
- Because we will use Git from a 3rd-party software, which is in our case the Android Studio, so leave here again the default selected.
- Configuring the line ending conversions: Leave the 1st selected, which is the default one.
- Configuring the line ending conversions: Leave the 1st selected, which is the default one
- Configuring the terminal emulator to use with Git Bash: Leave the 1st selected, which is the default one
- Configuring extra options: Leave here again everything as default
- Finally click on the Install button
After the install of Git we can start to configure the version control system in our Android Studio project. For this tutorial we will upload only a basic start up project and later we will extend it with some other views.
Step 1 – Create new Android Studio project
To create a new project, start Android Studio and select “Start a new Android Studio” project or if you have already an open project, then select File menu – New – New project and from the list select Empty Activity.
Step 2 – Settings
We have installed Git on our computer, but still we haven’t set the path of it in Android Studio. Without this settings, still we can’t use the version control system. So if your project is ready to start, select from the File menu the Settings option.
In the Settings window expand the Version Control, and there select the Git option. There will be a line called “Path to Git executable”, and a text field with the hint “Auto-detected: git.exe”. If you have changed a default path in the Git’s installer, then with the 3 dots you can browse it.
There is a “Test” button next to the field of the path.
Click on it.
After the install of Git and the configuration the next step is to enable it for our project.
Select from the top menu the VCS, then the “Enable Version Control Integration…”
In the popped up window, select Git as the version control system.
After clicking on the OK button, the Android Studio will create for the project the Git repository in the folder of the project.
Now all of your files will turn red. It means, that they haven’t been uploaded to the repository yet.
Share on GitHub
Next step is to upload the project to GitHub.
Select again the VCS menu, then Import into Version Control -> Share Project on GitHub.
Now in the window, you have to login to your GitHub account.
Click on the “Share” button.
For the initial commit leave all of the files checked. It means, these files will be uploaded to GitHub.
If you are done, click on the “Add” button.
Check out your GitHub. If you have done everything correctly, then you should see there the initial commit.
After the initial commit we can start working on our project. From now on when we make changes in the committed files, then they turn blue. If we add a new file, then they will be red. It means again, that they aren’t in the repository yet.
Add to your code a simple Toast in the onCreate() method.
At the bottom of the window there are some tabs, where you can find the Logcat tab also. Here you can see a new one, called Version Control. If you open it, then you can check what kind of changes do you made since the last commit.
If you press the green check mark or just press Ctrl+k / ⌘+k then you can commit the changes. Here again you can give it a message, what has to decribe your changes. At the bottom of the window we can see the changes.
Click on the “Commit” button.
In our case we use GitHub as a central repository. It means the codebase is on a “central” server and people retrieve files fom it and commit to it.
Git refers to the centralized server as a “remote repository”. The remote repo is usually not on your machine and is the one shared by the team. The team “pushes” commits to it when ready to share with the team.
So our last step to have the changes on GitHub is to push them. For this click on the VCS top menu, then Git, then in the submenu there will be the Push option with a green up arrow.
We can use the shortcut also Ctrl+Shift+k / ⌘+Shift+k
In the pupped up window you can select to which branch we want to push in. Leave the first row as a selected, and click on the “Push” button.
Open your GitHub account, where you have created the repository for this project. There you should see, that you made a change and you committed and pushed it to the server.
To see the newest source code, so what we committed now, navigate to the MainActivity. In my case it looks like this
Imagine that you want to add to your existing code a new feature, but still you don’t want to change the original code. What if you can make a copy, where you can test your code? This is when branches come to the fore. Branching allows to make a “copy” of your working project and change it without affecting the main branch.
After the implementation of the feature or bugfix is done, you can merge it with the master branch.
Creating a new branch
When we have added the version control system to our project, we created a master branch also. It means, this is the default. So to implement our new feature in our app, we will create a new branch and later on we will merge it to the master branch.
There you can see Git: master.
Click on it!
We will name the new branch as “toastTestFeature”.
Leave the check mark of the “Checkout branch” checked, because in this case Android Studio after the creation will check out automatically this branch, and we can start immediatelly the implementation of our new features in the code.
If you are done, then click on the OK button.
Our new feature will be a new Toast. So click on the line of the first Toast and press Ctrl+d / ⌘+d. It will duplicate the current line.
Change the text of the Toast to something else.
Commit & Push
After the changes, we gonna share our new feature with another developers. In this case they can check our code and if they have some change request, then they can do it as well without changing the master branch.
Press again Ctrl+k / ⌘+k to make a commit. The message in my case will be “Test Toast branch”.
Click on the “Commit” button.
Then press Ctrl+Shift+k / ⌘+Shift+k to push our changes to GitHub.
Now open your GitHub account, where you have the repository of this project. After the push, you should see this line.
Click on the green “Compare & pull request” button.
Because our code is not able to autmatically merge to the master branch, we have to create a pull request.
Pull requests let you tell others about changes you’ve pushed to a GitHub repository. Once a pull request is sent, interested parties can review the set of changes, discuss potential modifications, and even push follow-up commits if necessary.
If our code don’t have any conflicts with the code of the master branch, then we should see the window below.
Click on the “Merge pull request” button then confirm it.
Now, if you navigate to again the MainActivity, then there you should see the new Toast.
That’s all. You just learned the basics of the Version Control System and the usage of Git. Congrats 😉
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂