Using Git in Java Projects
Git is a Distrubuted Version Control System (DVCS) that is fast becoming the defacto standard in many software projects. Many Java Projects are switching from Subversion (SVN) concurrent version control to Git.
The main reasons for the switch are:
- It is easy to develop offline or when traveling using Git. Neither a central server or an internet connection is needed to commit the code changes.
- Git is faster than Subversion.
- Git is well documented and has a strong open source community.
In this article I will show you how to integrate Git in your Java Project.
If you are a Linux user, then you probably have Git out of the box. If not then use the following command to install it:
sudo apt-get install git
If you are running a Windows machine, you can download it for free from git-scm.
In the following screenshots are my recommendations for Windows users:
Git has an embedded version of the Bash shell from Linux, which we will use to create our repository.
Using Git in Java Projects
Setting up a new git repository
After installing Git on my machine, I use the Git Bash terminal to navigate to the Java project HelloGit directory.
The first step is to set up a new repository. A Git repository or repo is a container for your entire java project. To create it type the
git init command, as shown here:
A repository is physically a special .git folder.
Then you perform a set of one-time setup steps:
$ git config --global user.name "Your Name"
$ git config --global user.email email@example.com
Under the java project directory I create a special file named .gitignore:
Git will read this file and skip the files and folders I listed inside it. This is often necessary when you work with temp or personal files that we don’t want to commit in the repository.
git status command, we can check the status of the repository, as shown in the following screenshot:
To add files to the repository we use the
git add command that tells git that we want to take care of this files and check them for future modifications.
At this point Git knows about .gitignore and the src folder content, we have now commit this files to the repository using the
git commit command. The
-m subcommand add a message or comment to our commit:
After creating the local git repository, we can configure Eclipse to integrate git in our HelloGit project:
Creating a remote repository
To have a central repository where all developers can push their changes to it, we create a remote repository. A remote repository is nothing other than remote copy of the same Git repository we created locally. There are many services that offer room for Git repositories like BitBucket, GitLab or GitHub. In this tutorial I will use GitHub.
GitHub offers unlimited free public repositories, you have to pay only if you need private repositories. Creating a new account is simple just go to GitHub and sign up using your email. After that you can sign in to create a new repository as shown here:
Click on the green Create Repository button:
Now, we have a remote repository:
To push my local repository to the github remote repository, we open Git Bash and type the following command:
git remote add origin https://github.com/hrbenkirane/HelloGit.git
Then I push everything to my remote repository using the command
git push -u origin master:
We have configurated Eclipse earlier to use Git so that we can commit our changes and work with Git without using the Git Bash terminal:
To learn more about Git, I recommend the following resources:
Until next time, Happy Coding!