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.

Posted

in

by


Related Posts

Comments

50 responses to “Salesforce + Git + Eclipse + EGIT = Better and Distributed Source Control”

  1. […] In Previous article, i have explained step by step approach to configure Git for Salesforce using Eclipse and Egit plugin. However, i have explained the “https” method to connect Git directly. There are several repositories which does not support the “https” protocol and prefer “SSH” (Secure Shell) protocol. […]

  2. Salesforce Master Avatar
    Salesforce Master

    Thanks man

  3. Pawan Kumar Avatar
    Pawan Kumar

    You are awsome !

  4. kavatiramu Avatar

    good material

  5. Phani Avatar
    Phani

    Thank you somuch

  6. Ani Avatar
    Ani

    Very good steps and good job, thanks.

  7. ?????? ???? ??????? Avatar

    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

    1. JitendraZaa Avatar
      JitendraZaa

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

  8. martin Avatar
    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 ?

    1. JitendraZaa Avatar
      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

      1. snune Avatar
        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

        1. Dev Avatar
          Dev

          Hi,

          I am facing same problem. Did you solved it?

          1. rad Avatar
            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.

      2. Bob Avatar
        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.

        1. sugandha Avatar
          sugandha

          Hey bob,
          Did you find the solution for this ? i had also faced something like this earlier.

    2. snune Avatar
      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

  9. doug sawyer Avatar
    doug sawyer

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

  10. Robert Guo Avatar
    Robert Guo

    Awsome!!

  11. ganesh Avatar
    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?

    1. Jitendra Zaa Avatar

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

  12. Lahiru Avatar
    Lahiru

    Superb Article. thanks for sharing the knowledge

    1. Jitendra Zaa Avatar
      1. Lahiru Avatar

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

        1. Jitendra Zaa Avatar

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

  13. Pinal Patel Avatar
    Pinal Patel

    Great blog on Git with SFDC Jitendra!

    1. Jitendra Zaa Avatar

      Thanks Sir, Glad you liked it 🙂

  14. Lahiru Avatar

    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

    1. Jitendra Zaa Avatar

      Hi Lahiru,

      You need to check settings in Bitbucket. Whenever you add a team member, there are set permissions we need to provide. You can check this URL for more info – https://confluence.atlassian.com/display/BITBUCKET/Add+a+user+to+a+team

  15. SFdev1288 Avatar
    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.

    1. Jitendra Zaa Avatar

      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.

      1. SFdev123 Avatar
        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.

        1. Jitendra Zaa Avatar

          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.

  16. swetha Avatar
    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.

    1. swetha Avatar
      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.

  17. Mythili P Avatar
    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

    1. Jitendra Zaa Avatar

      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.

      1. Mythili P Avatar
        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?

        1. Jitendra Zaa Avatar

          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 ?

          1. Mythili P Avatar
            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!

  18. […] i možnost verzování zdrojového kódu. Přes web máte smůlu, tady stačí nainstalovat EGit, spojit s BitBucketem, GitHubem či čímkoliv dalším a je to. Ta krása když se můžete vrátit zpět aniž byste […]

  19. Priyanka Singh Avatar
    Priyanka Singh

    thank u sir ..such a nice article

  20. Priyanka Singh Avatar
    Priyanka Singh

    Nice post

  21. Manjunath TJ Avatar
    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

    1. sugandha Avatar
      sugandha

      Hey manju,
      How did you overcome this hussle of incremental builds?? what strategy did u follow

  22. Sarma Avatar
    Sarma

    Great post Jitendra! Any way we can skip the extra folder in the git repository? Would be better if src and Reference Packages folders are in the root of the repository.

    1. Jitendra Zaa Avatar

      Yes, to skip extra folder, mark parent directory of src as git. “.git” folder created only in parent directory unlike SVN

  23. Sandeep Kumar Avatar
    Sandeep Kumar

    Hi Jithendra,

    I want to push to Heroku (Remote Repository) instead of Github. I’m having issues while Code is being deployed to heroku. Do you have any documentations on it.

  24. sugandha Avatar
    sugandha

    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.

  25. Vanasvi Avatar
    Vanasvi

    Hi Jitendra,

    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.

    Thanks,
    Vanasvi

Leave a Reply

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

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading