Seth Barrett

Daily Blog Post: April 15th, 2023

java

Apr 15th, 2023

Using Git to Manage Your Java Project on Linux

In the previous post, we showed you how to create unit tests for your Java code using JUnit and Maven. In this post, we'll guide you through the process of using Git to manage your Java project on Linux.

Step 1: Install Git

If you haven't already, you'll need to install Git on your Linux system. To install Git on Ubuntu, you can use the following command:

sudo apt-get install git

Step 2: Create a Git Repository

To create a Git repository for your Java project, open a terminal window and navigate to your project directory. Then, run the following command:

git init

This will initialize a new Git repository in your project directory.

Step 3: Create a .gitignore File

A .gitignore file is used to specify files and directories that should be ignored by Git. Create a new file named .gitignore in your project directory and add the following lines:

target/
*.class

This will ignore the "target" directory and all .class files in your project.

Step 4: Add Your Files to the Repository

To add your Java code, test code, and pom.xml file to the Git repository, run the following command:

git add .

This will add all files in your project directory to the Git repository.

Step 5: Commit Your Changes

To commit your changes to the Git repository, run the following command:

git commit -m "Initial commit"

This will create a new commit with a message of "Initial commit" that includes all the files you added in the previous step.

Step 6: Push Your Changes

To push your changes to a remote Git repository, such as GitHub or GitLab, you'll need to create a new repository on the remote server and obtain its URL. Then, run the following command:

git remote add origin 
git push -u origin master

This will add the remote repository as the "origin" and push your changes to the "master" branch.

Congratulations! You've successfully set up Git to manage your Java project on Linux. In the next post, we'll explore how to use continuous integration with Jenkins to automate your build and test process.