To start with the Salesforce Source Code Management with Visual Studio Code and GitHub, we need to have certain software installed in our systems.
1. Install Visual Studio Code and Salesforce CLI
Visual Studio Code can be installed in any Macos, Windows and Linux operating system. To install go through the following. Salesforce CLI is a command line interface and helps you to run commands for Salesforce org and work with the metadata and files locally in your system which are retrived from Salesforce org.
Install Salesforce Extensions for Visual Studio Code
Extensions contain everything that needed for developing Salesforce applications using Visual Studio Code.
Get Started With Visual Studio Code and Salesforce
To get started with the project, click on Command Palette(ctrl+shift+p) which you can find it in view option and there are two ways to create a project and develop it.
If you are developing against developer edition, sandboxes, etc orgs, you can use the command "SFDX: Create Project With Manifest" or if you are developing against scratch orgs, you can use "SFDX: Create Project" Command.
Upon clicking any of the commands to create a project, it will ask the destination folder in your system to save the project locally and you can start working on your project.
To work with the source or to deploy and retrieve the source from any org, first, you have to authorize it. In Command palette, click on SFDX: Authorize an org and you will get options like project default - use login URL defined in sfdx-project.json (https://login.salesforce.com/) to work in production org or for sandbox click on https://test.salesforce.com/ or you can enter custom URL based on your requirement. You can change the login URL in the sfdx-project.json file and set sfdcloginurl to the required one. In the sfdx-project.json file, you can also change the destination path.
Once you click on any of the above options, it will ask you to enter Salesforce login username. Upon entering username it will redirect you to the login page and when you login successfully then come back to the visual studio code, it will show you success message like Salesforce Authorized "username" with org id "1234567890".
Now you can create apex classes, triggers, visualforce pages, components, etc in the project. To retrieve the source from authorized org, Right click on the files in the project folder so that you can find a command SFDX: Retrieve Source From Org. click on it to get the source from the authorized org. You will get the components which are mentioned in your package.xml file which you can find in the manifest file.
To deploy the modified source code or a new one, click on SFDX: Deploy Source to Org command which you will find in a similar way as above. To deploy source to different org, log out from the current authorized org and login to the new one to continue.
1. You can deploy or retrieve individual files to or from source org.
2. You can't deploy your code to production directly from visual studio code. It can fail sometimes so deploy using packaging or by converting your source code into metadata format.
Visual Studio Code and GitHub:
To start with Source Code Management with Visual Studio Code and GitHub, you must have a basic idea about Git and GitHub.
To get your Source code to GitHub, first, you have to stage and commit your code changes in the local system. This can be done by hitting on Git: commit and Git: stage commands or you can directly see in the source option which will be on the left sidebar, when you open it you can see all the changes that you have done to your source code and click on check mark where you will get an option to Stage and Commit all the changes. Upon hitting it, commit changes are saved in your local directory.
Now, the changes have to be pushed to GitHub. To do this, make sure you have a repository created in GitHub.
To Push the changes to GitHub repository, Use command GIT: Clone and add URL( you can find it in your GitHub --> Repository--> Clone or download option and copy it) and hit on entering to connect with that repository. You can check that connection in Visual Studio Code at bottom left corner. Initialize the repository by GIT: Initialize Repository Command and you can work on that repository or you can add it the current workspace you are working on.
Now, you can Push the committed changes in your local to the git hub by GIT: Push to the repository which you are currently working on or GIT: Push to command where you can see the option push to the origin with the URL which you have given for your repository. Upon clicking that option, your source code is published to Github and you can see the changes in Github. To push source code to different branches(you must have a branch created in Github), you can change the branches by clicking on the current repository which you can see in the bottom left corner, upon click select your desired branch or you can create a new one and push your code to that branch.
To Pull source code from GitHub to Visual Studio Code, follow the same process until initialization and click on GIT: Pull command or GIT: Pull to pull the source code from GitHub to your local project from the selected repository. To pull code from different branches, hit on GIT: Pull From command where you can get options.
This an Article Crafted by Salesforce Consultant at Micropyramid, the leading Salesforce CRM & Salesforce Development Services Provider in the USA, India.