Miscellaneous Tips for Development

Miscellaneous tips for development in Git and Maven.

I’m currently learning many different programming languages and frameworks. During the work, I found some interesting tips for accelerating the development. Here, I want to share them with you.

Git: show the staged difference in CLI

git diff --staged

git diff is a useful command to see the staged difference of all files which are staged for the next commit. --cached means show the changes in the cache / index against the current HEAD. --staged is a synonym for --cached. Therefore, we don’t need to launch a GUI to check the staged files anymore!

Source: Stack Overflow - How do I show the changes which have been staged?

Git: protect the master branch!

I wanted to try some git features on Windows 10, but I didn’t setup anything like username and useremail on my global settings. After cloning my project java-examples and modified some code, I push my code the master using git push origin master. And it works?! This shouldn’t be allowed actually!! Because I didn’t setup my account and as an anonymous, I shouldn’t be trusted. You can see from the recent git log that the commit ebb724e do not possess a valid author email.

$ git log --pretty=format:'%h %ae'
36ee504 mincong.h@gmail.com
300337c mincong.h@gmail.com
9320fa1 mincong.h@gmail.com
b1e93b0 mincong.h@gmail.com
e0f3928 mincong.h@gmail.com
35c6a70 mincong.h@gmail.com
500759d mincong.h@gmail.com
8c56336 mincong.h@gmail.com
ebb724e Mincong HUANG        # Invalid!
71a33b0 mincong.h@gmail.com
7355062 mincong.h@gmail.com
dd7b913 mincong.h@gmail.com

It means that the commit ebb724e is a force-pushing commit. In order to prevent this situation happens again, we need to protect the master.

Protect the Master branch from GitHub

Maven: using slash is safe on all platforms

In maven configuration, the file separator ${file.separator} is used to ensure a good interpretation in both Unix-like platform and Windows. However, this isn’t necessary: using a simple slash / is safe too! According to T.J. Crowder:

With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.

You might want to use File.separator in UI, however, because it’s best to show people what will make sense in their OS, rather than what makes sense to Java.

So, it’s up to your choice to work with either slash / or File.separator. I tested both solutions on my Windows 10, both work.

Source: Stack Overflow - File.separator vs Slash in Paths

Linux: configure Maven in Ubuntu

  1. Install OpenJDK 8
  2. Set JAVA_HOME
  3. Download Maven using sudo apt-get install maven