Ora

How Do I Push Salesforce Code to GitHub?

Published in Salesforce Git 7 mins read

Pushing Salesforce code to GitHub involves preparing your local Salesforce project, setting up a new repository on GitHub, initializing Git, and then syncing your local changes with the remote repository. This process is crucial for version control, collaboration, and implementing robust CI/CD pipelines for your Salesforce development.

Why Version Control for Salesforce?

Version control systems like Git, hosted on platforms like GitHub, are essential for managing changes to your Salesforce metadata and code. They provide a history of all modifications, enable team collaboration, facilitate rollbacks, and support automated deployment processes.


Prerequisites for Pushing Salesforce Code

Before you begin, ensure you have the following tools and accounts set up:

  • GitHub Account: A free or paid account on GitHub.com.
  • Git Installed: The Git command-line interface must be installed on your local machine. You can download it from Git-SCM.com.
  • Salesforce DX CLI: The Salesforce Command Line Interface (CLI) is necessary for interacting with your Salesforce org and managing metadata. Install it from Salesforce CLI Documentation.
  • VS Code (Recommended): Visual Studio Code with the Salesforce Extension Pack provides an excellent integrated development environment for Salesforce.

Step-by-Step Guide to Pushing Salesforce Code to GitHub

This guide breaks down the process into three main parts: setting up your GitHub repository, preparing your Salesforce project locally, and finally, pushing your code to GitHub.

Part 1: Setting Up Your GitHub Repository

The first step is to create a new, empty repository on GitHub where your Salesforce code will reside.

  1. Create a New Repository:
    • Log in to your GitHub account.
    • In the header, click the + icon (located near your profile picture) and select New repository.
  2. Configure Repository Details:
    • Repository name: Enter a descriptive name for your project, for example, my-salesforce-project or best-repo-ever.
    • Description (Optional): Briefly explain what your repository contains.
    • Public or Private: Choose whether the project is Public (visible to everyone) or Private (visible only to you and collaborators you invite).
    • Initialize this repository with: Select Add a README file. This provides an initial file in your repository.
  3. Create Repository: Click the Create repository button.

Once created, GitHub will display a page with instructions on how to set up your local repository and push your existing code. Keep this page open, as you'll need the repository's URL.

Part 2: Preparing Your Salesforce Project Locally

