Seed a branch with files from a different GIT branch. - Azure Dev-Ops

There is barely any documentation provided on this topic where you can automate adding files from 1 branch into another branch in GIT using C# or any of the programming languages. Then, this process becomes even harder when you have to seed files from a different repository.

In this snippet of the code, I am reading files of a branch in a different repository and then committing all of them to a different branch in a different repository. Using this process, I can maintain the same structure that exists in source in the destination bra

I had to try a few things and then somehow figured it out based on hit and trial.

GitHttpClient _gitClient, 
GitRepository _gitDestimationRepo, GitRepository _gitSourceRepo, 
GitRef _gitSourceBranch, GitRef _gitDestinationBranch, string _Comment

 //Create the branch where we will commit.
 //Note: There is no documentation available that shows how to commit to an existing branch. These next 5 lines just do that.
            var _destinationBranch = new GitRefUpdate
            {
                Name = _gitDestinationBranch.Name,
                OldObjectId = _gitDestinationBranch.ObjectId
            };

            List<GitItem> _Processed = new List<GitItem>();

//Gets all files from a repository.
  List<GitItem> _items = _gitClient.GetItemsAsync(_gitSourceRepo.Id, scopePath: null, recursionLevel: VersionControlRecursionType.Full).Result;
            foreach (var _singleItem in _items)
            {
                GitItem singleItemWithContent;
                if (_singleItem.IsFolder == true)
                {
                     singleItemWithContent = _gitClient.GetItemAsync(_gitSourceRepo.Id, _singleItem.Path, includeContent: false).Result;
                }
                else
                {
                   //Use this to get the content else content is not retrieved.
                     singleItemWithContent = _gitClient.GetItemAsync(_gitSourceRepo.Id, _singleItem.Path, includeContent: true).Result;
                }
                _Processed.Add(singleItemWithContent);
            }
            
            //GitItem singleItemWithContent1 = _gitClient.GetItemAsync(_gitSourceRepo.Id, "/master/XMLFile.xml", includeContent: true).Result;

            List<GitChange> _gitChanges = new List<GitChange>();
            foreach (var item in _Processed)
            {
                if (!item.IsFolder)
                {
                    ItemContent _newContent;
                    GitChange gitChange = new GitChange();
                    gitChange.ChangeType = VersionControlChangeType.Add;
                    gitChange.Item = item;
                    _newContent = new ItemContent
                    {
                        Content = item.Content,
                        ContentType = ItemContentType.RawText,
                    };
                    gitChange.NewContent = _newContent;
                    _gitChanges.Add(gitChange);
                }
            }
            GitCommitRef newCommit = new GitCommitRef
            {
                Comment = "Initial Seeding",
                Changes = _gitChanges
            };
            var _Push = new GitPush()
            {
                RefUpdates = new GitRefUpdate[] { _destinationBranch },
                Commits = new GitCommitRef[] { newCommit },
            };
            GitPush push = _gitClient.CreatePushAsync(_Push, _gitDestimationRepo.Id).Result;

Comments