Using Git in SQLDeveloper

As I write, South Africa have just emerged victorious from the 2019 version of the Rugby World Cup having vanquished England in the final.
This explains both the code used in the examples that follow and the fact that I’m writing this to escape from the “commiserations” pouring into my phone from my Welsh In-Laws. Tell me, what is the Welsh for Schadenfreude ?
Continuing my SQLDeveloper appreciation fest, I’m going to look at the latest version of SQLDeveloper’s (19.2) level of integration with Git.
Specifically, what I’ll be covering is :

  • Using the SQLDeveloper Files Tree to work with a local Git Repository
  • Creating a branch
  • Staging changes
  • Commiting changes
  • Comparing different versions of a file within a branch
  • Comparing branches
  • Merging branches
  • Deleting branches

We’re going to do all of this without leaving the comfort of our favourite Oracle IDE…

Environment

Running SQLDeveloper 19.2, we’ll be working on a local Git repository using Git 2.7.4.
I’m doing this on Ubuntu but SQLDeveloper seems to behave in the same way on Windows. I haven’t had the chance to do any comparison on MacOS but I’ve no reason to believe it won’t be the same story there too.

Changing your perspective

By default, SQLDeveloper views the world through objects in the database :

Fortunately, it can also be persuaded to look at Files on your computer.
To achieve this, simply go to the View menu and select Files :

If you start exploring this view, you may well find that you’re seeing spots in front of your eyes :

Don’t worry, it’s not contagious. It’s just SQLDeveloper realising that this directory is actually a local Git repository (repo).
We can find out the branch that is currently active by right-clicking any member of the repo and selecting Versioning/Properties.
In this case, we can see that we’re on master :

We want to make some changes to this application code. We want to create a branch to do this so…

Creating a new branch

Right-click on one of the repo members and select Versioning/Create Branch :

Enter the name you want to give the new branch. In my case find_winners
Then click Select Commit and choose the commit from which you want to branch. For me it’s usually the latest one, which is just as well in this case :

I want to switch to the new branch as soon as it’s created so I need to check Checkout Created Branch :

Click OK and I’m now in the new branch. To check, I just look at the properties window of one of the repo members again ( in my case it’s the rwc root folder) :

I’m going to edit the package header file because I want a function to tell me who the winners were for a given tournament :

create or replace package save_finals as

    function get_winners( i_year rwc_finals.year%type)
        return countries.country_name%type;

    procedure add( i_year rwc_finals.year%type, i_host rwc_finals.host_cid%type, 
        i_team1 rwc_finals.team1%type, i_team2 rwc_finals.team2%type);
        
    procedure result( i_year rwc_finals.year%type, 
        i_t1_score rwc_finals.t1_score%type,
        i_t2_score rwc_finals.t2_score%type);
end save_finals;
/

I can see that the icon for the changed file is now different in the File Tree :

Furthermore, if I want to see what has changed since the last Git checkin, I can simply click on the History tab in the code editor and select the last Git checkin:

Whilst I’m here, I’d like to add another view to my application, so I’m going to create a new file for it :

create or replace view roll_of_honour_vw as
    select year, save_finals.get_winners(year) as champions
    from rwc_finals
/    

Now I’ve saved the file, I can see that it has a different icon in the Files Tree :

Right, I want to start saving my changes to Git. First up then…

Staging changes

Now, it is possible to stage your files one-at-a-time by right-clicking the file and selecting Versioning/Add.

However, if you select Add All instead, you get a list of all candidates for staging and you can then remove any files you don’t want using the Select checkbox.

When you’re happy with your selection, hit OK

We can now see that the file icons have changed again. The edited files ( save_finals.pks and save_finals.pkb) now have a green spot, whilst the newly created roll_of_honour_vw.sql now has a big plus icon :

Incidentally, if you have a file in the tree that you don’t want git to manage, you can add it to the .gitignore file from the same menu.

For example, I’ve added a todo list so I can keep track of what I’ve still got to write ( I can’t quite afford JIRA at the moment)

If I right-click on the file and select Versioning/Add to .gitignore, I’ll get :

Once I’m happy with my selections, I hit OK and the file icon changes :

Committing changes

The process for committing changes is similar to staging them. Right-click a repo member and select Versioning/Commit All…

Note that we we could include non-stages files in the commit by checking Commit non-staged files.
Finally, we add an appropriate commit message :


Just as SQLDeveloper will allow you to compare the saved version of a file with the last committed version in the repo, you can also compare the file from two different commits :

If we open save_finals.pkb and switch to the History tab, we can Filter to view only Git commits :

If we now select the previous Git checkin, we can see what’s changed :

Alternatively, you can achieve a similar effect by right-clicking the file then selecting Versioning/Version History.

Comparing branches

SQLDeveloper will even allow you to compare different branches.
First, you need to go to the Teams menu and select Git/Branch Compare

Remember, where on the find_winners branch so we need to select the master branch from the drop-down :

You can compare the versions of a file in each branch by right-clickikng and selecting Compare : compare on a file :

Merging branches

We’ve finished our enhancement and we’re happy with it. It’s now time to merge the changes back into master.
Before we get to merging, we need to switch to the branch that we want to merge to ( in this case, master).

So, from the Team menu, select Git/Checkout

Now click Select Branch and choose the branch we want to switch to ( in our case, Master) :

Finally, hit the OK button and we’re on master.

Next we need to go back to the Team menu and select Git/Merge

Select branch you’re merging from and select a commit…

The merge Window now looks something like this :

Hit OK

If we now look at the Commit History ( Team/Git/Commit History…) we can see that the latest commit matches the one that we selected to merge :

We can also see the list of files that formed a commit by selecting it in the history pane:

Deleting the branch

Now we’ve finished with the branch, let’s do a bit of tidying up.

From the Team menu, select Versions. This will bring up the Versions pane.
Here, we can expand the Git node until we can see our local branches :

The current branch has a green icon.

To delete the find_winners branch, we simply select it in the tree and then click the big red cross :

…and it’s gone.

References

Everything I’ve covered here involves using a local repository.
Blaine Carter’s Using Git with Oracle SQL Developer video includes steps to connect to a remote repository.

Galo Balda has also written a series of posts on using Git with SQLDeveloper. You can find them here.

Author: mikesmithers

Back in 1993, I discovered that I could get paid money for doing fun stuff with computers. Over the years, I've specialised in Oracle Databases as a developer, a DBA and sometimes, an architect. It's my evil alter-ego - The Antikyte - who writes a blog about my various technical adventures. Yes, that is his Death Star parked in the Disabled Bay. I currently live in the South-West of England with Deb, my long-suffering wife.

5 thoughts on “Using Git in SQLDeveloper”

  1. Very Informative and useful !

    Can you please advise on Git hooks on Sql Developer. I have a commit-message hook script that works absolutely fine when performing commit from command line or git-gui. But when I try commit from sql developer, the git hook is not triggered.

    Like

    1. Khader,

      I don’t use git hooks myself so this is not something I’ve come across.
      If it’s a client-side hook then it may be related to an environment setting which is not available in SQLDeveloper.
      Other than that, I don’t really have any ideas.
      Sorry,

      Mike

      Like

  2. Is it possible to connect GIT directly to a schema and have it track any changes being made to procedures, views, triggers, etc… under that schema? So when you commit it writes the file directly back to the live schema.

    What I got working didn’t quite work that way. I created a repository and copied a few of my .sql files to that repository. I was able to check them out and back in but they are writing the file to the repository not to syncing it back to my database schema. I must be missing something.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.