5 minutes
Devoxx BE 2025: It Only Starts with a Container & How Abstraction Becomes Reality
At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]
Git, the weirdly named version control system developed by Linus Torvalds, is an amazing tool for developers, and us Java developers are no exception. While most of us are familiar with the basics like git commit, git push and git pull, Git has a lot of lesser-known features that can significantly improve your workflow. In this post, we’ll explore five hidden Git tips tailored specifically for Java development. These tips will help you manage your codebase more efficiently, debug issues faster and collaborate more effectively. Let’s go!
Java applications often have complex codebases, and tracking down the exact commit that introduced a bug can feel like finding a needle in a haystack, or rather a snowflake in a blizzard? Anyway, git bisect is a powerful git feature that conducts binary search through your commit history to identify the problematic commit.
The git bisect command helps you find the exact commit that introduced a bug by performing a “binary search” through your commit history. It works as follows:
git bisect start
git bisect bad
git bisect good <commit-hash>
If the bug is present, mark the commit as bad:
git bisect bad
If the bug is not present, mark the commit as good:
git bisect good
git bisect reset
When working on a large Java codebase, it’s common to come across code that’s difficult to understand. git blame is a handy command that shows you who last modified each line of a file and when. I think this command could have been named something else than ‘blame’, but I am digressing.
Run the following command to see the commit hash, author and date for each line in a file:
git blame <file-name>
For example:
git blame src/main/java/com/example/MyClass.java
Use the -L flag to focus on specific lines. For example, to see who modified lines 50 to 60:
git blame -L 50,60 src/main/java/com/example/MyClass.java
We often switch between tasks, such as fixing a bug while working on a new feature. git stash allows you to temporarily save your changes without committing them, so you can switch branches or tasks cleanly. Think of git stash as a temporary shelf for your code changes.
git stash
git stash pop
Use git stash list to view all stashes and git stash apply <stash-id> to apply a specific stash.
Java projects generate a lot of build artifacts (e.g., .class files, target/ directories). Over time, these files can clutter your workspace. The git clean command removes untracked files, helping you keep your workspace clean.
git clean -n
git clean -f
git clean -fd
Use git clean -x to remove ignored files (e.g., .idea/ or .classpath) too.
Git hooks are scripts that run automatically before or after Git events like commits, pushes or merges. We can use git hooks to enforce coding standards, run tests or trigger builds.
chmod +x .git/hooks/pre-commit
Add your custom logic. For example, a pre-commit hook to run unit tests:
#!/bin/sh
mvn test
if [ $? -ne 0 ]; then
echo "Tests failed. Commit aborted."
exit 1
Fi
This is a simple pre-commit hook that aborts a commit if tests fail. You could also automate code formatting with tools like Spotless in a similar pre-commit hook.
We often work on multiple branches simultaneously, such as feature, bugfix and release branches. Such is the nature of all enterprise Java applications. Switching between these branches can be cumbersome if you have to type out the full branch name each time. Fortunately, Git provides a shortcut to quickly switch back to the previous branch you were on.
The git checkout – (the dash symbol) command allows you to switch to the last branch you were working on without needing to specify its name.
For example:
git checkout feature-branch
git checkout main
git checkout -
This command will switch you back to feature-branch instantly.
Java projects often involve working on multiple branches for different tasks, such as feature development, bug fixes and hotfixes. The git checkout – command saves time and reduces the risk of typos when switching branches, making your workflow more efficient.
You can combine the power of git log –grep and git checkout – to streamline your workflow even further. For example:
This combination allows you to efficiently navigate through your commit history and return to your original context without losing track of your work.
Git is a versatile tool with many hidden features that can significantly enhance your productivity as a Java developer. Whether you’re debugging with git bisect, managing unfinished work with git stash, or quickly switching branches with git checkout -, these tips can help you work smarter, not harder. Incorporating these techniques into your daily workflow will help you manage your Java projects better and focus on writing great code. Of course git is not limited to just Java projects. These tips apply to any project that you are tracking with git.
So now, what is your favorite Git tip? Share it in the comments below!
Share:
5 minutes
At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]
5 minutes
Welcome aboard the October issue of The Monthly Catch!As the leaves turn and conference season hits full stride, the […]
2 minutes
How outdated Java systems are draining budgets and throttling innovation across financial services? Let’s dig in in this blog […]