私はこの同じ問題に苦労しました。私は元々、既存の回答と非常によく似たコードを書きました。秘訣は、すべてのモジュールをファイル システムに配置することですが、この方法にはいくつかの欠点があります。その方法で、VBA プロジェクトからフォームとレポートを取得できますが、元に戻すことはできません。そこで、Rubberduck VBE Add-inの一部としてライブラリを作成しました。私が書いたライブラリは、一見したところプッシュ、プル、コミットするように、VBA プロジェクトとリポジトリの間ですべてのコードをインポートおよびエクスポートします。これは無料のオープン ソース プロジェクトですので、最新バージョンをダウンロードしてインストールしてください。
ライブラリの使用例を次に示します。今後のリリースで、VBA エディターとの実際の統合を追加する予定です。
Dim factory As New Rubberduck.SourceControlClassFactory
Dim repo As Rubberduck.IRepository
Dim git As ISourceControlProvider
Dim xl As New Excel.Application
xl.Visible = true
Dim wb As Excel.Workbook
Set wb = xl.Workbooks.Open("C:\Path\to\workbook.xlsm")
' create class instances to work with
Set repo = factory.CreateRepository(wb.VBProject.Name, "C:\Path\to\local\repository\SourceControlTest", "https://github.com/ckuhn203/SourceControlTest.git")
Set git = factory.CreateGitProvider(wb.VBProject, repo, "userName", "passWord")
' Create new branch to modify.
git.CreateBranch "NewBranchName"
' It is automatically checked out.
Debug.Print "Current Branch: " & git.CurrentBranch
' add a new standard (.bas) code module and a comment to that file
wb.VBProject.VBComponents.Add(vbext_ct_StdModule).CodeModule.AddFromString "' Hello There"
' add any new files to tracking
Dim fileStat As Rubberduck.FileStatusEntry
For Each fileStat In git.Status
' fileStat.FileStatus is a bitwise enumeration, so we use bitwise AND to test for equality here
If fileStat.FileStatus And Rubberduck.FileStatus.Added Then
git.AddFile fileStat.FilePath
End If
Next
git.Commit "commit all modified files"
' Revert the last commit, throwing away the changes we just made.
git.Revert