During my J2EE and .Net days, I was much dependent on the subversion repository. I had never thought my code without SVN. When I moved to Salesforce few years back, the first thing I missed is code repository to have better source code control. As svn creates either “.svn” or “_svn” folder, it was not supported by the force.com IDE and I struggled much to get it work out. Salesforce saves only real time information. So it is very difficult to know what was changed in past related to any issue as well as complete code backup.

Era of Git

Git has emerged as the most powerful source code repository and widely accepted by the developer communities. If I will talk about Git this article is going to be out of discussion scope.

Architecture of GIT

Above figure describes Architect of Git. The one thing to notice in above figure is that Git has local repository as well as remote repository which make it better and more flexible than any other code repository tool.

Remote and Local Repository in GIT

As you can see in above diagram also, user can work on completely different branch which is stored on his local repository and when he thinks that he is ready to push the code on Git, just commit the changes on remote repository.

EGit is an Eclipse Team provider for the distributed version control system Git. EGit allows performing Git commands from the Eclipse IDE.

EGit is based on the JGit library. JGit is a library which implements the Git functionality in Java.
Most Eclipse 4.2 packages from Eclipse.org contain EGit in there default configuration. In this case no additional installation is required.

If EGit is missing in your Eclipse installation, you can install it via the Eclipse Update Manager via: Help ?Install new Software. EGit can be installed from the following URL: http://download.eclipse.org/egit/updates

Step 2:
Gets the Salesforce code in local workspace using force.com IDE.

Step 3:
Now, it is the time to configure repository on your local system which is also known as “Local Repository”. Navigate to “Right click on Project | Team | Share Project”

Share Salesforce Project in Eclipse

On the basis of your eclipse configuration, you will get options like CVS, Git, SVN etc. Select “Git” and click on Next button.

Step 4:
Click on “create” button as shown in below image

Configure Git Repository in Eclipse

A new pop up will open where we can give Local repository name as well as location where we want to save the local repository. In this article, I am using the name “GITDemo” name. Now click on Finish button.

Non Indexed Local GIT Repository

If you check the Salesforce project, the folder will look something like above image with questions marks (“?”) on folders. Question marks means Git does not know what to do with files, either you can schedule them for commit or add few of them in ignore list (means they will not saved in Git).

Step 5:
Now we will commit our files and folder in Local repository. Before commit we have to mention that which files and folder should be committed and ignored. In this case I am adding complete salesforce directory for commit. To do this, write click on “Project | Team | Add to Index”. After this operation your icons should be something like below image:

Indexed but Non Committed changes in GIT – Eclipse – Salesforce

It’s time to commit all indexed files and folders in local repository. Again navigate to “Project | Team | Commit”. New Popup window will appear and ask for comment. It is mandatory to provide meaningful comment for each commit as it will be very easy for diagnosis in case needed in future.

Commit Changes to Git Repository – Eclipse – Salesforce

After commit the folder structure will appear with following icons:

Committed folders in Salesforce Eclipse using Git

Step 6:
Until here, we have done configuration for Local repository. Let’s move on to create a remote repository. There are lots of options available for hosting your code which supports Git. Few of them are

Choice is up to us, which depends on license cost and many other factors. For demo, I am using the “BitBucket”.

Create a free new account at “BitBucket” and create a repository. After successful creation of Git repository you will get the URL for your repository. There will be two URL provided, https and SSH.
For this demo, I am using https URL. The URL format will be something like

In Eclipse, at upper right corner, change the perspective to “Git Repository Exploring” as shown in below image.

Git Repository Perspective in Eclipse

You will get the list of all Local Repository in this perspective. Now in Remotes, right click to create new Remote as shown in below image.

Create Remote Git Repository Eclipse for Salesforce

One new popup will appear like below

Git Create New Remote in Eclipse – Salesforce

First select the option “Push” and give name of remote repository Name. “Push” means you are going to save changes on remote repository and “Fetch” means you are going to fetch code from remote repository.
When you will click on Ok button new window will open.

Configure Push for remote repository – Git – Eclipse – Salesforce

In new window click on “Change” (1) button as shown in above image. Again new pop up window will open. At location 2 provide your complete Git https URL. At location 3 provide your username and password. Don’t forget to check the box at location 4 which says “Store in Secure Store”. Now click on Finish Button.

Git Ref Mappings – Eclipse – Salesforce

On Parent window click on Add (1) button. In new window, in Local branch text box and Remote branch enter “refs/heads/master” or you can press “Ctrl + Space Bar”.

After everything, click on “Dry-run” button to check, whether everything is properly configured or not? If everything is properly configured then it will show the repository name else it will show the error message.

Now click on “Save and Push” button to move changes from local repository to remote repository.

Hey Jiterndra, I am using git was writing an article on this – I am glad you wrote it here too. I’ll come with version of mine and will add the reference to your article as well – Great Post, keep adding your valuble articles

