In an ideal world, One should be able to identify the product maturity model by merely looking at the commit logs for the main/master branch in the git. But it's possible only if, each commit represent a feature Featureor change or bugfix or at least for an incremental atomic change.
Problem
Having only meaningful commits directly contradicts what I call as “no code left behind” principle. Developers shouldn’t leave any code on their development machines. Instead, the code should be committed and pushed to the remote repo, every day before logging off.
But sometimes one day is not enough to complete even incremental atomic changes suitable for a PR, let alone the code for the full feature. In those cases, this everyday push to the remote repo brings a lot of commits in the final Pull Request aka PR.
In this post, I will show how can we combine all these commits before raising a PR, or as they call MR in GitLab.
Solution
There are two ways, I’ve used in the past to combine commits. One is by using rebase and another by reset.
NOTE — both these processes change commit history in the remote repo. So DO NOT use anything explained below if you are working on a shared branch (branches where other developers also push & pull the code). All this should be used to fix the commits on the branches, only you are working on.
Let's create the problem
Setup a repository
Let's create a local git repo and add a README file in the master branch.
Let's assume, a user story was assigned to me to add a few lines of description to the project readme file. Because of my development speed, I am able to add only one line a day. And I push my code to the remote repo every day before logging off from work.
On day 4, when I tried to create the PR, I realized I have these 3 commits for just one logical change. This will clutter the final project history
So here are the steps I can take to make my PR look good
Squash commits using Rebase
We want to meld the last 3 commits into one, so we will do the interactive rebase on the 3 commits prior to HEAD using the following commands
git rebase -i HEAD~3
Once you press enter, the following content will show up in whichever editor is configured in your git.
Content of this file
The lines prefixed with # are the comments for some help documentation provided by git. The remaining lines at the top are the commits that we want to work on during this rebase.
NOTE- Unlike git log, in the rebase output the latest commit is displayed at the bottom and the oldest at the top.
Each line has the following 3 records. <rebase-action> <commit-hash> <commit-description>
For our use case, only the following 2 rebase-actions are needed.
fixup (f) to meld mentioned commit into the previous commit and ignore mentioned commit’s message
reword (r), To change mentioned commit’s message.
So, in my editor, I will write either f or fixup (both work same) in front of the commits I want to meld. Here is what my final file will look like.
Save this file and you will get such a message.
NOTE- You might not see a successfully rebased message if you’ve contradictory changes between melding commits. In that case, you will resolve conflicts as you usually do. I try to avoid such melds.
We’ve changed commit history, so we will not be able to push the local repo to remote. So we’ve to use force push
git push -f origin HEAD
Now I can go back to Github and raise my nice-looking PR
Rewriting new commit using Reset
If you’ve read my second post in the git series, you might remember, how git reset -— <option> <pointer>affects repo-tree, staging-area, & working-treedifferently, when used with different options.
For our use case where we’ve 3 commits that we want to meld into one, we will do a soft reset on HEAD, going back to 3 commits.
To verify, run the command git log. you should see 4 commits. a first commit, followed by 3 commits where we changed the readme.
run git reset — -soft HEAD~3
Run the command git log again. Now you should see only the first commit. However, when you open the readme file, you will see all the changes from the last 3 commits are preserved.
So now we’ve all our changes in the staging area and no commit in the repo tree. All we need to do now is to commit these changes with a clean commit message and push them to the remote repo (force push)
git commit -m "Adding Readme description"git push -f origin HEAD
Now go ahead and raise a PR on GitHub.
Conclusion
That's all we had for this topic, but before I close, Two things to note here are
1. We are using a force push to remote. Avoid it if you do not completely know what you are doing.
2. Avoid usingrebase or reset when you have merged or rebased commitsin between the commits you want to meld.
Since the beginning of personal computers, few keyboard shortcuts are common among all operating systems and software. The ubiquitous cmd+c (copy), cmd+v(paste) , cmd+z (undo) and cmd+y (redo) I am not sure why, both of my favorite IDEs, Visual Studio Code & Intellij decided to not use cmd+Y for redo.Below are the quick steps to configure cmd+Y for a redo in VS-Code & Intellij Visual Studio Code Open VS Code & Go to keyboard shortcuts There will be a search bar at the top Type “ redo “ in the search bar. You can see on my system its still mapped to shift+cmd+z Double click on ⇧ ⌘ z and the below box will appear. Do not click anywhere or type anything on the keyboard except the key you want to assign, in our case it was cmd+y, so type cmd+y Press Enter and you are done. Now you can use cmd+z for undo and cmd+y to redo like always Intellij It is also as simple as VS-Code...
It's a common use case to have an enterprise application, perform specific work, at a specific time or in response to a specific action. In other words, “There is an ask to execute a Job upon a predefined Trigger ”. This brings us to the need for a Scheduling System. A system, where Jobs & Trigger can be registered and the system will manage the remaining complexity. Thankfully for the Java systems, Quartz is for rescue. It‘s an open-source library that has been extensively used in enterprise applications for more than a decade. Components in Quartz Sub System: Following are the all major component in the Quartz subsystem: Scheduler : It’s the control room of Quartz. It maintains everything required for scheduling, such as managing listeners , scheduling jobs , clustering, transactions & job persistence. It maintains a registry of JobDetails , Listeners & Triggers , and exec...
If you want to avoid overpriced pre-builts like the M1 Mac Mini, Mac Pro, or Dell XPS Desktop without compromising on performance, a self-built desktop is a preferred option. It's also a great choice if you enjoy building things. custom built with ASUS-PRIME-P If you choose to build a custom PC, be prepared to invest time in researching and assembling compatible components. In this post, I'll share my experience building this colorful powerhouse. I'll cover: Why did I do it. Key questions to ask when selecting components Thought process behind component choices Components used in my build Benchmark comparisons . ** My second custom-build **. *** Disclaimer: Not an Apple product. Just a free apple sticker is used *** Why did I do it I decided to get a desktop during the pre-MacM1 era (yes, that’s a thing). After browsing many websites, I found that well-configured prebuilt PCs were overpriced, while cheaper ones had subpar components. Unable to choose betwee...
Once, I was working on a few geospatial APIs handling many time zones. While writing tests, I realized I did not know much about timezones. A lame excuse might be, my subpar schooling as a village kid. Nevertheless, I decided to turn the pages on timezones, what I found was more politics than science. Photo by Arpit Rastogi on Unsplash Before diving into anomalies, let’s talk about history then we will go to science followed by politics. History The world without time zones By 300 BCE, the western world agreed that the earth is round. Each developed civilization devised its unique distinct system to measure distances, times & absolute locations, but relative to prime locations within their civilizations. It all worked in ancient times because long-distance travel was not prevalent among common people. Only merchants or armies traveled long distances. And they already developed systems that worked on their predetermined routes, irrespective of the time differences b...
During our java based microservice development, we extensively use build tools like Maven or Gradle. Usually, IDEs do a lot on our behalf or we just run some predefined commands without checking what's happening inside. Here in this series of 6 posts, I tried to explain Maven. Before I start talking about what Maven is, and its different components, let’s discuss the “why”. Why do we even need Maven? For this, I’ve to first explain the nature of a Java-based project and also need to take you back in history. The “Build” Step. Java is a compilable language, Unlike Python or Javascript, which are interpreted. ie, the code we write in java, can not as-is run on a Java virtual machine (JVM). JVM understands only the bytecode. Therefore, in the Java world, there is always a need for an intermediary step. A step that compiles the java code files into bytecode. That's why after writing the java code, we “somehow” create some deployable (jar, war, ear) to run on ma...
A wise man ( narcissist me ) once said, “Life is all about the question and answers. The trick to a meaningful life is, To ask the right questions to yourself, so you can get on the right path to search for the answer .” The very first question one should always ask oneself is WHY. Let's discuss our WHY in the current case. Why BDD Let's take a step back and start with the well-known software development practice TDD ( Test-Driven Development). In TDD, the very first thing developers do is, set up the technical expectations from the code by writing failing test cases. After the expectation is set, the code is written/modified to finally pass all of the failing tests. It's an Acceptance driven development strategy . TDD works fine to create a robust technically working product. But the whole TDD approach revolves only around technical teams. It barely involves the business analysis or product owners to validate the business aspect of a feature, they get involved o...
Comments