As a developer, we understand the importance of using version control to track our code changes.  Setting this up with Salesforce development may not be as intuitive as with other development tools so this post will walk you through how to setup Git within your Eclipse development environment.

First, go to the Git website, download and install the latest version.  You may already have Git installed but this will insure you have the latest updates.

version-control-salesforce-code-git

Now let’s get Eclipse setup.  Open Eclipse and we’ll add some additional views that will allow us to monitor and manager the repositories.

  • To add the Git views, do the following steps:
    • From the Window menu, select Show Views then Others
    • Expand Git and select Git Repository and Get Staging
    • Press Ok
  • Add the Team History view:
    • From the Window menu, select Show Views then Others
    • Expand the Team and select History
    • Press Ok

 

Next lets setup a Git repository site.  I prefer GitLab simply because it allows me to setup private repositories at no cost.  GitHub is another option but their free repositories are publicly visible.   Setup an account with either solution.

In GitLab, create a New Project.  In GitHub, it’s New Repository.  Name the repository the same as your Eclipse project.  You don’t have to use the same name but I find it simpler when working with multiple projects.

version-control-salesforce-code-gitlab1

I only setup repositories on my sandbox projects since code changes is all I typically track. However, if you’re wanting to track all metadata changes then create a Project that holds all metadata and you can setup a repository on it as well.

Copy your repository’s HTTPS UTI.  You’ll need this in our next step.

version-control-salesforce-code-gitlab2

Click back to Eclipse.

  • From the Package Explorer, right click on your project and select Team > Share Project
    • Select Git then Next
  • The Configure Git Repository screen will appear
    • Press the Create button
      • Accept the default Parent Directory
      • Name the repository the same as you Git repository for simplicity
      • Press Finish
    • Press Finish.  When its done, you’ll see a new repository in the Git Repository view
  • From the Git Repositories view, expand the repository
    • Right-click on Remote
    • Select Create Remote
    • Leave the default Remote Name (origin) in the New Remote screen and press OK
    • Press the Change button next to the URI and paste in the URI for the project
    • Add your GitLab login name/password
    • Press the Finish button
    • Press Save
  • From the Package Explorer, right click on your project and select Team > Commit
  • The Commit Changes to Git Repository window will appear:
    • Git requires a Commit Message whenever committing changes.  Best practices would suggest the messages should list the changes made since the last commit.
      • For your first message, use something like “Initial Commit”
    • In the Files section of the window, select the files you want to commit.  Use the Select All button to mark all the checkboxes.
    • Press Commit and Push
      • Commit will update your local Git repository while Commit and Push updates your GitLab/GitHub repository.
  • The Push Results screen will appear next:
    • Press OK
  • The Push Ref Specifications screen will appear:
    • Under Source Ref, select Master [branch]
    • Press the Add Spec button
    • Press Finish

 

Your project is now setup with version control.

When you need to make code changes, simply open the code and make your changes as you would normally.  You’ll notice the icons next to the project files will change to indicate files that no longer match the repository.  The differences also show in the Git Staging view.

Once all the changes are complete and you’re ready to commit them to the repository:

  • From the Package Explorer, right click on the project and select Team > Commit
  • The Commit Changes to Git Repository window will appear:
    • Enter a Commit Message to indicate the changes made
    • The file(s) that were changed will be listed.  Press Commit and Push.
  • The Push Results screen will appear next:
    • Press OK

 

If you return to your GitLab/GitHub website, you’ll see the changes in your repository.

For additional help, here is a good YouTube video that walks you through much of the Eclipse steps.

Finally, if you’re working with a team of developers, each team member will leverage the same Git Repository.  To keep the local repository in sync with other teammates committed changes:

  • From the Package Explorer, right click on the project and select Team > Synchronize Workspace.  This allows you to see the differences between your local repository and the shared repository and merge the remote changes into the local repository.

 

Hope you find this step by step guide helpful in setting up version control for your salesforce.com projects.  If you have questions or additional suggestions, please ask.

Terry Miller
Terry has spent over 20 years focused on business leadership and information technology. As an independent consultant, he enjoys working with a variety of customers to help them solve business problems using the Salesforce® platform. His ability to quickly identify bottlenecks and provide understandable solutions has gained him the trust of his customers. If you're looking for expert guidance on your next Salesforce® project, click here to contact Terry today.

3 Comments

  1. That is awesome that companies that use Sales Force are able to compute their company on a cloud. I really like that Sales Forces seems to be a very organized and effective way to record your daily sales. Thank you for sharing the benefits of turning towards an online method of computer recording

    1. Why not use a decent Git GUI like smartGit? Often one is not just wokring on one feature, but also fixes one or another minor bug. With smartGit you can stage individual pieces of code to be able to create clear-cut commits. IIRC, this is not possible with plain command line Git and saved my ass a lot of times. Give it a try and you never will use command line again for such common tasks.

  2. Wow, thanks for the very informative article Terry! I have been able to git a repository created of all of our Salesforce metadata in Team Foundation Server on visual studio.com and everything works form the Git command line but I am having some problems in Eclipse/Force.com IDE. When I try to connect to the repo through Eclipse I get a ‘Not authorized’ error. I’ve tried several combinations of usernames passwords and tokens and nothing works. I have a few more ideas I am going to try but was just wondering if you had any thoughts or have seen this issue before.

Leave a Reply

Your email address will not be published.

*