Thanks for the article. I have created both push and fetch repositories as mentioned in this article.

Its working fine when I push the code into remote repository from my local repository. While I try to fetch the last committed code from remote to local, I don’t see any latest code in my local repository. I see only the last saved code from the sales force server in the local repository.

can you tell me how this Fetch works? and how can we use this Fetch functionality if we have more than one developer trying to update the code?

Thanks for the blog. I have created both push and fetch repositories as mentioned in this article.

Its working fine when I push the code into remote repository from my local repository. While I try to fetch the last committed code from remote to local, I don’t see any latest code in my local repository. I see only the last saved code from the sales force server in the local repository.

Hi Jiterndra,
I am using git for sfdc deployment. I need to know that while doing deployment with git it has to run the all test classes likewise ANT(RunAllTest=”True”) doing.
Do we have any command or config like that?

We have started using Git and Bitbucket as our version controlling system. Also we are using Egit plugin as the GUI for git. We created a team in bitbucket, added our developers in to it and start working on it. So using eclipse IDE I have created a force.com project, added necessary meta data and committed and pushed the project to bitbucket. And my team members were successfully able imported the same project in to their workspace.

And the issue came when one of the team members wanted add a brand new class in to the their local project. When he tries to do that, it simply requires credentials to add the class to the project. And it requires my credentials, he couldnt do that using his salesforce credentials. How to avoid these types of issues in IDE? Are there any best practice to follow? Because I simply can not give away my credentials to team members to add new codes.

Hi Jitendra, this is an awesome post.
I have few doubts please clarify those:

1. While i create the local repos and try saving the any changes in eclipse, why does it change it on salesforce organization. Cant i stop it saving from salesforce.
2. if its the case for what are we using the remote repository and pushing data using jenkins.

Sure. If you have many developers with different sandboxes , How do you sync Sandboxes ? Most probably manually and its time consuming process, Using Continuous integration tools like Jenkins and BOOMI, you can save lot of times.

Hi Jitendra,
When I push my code changes from Force.com ide to Bitbucket, I got master:rejected error. This was because there was already a commit which my team mate had done in the remote branch and that was not there in my local git folder. Hence I did Git Pull to merge the changes between remote folder and my local git folder. This solved my master:rejected error but I face some other problem because of this. Git Pull had overwritten the code in my eclipse and it had got saved to force.com server which I does not want to happen. I even tried refreshing from server but I was able to see only the changes from bitbucket. Any help is much appreciated!
Thanks

Hi Mythili, Once code is saved in Salesforce we cannot get previous version untill we have code backup. I assume, while pulling code you overwritten code. Normally we gets conflict error and need to merge changes manually but I guess this is not choice right now for you.

Hi, Yes you are absolutely right. Gill Pulls overwrites my code in eclipse and it automatically gets saved in Force.com server. Say suppose my team mate had committed code in remote repo and he has started working on some other feature in force.com. In this case, if I do Git Pull, the code from remote repository will get overwritten and hence my team mates changes in force.com server will be lost right?
Any idea how do you handle this kind of situation?

Proper sequence suggested, each developer should have there own branch and if they want to commit, they should create pull request. There should be Dev lead or deployment lead who will be responsible to merge it with main branch / subbranch.

Also, How could code auto commit to Salesforce ? Do you have any automated job, which reads Git and push code to Salesforce, may be using Continuous integration ?

Great. Thanks Jitendra. Even I was thinking as to fork the repo for each developer and they can commit in that and create pull request to push it to main branch. But I was not sure about the proper workflow/best practice to be followed for salesforce projects using Git. Thanks for confirming the appropriate sequence to be followed.
Committing the code to Git does not overwrite in salesforce. When I do git pull/git fetch + git merge operation before committing my code to Bitbucket, the commits from bitbucket will get reflected in my local eclipse workspace and since ‘Work Online’ mode is On in eclipse, the committed code is directly getting saved to salesforce. I can manage this by setting ‘Work Offline’ mode in eclipse so that the code does not directly gets saved to saved rather overwrite only my local eclispe workspace. In this case, the overwritten files will be shown as warnings and I have to open each and every file and do refresh from server.I dont want to follow this approach because if there are more commits I will have to open each and every file and do refresh from server.

Great post. I have a query on the management of incremental builds using Git.

Contrary to SVN, Git branch is a reference of master and we do not want to deploy the complete master copy(updated for release) to be deployed. Ex: if a object has 10 WFs, master will have Object.workflow file with all metadata. If one WF is updated, we can’t deploy this specific change to next sandbox in Git.

Hey jitendra so this is like awesome. thanks a lot for sharing it . Also one thing i would like to ask is that when we create different branches in Egit , how can we commit code to different branches and then merge it to one branch …
Our requirement is to have different release branches which will merge into one development branch and then to master one.

This blog really helps us and we are looking to ignore few files from the project before committ.
Can you help me understand how to ignore few files from eclipse before committ and this is really important for us.