git with salesforce locally

Git with salesforce locally

Sometimes I see Salesforce developers( or any other developer as well 🙂 ) are afraid of the term SCM (source code management). As a developer should we be caring about it ? Answer is, yes, we should be using SCM for our own benefit. At least you can use git with salesforce locally. Wait locally?? Yes, locally, it’s really useful some times. I will go thorough the SCM use for a development team at some other time. But today I will post about the SCM of my choice, git. So, let’s start….

Why locally?

Some time you would save a lots of time if you have a local versions of the code you are working on.If you are already using SCM with team you probably don’t need this. But some time you may not be working with a team, so may not have a repo for the Team. Eclipse and other tools keep a track of your changes, but again if you have to,for some reason re-install these IDEs’, you may loose the change history. So, a git repo may be useful in that case. I do it personally though, when I am working on some code with out a team, and thought of sharing with you. Will share using git repo on the internet(Social coding as they say it) sometimes as well in future. But this post is about “Git with salesforce locally”.

Git with salesforce locally

I am assuming you know about some of the git stuff. Here in this post I will be using the git and IntelliJ IDEA Community Edition. But there are much more that can be done. I assume you have installed git and IntelliJ IDEA (or any IDE) already. Will use the IDE to just pull the metadata from Salesforce once.

Git: Can be downloaded from Git Site
IntelliJ IDEA: Can be download from JetBrains Site (Community edition is free). I am using the illuminated cloud plugin for Salesforce support with it.

Creating a base

We can start with creating our own code base. Then we can keep modifications on top of that. I am using IntelliJ but in Eclipse also steps are almost same with their own ways.

In IntelliJ, you can go to File > Project and select Illuminated Cloud

git with salesforce locally
git with salesforce locally

Setup your connection to get the salesforce org connected.

Connect to Salesforce
Connect to Salesforce

After this is done you will see your project folder somewhat similar to this:

Directory listing
Directory listing

Right click anywhere in this directory and if you have installed Git, you should see a menu like this :

The Menu
The Menu

I still like command line so selected command line here. You can see the below image , that this is not a git repo yet.

No git repo
No git repo

Let’s initiate a git repo here in this folder and add our files to that. So, to do that what I did is:
git init (hit enter)
The above command will initiate the repo. See the image it created a repository called “master
Next I executed command : git status (hit enter), this will give the list of file that need some action.

Git init

Now let’s add all the files to the repo by using :
git add –all (hit enter)
git commit -m “You comment” (hit enter)

files added
files added

We added all the files and how does it looks like in a GUI ? Well right click in the directory and go to the GUI option to get the below screen (may vary as per your directory name)

Git GUI
Git GUI

Let’s check how it looks like visually:

Visual Master
Visual Master

Visual list

New we are set with the master branch. Let’s branch out and work on them.

Branching

Let’s create a development branch and make some modifications to the code for this post. Branching is done to create a separate working copy, so that we can work on the code at a different branch without affecting the original code , we have in master. To do that we will go to Branch (menu) > Create > Type the name of the branch you want (in this case Development) and click create. This will create a branch and will checkout the code for you to start modifying.
Development branch

For this demo, I am not going to use IntelliJ for now, let’s just change a content in one of the triggers in notepad. I added a comment line as below:
Change content

Now if we open the GUI in the directory, you will see the changes I made is marked by the GIT. Smart enough to detect the changes.

Changed Detected

Click on Stage change and it will be added to the Stage box. Add a comment there and click on Commit. This step will add the changes to your development branch. Now if you go to the visualizer you will see something like the below screenshot. See the details it has tracked for you.

Development changes

I made some more changes to the code again and committed them. If I go to visualiser now it will show me below details it has captured:

Details

Deleting branches after merging

As I am done with the changes now, I can go ahead and delete the development branch after merging all the changes to the “master” branch. Remember to merge the branch to master before deleting. And for my next work I can checkout a new development branch again and the cycle will go on.

Let’s merge the branch first. Go to Merge (Menu) > Local Merge. Select the branch to merge with master and click Merge. You will see something similar.

Merge

For deleting the branch in GUI, go to the Branch (menu) > Delete > Select the Development branch and click Delete. Now you will have one repository as per the flow we did in this post. If you visualise now you will see :
after delete

There is no more Development branch.

Conclusion

It’s seems difficult initially. But , with regular usage you will love this flow for your own benefit. If you accidentally delete any code from your sandbox, you can always come back to the repo and get a copy of the older version of the code. Hope readers will like it and use it at some point. Keep questions coming in, if you have any. Happy to answer them. Here for this post, I used Git GUI and command, but there are better tool to manage things. Will discuss about one of the tools in the next post “SourceTree“. Sourcetree is free git client and I am sure you will like it. Keep tuned in…

Keep reading and sharing…


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *