title: How to replace the contents of a Git repository with Cake
body: <p>I'm working on a Cake script that should build a Wyam site and deploy it to Github Sites. This means the entire content of the master branch should be replaced with the new Wyam build. </p>
  
  <p>So far I have created the Cake script below, which works as it should. But I'm wondering whether there is an easier way of replacing all contents of the master branch with the new Wyam build then doing these tasks manually.</p>
  
  <p>The tasks that I would like to simplify are <code>EmptyMasterBranch</code> and <code>CopyToMasterBranch</code>.</p>
  
  <pre><code>Task("Build")
      .Does(() =&gt;
      {
          Wyam();        
      });
  
  Task("Preview")
      .Does(() =&gt;
      {
          Wyam(new WyamSettings
          {
              Preview = true,
              Watch = true
          });        
      });
  
  Task("CloneMasterBranch")
      .Does(() =&gt; {
          Information("Cloning master branch into temp directory");
  
          GitClone(
              repositoryUrl,
              new DirectoryPath(tempDir),
              githubUserName,
              githubAccessToken,
              new GitCloneSettings {
                  BranchName = "master"
              }
          );
      });
  
  Task("EmptyMasterBranch")
      .IsDependentOn("CloneMasterBranch")
      .Does(() =&gt; {
          Information("Emptying master branch");
  
          string[] filePaths = System.IO.Directory.GetFiles(tempDir);
  
          foreach (string filePath in filePaths)
          {
              var fileName = new FileInfo(filePath).Name;
              fileName = fileName.ToLower();
  
              if(System.IO.File.Exists(filePath))
              {
                  DeleteFile(filePath);
              }
          }
  
          string[] directoryPaths = System.IO.Directory.GetDirectories(tempDir);
  
          foreach (string directoryPath in directoryPaths)
          {
              var directoryName = new FileInfo(directoryPath).Name;
              directoryName = directoryName.ToLower();
  
              if(directoryName == ".git")
              {
                  // Do not delete the .git directory
                  continue;
              }
  
              if (System.IO.Directory.Exists(directoryPath))
              {
                  DeleteDirectory(
                      directoryPath,
                      new DeleteDirectorySettings{
                          Recursive = true,
                          Force = true
                  });
              }
          }
      });
  
  Task("CopyToMasterBranch")
      .IsDependentOn("Build")
      .IsDependentOn("EmptyMasterBranch")
      .Does(() =&gt; {
          var sourcePath = "./output";
  
          Information("Copying files to master branch");
  
          // Now Create all of the directories
          foreach (string dirPath in System.IO.Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
          {
              System.IO.Directory.CreateDirectory(dirPath.Replace(sourcePath, tempDir));
          } 
  
          //Copy all the files &amp; Replaces any files with the same name
          foreach (string newPath in System.IO.Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
              System.IO.File.Copy(newPath, newPath.Replace(sourcePath, tempDir), true);
      });
  
  Task("CommitMasterBranch")
      .IsDependentOn("CopyToMasterBranch")
      .Does(() =&gt; {
          Information("Performing Git commit on master branch");
  
          GitAddAll(tempDir);
          GitCommit(tempDir, "Johan Vergeer", "johanvergeer@gmail.com", $"Automated release {gitVersion.InformationalVersion}");
      });
  
  Task("PushMasterBranch")
      .IsDependentOn("CommitMasterBranch")
      .Does(() =&gt; {
          Information("Pushing master branch to origin");
  
          GitPush(tempDir, githubUserName, githubAccessToken, "master");
      });
  </code></pre>