[SalesForce] convert existing git repo to sfdx project

I have an existing git repo with Salesforce metadata in it that I've been using for the last couple years. It's not a sfdx project, it's just what is returned when using the metadata api. I'd like to convert it to a sfdx project but the only thing is, that I'm aware of, is when creating a new sfdx project, it has to create a brand new folder. I don't want this because I will loose my commit history.

What are my options? I do see from this question that they just created a brand new repo, but that is doesn't seem ideal.

Best Answer

You need to create some files to make a directory into a SFDX project, particularly sfdx-project.json. The easy way to do this is to go to the parent folder of your Git root, and type in the command sfdx force:project:create -n directoryName. This creates a number of files from a template that will be used to configure the project. After this, you should then convert your source folder using the steps below.

That looks like this:

computer-name MINGW64 ~/workspace/my-git-project (master)
$ cd ..
computer-name MINGW64 ~/workspace
$ sfdx force:project:create -n my-git-project
target dir = C:\Users\my-name\workspace
   create my-git-project\config\project-scratch-def.json
   create my-git-project\README.md
   create my-git-project\sfdx-project.json
   create my-git-project\.vscode\extensions.json
   create my-git-project\.vscode\launch.json
   create my-git-project\.vscode\settings.json
   create my-git-project\force-app\main\default\lwc\.eslintrc.json
   create my-git-project\force-app\main\default\aura\.eslintrc.json
   create my-git-project\scripts\soql\account.soql
   create my-git-project\scripts\apex\hello.apex
   create my-git-project\.eslintignore
   create my-git-project\.forceignore
   create my-git-project\.gitignore
   create my-git-project\.prettierignore
   create my-git-project\.prettierrc
   create my-git-project\package.json

You may also see "conflict" and "force"; this means files were overwritten. You can git checkout to revert those changes afterwards.


Git is good about detecting the file name changes. However, it has a built-in limit on a number of files it can handle at a time.

Hence when you convert your project to sfdx format chances of losing history are high since number of files that get renamed in this process may be higher than the limit that git has.

You can increase this limit by a simple config like below

git config merge.renameLimit 999999

Here is what you will need to preserve the history

git config merge.renameLimit 999999
sfdx force:mdapi:convert -r src -d tmp // This converts what is in src to a DX source format and moves to tmp folder
rm -rf src // This removes src folder
mv tmp src // This moves tmp folder items to the src folder
git add -A
git commit -m "Converted from metadata to source format"
git config --unset merge.renameLimit # Return the git config option to the default
Related Topic