Salesforce + Git + Eclipse + EGIT = Better and Distributed Source Control

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
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
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.

Step 1:
So, let’s start with using Git with Salesforce using Eclipse plug-in EGit.

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
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
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
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
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
Commit Changes to Git Repository – Eclipse – Salesforce

After commit the folder structure will appear with following icons:

Committed folders in Salesforce Eclipse using Git
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

  1. GitHub
  2. BitBucket
  3. Unfuddle

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

https://YOURUSERNAME@bitbucket.org/YOURUSERNAME/YOUR_REPOSITORY_Name.git

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

Git Repository Perspective in Eclipse
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
Create Remote Git Repository Eclipse for Salesforce

One new popup will appear like below

Git Create New Remote in Eclipse - Salesforce
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
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
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.

Question : Why I am getting an error something like “Git Push Failed , Non-fast forward updates were rejected“.

Solution : Mostly in my case, I am getting this error if you are trying to push something to remote server first time. Simplest way to resolve this error is to Fetch (Pull) first and then Push.

In the same way you can configure “fetch” to get the code from remote repository.

I hope it will be help full for learners. I am waiting for your feedback. In next article I will show how to connect using SSH instead of https protocol.

Related posts

  • Pingback: Authenticate Git using SSH with Eclipse and Egit | Shiva Blog()

  • Salesforce Master

    Thanks man

  • Pawan Kumar

    You are awsome !

  • good material

  • Phani

    Thank you somuch

  • Ani

    Very good steps and good job, thanks.

  • 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

    • JitendraZaa

      Thanks Harshit for your words. I will also add link to your article for readers.

  • martin

    Nice Job!

    However, I cant figure out how is it possible to use GIT in a force.com project where more than one developer is pushing code.

    Sample:

    Dev 1 ) refresh from server > change code > save to salesforce > commit > push
    Dev 2 ) refresh from server > change code > save to salesforce > commit > push error “out of date”, if pull to update then conflict

    Could you find the way to work with more than one dev ?

    • JitendraZaa

      Have u tried this ?

      Dev 1 ) Pull > refresh from server > change code > save to salesforce > commit > push
      Dev 2 ) Pull > refresh from server > change code > save to salesforce > commit > push

      • snune

        Hi Jitendra,

        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?

        Thank you

        • Dev

          Hi,

          I am facing same problem. Did you solved it?

          • rad

            Hi Jitendra,

            I am also facing the error while doing fetch. I mean, I don’t see any latest code in my local repository. Could you please help us regarding this issue.

      • Bob

        Hi Jitendra,

        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.

        Could you please help us regarding this issue? Thanks in advance.

    • snune

      Hi Martin, Did you get this resolved? How does this ‘Fetch’ functionality works if there are more than 2 developer working on the same file at same time?

      Thanks

  • doug sawyer

    Thank you for this article as it was a great help for my initial setup!

  • Robert Guo

    Awsome!!

  • ganesh

    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?

    • Hi Ganesh,
      You can use Jenkins, and Jenkins internally uses ANT command only

  • Lahiru

    Superb Article. thanks for sharing the knowledge

    • Thanks

      • Can we directly push our changes to a branch in GIT hub rather than to a master? Can we configure Push to a branch?

        • Thats right, every branch has unique URL, you just need to use that URL.

  • Pinal Patel

    Great blog on Git with SFDC Jitendra!

  • Hi Jitendra,

    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.

    Appreciate your help

  • Pingback: Version Controling for Force.com projects with Git, Egit and BitBucket | Lahiru's Tech Blog()

  • SFdev1288

    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.

    I expect a quick reply sir.

    • There is setting in eclipse to work in offline mode.
      Jenkins will ne useful if you have miltiple sandboxes and developers working actively to autosync.

      • SFdev123

        Hi Thanks for the reply,
        Yes we have many developers working, i am still unable to create a logical architecture.
        I will also go through your post of 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.

  • swetha

    Hi Jitendra,

    Thats a very good article.
    We have a requirement to refresh the sandbox B with sandbox A and merge apex classes instead of overwriting them. How can we do this. Please let me know. Thanks in advance.

    • swetha

      Giving more clarity: Its not complete refresh, but add the latest changes from sandbox A to sandbox B by merging the apex classes & triggers.

  • Mythili P

    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.

      • Mythili P

        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 ?

          • Mythili P

            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.

            Thanks a lot for your help!

  • Pingback: Salesforce a programování v Eclipse - Blog Martina Humpolce()

  • Priyanka Singh

    thank u sir ..such a nice article

  • Priyanka Singh

    Nice post

  • Manjunath TJ

    Hi Jitendra,

    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.

    Any strategy for such incremental builds using Git is very helpful.

    Thanks. Manju