Friday, June 14, 2019

Combine multiple git commits into a single commit in GIT using squash



Introduction

As a developer, one wants to keep the commits cleaner. At the same time, while developing I also want to keep the detailed record of changes I made and commits I make. From the start to the end, depending on scope, I might have made many trivial commits and after I am happy with the changes, I would like a few (sometimes one while at other times multiple) commits to indicate what changed, rather than to have several commit messages.

Squash in GIT can be used to combine multiple individual commits into one. This makes commits in the repository cleaner. For illustration, let’s say that we have about 25 commits from the beginning through the end of development on an issue. During those 25 commits, several code changes were made which may not be of high importance to remain in the repository. Those 25 commits can be wrapped to a single commit before it can be merged into a master branch.

The rebase command in the interactive mode (i.e. -i or –interactive) can be used to combine those comments into one.

I am using git bash in this article and I have 25 commits on a branch which I want to modify to have just one commit reflecting a feature has been completed.

Git commit history


Rebasing

Run the following command to ensure that I am picking 25 commits in the branch I am in.
git rebase -i Head ~25
Git Bash Window

The above command is instructed to get 25 commits from the head. Upon execution, the git opens a text editor showing the commit details (messages) of the last 25 commits.  

Editor to mark commits as squash or pick

I can edit the commits by putting ‘Squash’ on all the commits by changing ‘pick’ to ‘squash’ and leaving one commit with ‘pick’, probably the first one in the list i.e. the latest. We can change that message later on. In the example, I am picking the first one as shown in the figure: 

Editor after marking the commits as squash or pick


Save the changes in the editor by clicking the save button on the top left and close by the window by clicking ‘x’ on top right.

After rebasing in completed, the branch will change to show the commit message what had ‘pick’ in the editor.

Updating Commit Message
The commit message may not be relevant at this point and thus one may want to change the commit message. This can be achieved by
git commit –amend
The above command will open an editor just like before showing all the previous commit messages that can be changed to relevant message and save and close the editor. After which git would have updated the message.

Handling Remote
If the branch was previously pushed to remote, we need to synchronize the commits to remote as well.

Since the branch appears different in remote it might find conflicts. Thus, we must force the local branch which will replace the remote with the latest local changes by squash.
Git push –force

With the rebase, amend and force I was able to combine the twenty-five smaller commits into one with one clear message, both in remote and local.

Monday, June 10, 2019

Interacting with Microsoft Access Database in .Net Core Application

I had to work with a program to read/write Microsoft Access Database and I was using .Net Core 2.2 application for processing a large number of files. I later found out that Oledb is no longer supported in the .Net Core world, and then I had to explore additional options. While the Entity Framework option is there, but it would be overkill for the programming needs for the project. I looked into the ODBC based approach and it showed the promise.  After some issues and errors, I was able to successfully connect/interact with the access db.

The AccessDatabaseEngine_X64.exe needed to be installed in the machine. The error message that is thrown due to the missing correct driver (I had 32 bit version before) was vague so it was not immediately evident, but eventually, it worked out.

In this article, I am going to discuss the steps to take in order to connect to access db from within a .Net Core 2.2 application written in C# using Visual Studio 2017.

The content of app.config is fairly simple as shown below which stores a key value for datapath:

The following code snippet shows a method named 'ProcessFile(..)' demonstrating demonstrates the use of interaction with Access. The method processes the *.accdb file in the folder path, an input to the method itself. The method looks into the folder path for an access db file. If the file is found, a connection is made and names of the tables (user tables only) are written in the console. In case of error, it writes the error message.