Use GIT in Salesforce Implementation Project

GIT

GIT Management/Dev Ops

This lists the steps to use Git for any Salesforce project considering below facts:

  • – GitLab is used
  • – VS code editor is used
To check changes if made locally

git status  
  • #If no repo
    • git init
  • #If repo exist
    • . Add only the files you want to push
      #add core files
      git add sfdx-project.json
      git add config/
      git add manifest/
    • # If you want to move all components

      git add .

      # Only selected file to be pushed

      git add force-app/main/default/classes/
      git add force-app/main/default/flows/
      git add force-app/main/default/objects/
  • Local commit

    git commit -m “Initial commit: Salesforce components”
  • Connect to GitLab and push

    git remote add origin YOUR_GITLAB_URL_HERE
    git branch -M main
    git push -u origin main
  • After first time, if you want to push further any subsequent change

    git add .
    git commit -m “describe what you changed”
    git push

SOME IMPORTANT NOTES

  • IF you have added wrong gitlab url then change as below:

    git remote set-url origin https://gitlab.com/weeve2/weeve.git
  • If brnach is protected then
    1. Go to your project >> Left sidebar → Settings →Click Repository → Protected branches
  • If you want to some file/folder not to be pushed then put it in gitignore

check if you are already logged in Gitlab in VS Code

git ls-remote https://gitlab.com/xx/yy.git

Otherwise login in gitlab

git ls-remote https://gitlab.com/project/project.git

Note: enter username and then personal access token (can be generated from profile section)

In main repo, I want to delete all files as pushed some unwanted things

Run these commands to remove everything from remote (your local files stay untouched):

git rm -r –cached .

git commit -m “Clear remote”

git push origin main

This will leave the repo empty on GitLab. Then selectively add only what you want:

git add sfdx-project.json

git add config/

git add force-app/

git add manifest/

git commit -m “Add Salesforce components”

git push origin main

If need to delete a specific file/folder

git rm –cached .gitignore
git commit -m “Remove .gitignore from remote”
git push origin main

Manage Branches

Create and switch to a new branch

git checkout -b feature/package-xml

Add the package.xml

git add manifest/package.xml

Commit

git commit -m “Add package.xml”

Push to remote as a new branch

git push origin feature/package-xml

SF in VS Code

Deployment using command

  • sf org list : to list all org in vs code
  • command to deploy to production
sf project deploy start --target-org tenet-production \--test-level RunSpecifiedTests \--tests TE_CaseSummaryControllerTest \--manifest manifest/package.xml

To run multiple test classes whiile deployment:

sf project deploy start --target-org tenet-production \
--test-level RunSpecifiedTests \
--tests TE_CaseSummaryControllerTest \
--tests TE_AnotherControllerTest \
--tests TE_ThirdClassTest \
--manifest manifest/package.xml

To run default test classes:

sf project deploy start --target-org tenet-production --manifest manifest/package.xml

Login using Command

sf org login web --alias TenetProdOrg --instance-url https://login.salesforce.com

Link already logged in org

sf config set target-org  <orgAlias>  
Scratch Org

Login in scratch org

sf org open --target-org KnowledgeAppDev

Create Scratch Org

  • Setup sfdx-project.json and config >> project-scratch-def.json
  • Run below command:
sf org create scratch \  --definition-file config/project-scratch-def.json \
  --alidddddel 30 \
  --set-default