How to get Git information in Java Application

In this post, we are going to see how to get the GIT information in a Java application.

To do that, we have to use the maven-git-commit-id-plugin plugin. Refer it @maven-git-commit-id-plugin

Now let’s see how we gonna do that.

1. Include the below plugin configuration in your Maven POM file.


  <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.1.13</version>
                <executions>
                    <execution>
                        <id>populate-git-commit-information</id>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                        <configuration>
                            <verbose>true</verbose>
                            <dateFormat>MM/dd/yyyy HH:mm:ss Z</dateFormat>
                            <abbrevLength>8</abbrevLength>
                            <generateGitPropertiesFile>true</generateGitPropertiesFile>
                            <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                </configuration>
            </plugin> 
            

2. Then do maven install (mvn install). You can set the verbose as false if you don’t want to see the git commit id plugin log.

3. After the build is done, then go to target/classes folder. You will see git.properties contains the git related information. You can read this properties file in your application and use it.

Sample code is given below.


 static Properties properties = new Properties();
    static{
        try (InputStream is = this.class.getClassLoader()
                .getResourceAsStream("git.properties")) {
            properties.load(is);
        }
        catch (IOException io) {
            io.printStackTrace();
        }
    }

File Name: git.properties
File Path: /target/classes/git.properties
Sample properties are given below.



#Generated by Git-Commit-Id-Plugin
#Fri Nov 17 09:49:44 EST 2017
git.tags=
git.commit.id.abbrev=8ef4712a
git.commit.user.email=dkbalachandar@gmail.com
git.commit.message.full=Update Dockerfile
git.commit.id=8ef4712acd077d89b875c5bf67c20af1dbb8bb24
git.commit.id.describe-short=8ef4712-dirty
git.commit.message.short=Update Dockerfile
git.commit.user.name=Balachandar
git.build.user.name=Balachandar
git.commit.id.describe=8ef4712-dirty
git.build.user.email=dkbalachandar@gmail.com
git.branch=master
git.commit.time=09/19/2017 18\:25\:16 -0400
git.dirty=true
git.build.time=11/17/2017 09\:49\:44 -0500
git.remote.origin.url=https\://github.com/dkbalachandar/java-rest-docker.git


Refer my code @java-rest-docker

Advertisements

Git: Pushing remote branch to Gerrit

If you use the Gerrit as your code review tool, then follow the below steps for pushing the remote branch to Gerrit.


1. Create a local branch to track for the Remote Branch. Run the below command for the same. 

 git branch LOCAL_BRANCH_NAME origin/REMOTE_BRANCH_NAME 

2. Then try to merge your local branch with master branch to avoid any conflicts. This will be helpful when we merge the master with REMOTE_BRANCH_NAME in the future.

 git merge origin/master 

3. Make the necessary changes in the code and run the below commands to add and commit it. 

 git add . 
 git commit -m "COMMIT_MESSAGE" 

4. Run the below command to push the code to gerrit for the remote branch 

git push gerrit LOCAL_BRANCH_NAME:refs/for/REMOTE_BRANCH_NAME 

How to merge local branch with master[Git]

In this post, I am going to show the step by step instructions for merging our local branch with the master.

Please follow the below steps,

1. Go into your GitHub repository or your project repository
2. Create a local branch with the below command,

git branch local-branch

3. Then move into that branch

git checkout local-branch

4. Then make the relevant changes and issue the below commands to commit it

  git add .
  git commit -m "commit message"

5. Then move back to master branch and merge it as below

  git checkout master
  git merge local-branch
  git push origin master
   

If you see any conflicts while doing push, then do rebase and then push.

Remove Git Repository History

Please follow the below steps to completely remove the commit history

1. Clone the Git repository
2. Run rm -rf .git to remove all the .git files
3. Then run the below command to initialize the repo again and commit the changes


git init
git add .
git commit -m "Commit message"

4. Push the changes to Git


git remote add origin REPO_URL
git push -u --force origin master