Press enter to see results or esc to cancel.

How to setup Source Control Management (SCM) and work with SFDX?

In the previous blog-post we have seen what are scratch orgs and how to work with them. I know we have just have touched the tip of the iceberg, there is a lot to learn in Scratch orgs and whole sfdx process. Any how good thing is we have started it. 🙂
Now in this blog-post we will see how to work with GitHub which is our Source Control Management. This is completely new technology for me, I have covered pretty basic stuff on how to work with SCM.
Why do we need Source Control Management (SCM)?

Well there are multiple reason us to use Source Control Management in our Projects.

  • It acts as a source of truth
  • You can go back to past previous snapshot of the code
  • To resolve the conflicts when there are multiple people working on same piece of code

These are some of them i can think of. There will be huge list of advantages using SCM in your projects.

We are going to use GitHub for this blog. I am assuming that you might have already have GitHub account.If not please create one by visiting GitHub.com
How to create a project in GitHub?
After you successfully login to your GitHub account.
  • Go to the repositories tab
  • Click New button.
  • Give it a name (SourceControlManagement)
  • Leave the defaults as it is then click Create Repository.

SourceControlManagement

Once the Repository is created you will see something like this below.

SourceControlManagement-github

All right lets leave it like this for sometime. We need to finish some setup in our machine.
In order to login to GitHub on desktop we need to authorize it via ssh key. Prior to that Please install git on your machine if not that already. Install OpenSSL on your windows machine ( We are going to automate some regular tasks in coming blog posts).  You can follow below video to install OpenSSL on your windows machine
Go to settings page in your GitHub, then select SSH and GPG Keys. Here Click on New SSH Key. You can find an help article on how to generate SSH keys.
I have kept the sequence of command you need to execute below.
  • Now open your command prompt on your machine.
  • Execute the command ssh-keygen -t rsa -b 4096 -C “saicharan.reddy70@gmail.com” replace the email id with yours. click on enter for defaults
key
Observe that the verification is stored in .ssh/id_rsa.pub
  • Now execute the command to copy the contents : clip < .ssh/id_rsa.pub
  • Go to ssh and gpg keys in GitHub give it a name in Title section for your key then paste the content that you have copied from command prompt in Key section then Click Add SSH Key
You have successfully created SSH key. Now its time to execute one more command. Come to command prompt and execute the command ssh -T git@github.com
You will see a successful message on your command prompt screen. GitHub uses the files we have created earlier in our machine under .ssh folder to authenticate. We did not enter any credentials.  In case if you didn’t see the successful message there might be something wrong in the process you have followed.

Hi saicharanreddyk! You’ve successfully authenticated, but GitHub does not provide shell access.

 

Now that we have successfully completed all the required basic steps its time to start the real work. Let’s get our hands dirty.
Open your VSCode and create a project using command Pallet.
Once the Project is created its time to initialize the git command to inform OS that this particular project is our area of interest and we need this to move this to GitHub Repository.
Execute the command git init

git init ( Inform the OS this folder is really the github project)

  • .git folder
  •  Green means there is a change but we haven’t tracked, Fonts changed
  •  Qualified for repository
Once you have executed the command you can see the above differences.

git-init-before

Once the command is executed you can see the .git folder created in your project directory.
Initialized empty Git repository in C:/Users/sreddyk/Desktop/CLI/sourceControlManagement/.git/

git-init-after

Now its time to execute another command git add . ( Observe the dot )
  • Staged the changes
  • Not in my repository and currently it is in local.
  • Observe the Untracked symbols gone and now you can see Index Added symbol A.

git-add

Now its time to commit the changes using the commit command. Execute the command git commit -m “This is my initial empty commit”
 

git commit

  • Always commit With message
  • Only local changes and they are committed.
After successful commit you will see the output like this.
16 files changed, 259 insertions(+)
create mode 100644 .eslintignore
create mode 100644 .forceignore
create mode 100644 .gitignore
create mode 100644 .prettierignore
create mode 100644 .prettierrc
create mode 100644 .vscode/extensions.json
create mode 100644 .vscode/launch.json
create mode 100644 .vscode/settings.json
create mode 100644 README.md
create mode 100644 config/project-scratch-def.json
create mode 100644 force-app/main/default/lwc/.eslintrc.json
create mode 100644 manifest/package.xml
create mode 100644 package.json
create mode 100644 scripts/apex/hello.apex
create mode 100644 scripts/soql/account.soql
create mode 100644 sfdx-project.json
All the changes we have done committed locally at this point of time.

git-commit

Let’s change some files present in the project and see how it works. In the package.xml file I have removed everything and kept only ApexClass type and saved the file.
Once the changes made run the command git status.

git-status

To make changes appear we need to commit changes, before commit changes we need to stage them by adding git add . command.

git-changes-commit

Please Observe carefully the square boxes. Like this you can make changes to the files and stage them and commit them. What ever we have done, totally on locally, Now its time to connect the folder to github repository we have created earlier.
  • Go to GitHub repository.
  • Execute the command “git remote add origin git@github.com:saicharanreddyk/SourceControlManagement.git” inside the project folder to link the local project folder and GitHub repository. Here we just linked but the changes won’t appear on GitHub remote repository.
  • Then execute the command “git push -u origin master” to see the changes.
git push origin master
we can use authentication/non authentication way of cloning the git repository.

connect-github-repo-local-project

github-remote-repo
Please observe that we have committed twice that’s why you are seeing 2 commits. Go ahead and open the commits to see the messages and changes you have made to the package.xml file.

package.xml-commit

Now make some changes in the local folder to the package.xml to keep the original content. Stage it, commit it then push it
git add .
git commit -m “Reverting the changes”
git push -u origin master
see the changes on the GitHub repository. Commits increased to three and package.xml file changed to default.
These are some pretty basic things you can perform with the GitHub, I am sure if you are already known GitHub for a while these can be pretty straight forward. As I am learning this new technology, I will try to cover the advanced topics in the future blogposts.
Thanks for reading. Please let me know in case of any suggestions / queries.