Next, you need to set up your Salesforce project on your local machine using Salesforce DX.

  1. Authorize Your Salesforce Org:
    Open your terminal or command prompt (or VS Code's integrated terminal) and authorize your Salesforce org. This links your local environment to your Salesforce instance.

    sf org login web -a MyDevOrg # -a is for alias, choose a meaningful name

    This command opens a browser for you to log into your Salesforce org.

  2. Create a New Salesforce DX Project (if you don't have one):
    If you're starting fresh, create a new SFDX project.

    sf project generate -n MySalesforceProject -d MyProjects
    cd MySalesforceProject

    If you already have a local project, navigate to its root directory.

  3. Retrieve Metadata from Your Salesforce Org:
    Pull the metadata (Apex classes, Lightning Web Components, Custom Objects, etc.) from your Salesforce org into your local project. You can retrieve specific components or all unpackaged metadata.

    To retrieve all unpackaged metadata specified in package.xml (located in manifest/ folder):

    sf project retrieve start -m "ApexClass,ApexTrigger,LightningComponentBundle" # Example: specify metadata types
    # Or to retrieve all metadata defined in package.xml
    sf project retrieve start -x manifest/package.xml

    Ensure your package.xml is configured to retrieve the desired components. You can generate a basic package.xml using tools within VS Code or manually.

Part 3: Initializing Git and Pushing to GitHub

Now, you'll use Git to track your local project and push it to the GitHub repository you created.

  1. Navigate to Your Project Directory:
    Ensure your terminal is open in the root directory of your Salesforce DX project (MySalesforceProject in our example).

  2. Initialize Git:
    This command creates a new .git subdirectory, making your project a Git repository.

    git init
  3. Create a .gitignore File:
    It's crucial to prevent sensitive files, temporary files, and local configurations from being pushed to GitHub. Create a file named .gitignore in your project's root directory and add common Salesforce-specific exclusions.

    Example .gitignore content:

    # Salesforce DX Project ignore
    .sfdx/
    .sf/
    sfdx-project.json
    .vscode/
    force-app/main/default/lwc/**/*.js.map
    force-app/main/default/aura/**/*.js.map
    # Local credentials
    *.key
    *.pem
    # Log files
    *.log
    # OS generated files
    .DS_Store
    Thumbs.db
    # VS Code workspace settings
    .vscode/settings.json
    .vscode/*.code-workspace

    Note: Adjust this file based on your project's specific needs. sfdx-project.json is usually tracked, but .sfdx/ and .sf/ are not.

  4. Add Files to Staging:
    Stage all the files you want to track for the initial commit. The . means all files in the current directory (excluding those in .gitignore).

    git add .
  5. Commit Your Changes:
    Commit the staged files with a descriptive message. This creates a snapshot of your project's current state in your local Git history.

    git commit -m "Initial commit of Salesforce project metadata"
  6. Add Remote Origin:
    Link your local Git repository to the GitHub repository you created in Part 1. You'll need the HTTPS or SSH URL of your GitHub repository. You can find this on your repository's GitHub page under the "Code" button.

    git remote add origin https://github.com/your-username/best-repo-ever.git
    # Replace with your actual GitHub repository URL
  7. Push to GitHub:
    Finally, push your local commits to the main (or master) branch on GitHub. The -u flag sets the upstream branch, so subsequent git push commands don't require specifying the branch.

    git branch -M main # Renames your default branch to 'main' if it's 'master'
    git push -u origin main

    You might be prompted to enter your GitHub username and Personal Access Token (PAT). It's recommended to use a PAT instead of your password for security reasons. Learn how to create a PAT on GitHub Docs.


Ongoing Development: Syncing Your Code

Once your project is on GitHub, you'll follow a regular cycle for ongoing development:

  1. Pull Latest Changes: Always pull changes from GitHub before starting new work to ensure your local branch is up-to-date.

    git pull origin main
  2. Make Changes: Develop new features or fix bugs in your Salesforce org and then retrieve them locally using sf project retrieve.

    sf project retrieve start -m "ApexClass:MyNewClass" # Example for new class
  3. Stage and Commit: Stage your modified files and commit them with a meaningful message.

    git add .
    git commit -m "Added MyNewClass for Account management"
  4. Push Changes: Push your new commits to GitHub.

    git push origin main

Best Practices for Salesforce & Git

To maintain a healthy and efficient development workflow:

  • Branching Strategy: Use a branching strategy like Gitflow or GitHub Flow. Always work on feature branches, not directly on main.
  • Meaningful Commits: Write clear, concise commit messages that explain what changed and why.
  • Regular Commits: Commit small, logical changes frequently.
  • .gitignore Management: Keep your .gitignore up-to-date to avoid committing unnecessary or sensitive files.
  • Personal Access Tokens (PATs): Use PATs for authenticating with GitHub instead of your password.
  • CI/CD Integration: Consider integrating Continuous Integration/Continuous Deployment (CI/CD) tools (e.g., Salesforce DevOps Center, Azure DevOps, Jenkins, GitLab CI) to automate testing and deployments from your GitHub repository.

Git Command Summary Table

Here's a quick reference for common Git commands used in this process:

Command Description
git init Initializes a new Git repository in the current directory.
git add . Stages all changes in the current directory for the next commit.
git commit -m "Message" Records staged changes with a descriptive message.
git remote add origin [URL] Connects your local repository to a remote repository on GitHub.
git branch -M main Renames the current branch to main.
git push -u origin main Pushes local main branch commits to the remote origin and sets upstream.
git pull origin main Fetches and merges changes from the remote main branch into your local branch.
git status Shows the status of changes as untracked, modified, or staged.
git log Displays a history of commits.

By following these steps, you can effectively manage your Salesforce codebase on GitHub, enabling better team collaboration and a more robust development lifecycle.