Automating Git Repository Creation on GitHub and Azure DevOps with Git

Managing code across multiple repositories on different platforms can be time-consuming, especially when pushing to both GitHub and Azure DevOps. This guide provides a streamlined workflow to create and configure Git repositories locally, set up GitHub and Azure DevOps remotes, and push code to both platforms simultaneously—all from the first commit.

Prerequisites

1. Personal Access Tokens (PATs)

To interact with GitHub and Azure DevOps programmatically, you’ll need to create Personal Access Tokens (PATs) on each platform. PATs act as a substitute for passwords and allow you to authenticate CLI commands.

  1. GitHub:
  • Go to GitHub’s PAT setup page.
  • Click Generate new token, give it a name, and set an expiration date.
  • Under Select scopes, choose at least repo (for repository access).
  • Click Generate token and copy the token to a safe place.
  1. Azure DevOps:
  • Go to your Azure DevOps PAT setup page.
  • Navigate to User Settings > Personal Access Tokens.
  • Select New Token, set a name, organization, expiration date, and scope.
  • Choose at least Code (read and write) permissions for repository access.
  • Click Create and copy the token somewhere secure.

These tokens will be needed to authenticate and push code to each platform through command-line interfaces.

2. CLI Tools

To create repositories directly from the command line, install the following tools:

Step-by-Step Workflow

Step 1: Initialize the Local Git Repository

Open your terminal or command line, navigate to your project folder, and initialize the Git repository:

# Initialize the Git repository
git init
git add .
git commit -m "Initial commit"

Step 2: Authenticate and Set Up CLI Tools

Authenticate with GitHub and Azure DevOps using the CLI tools and your PATs:

  1. GitHub CLI:
   gh auth login
   # Follow the prompts to authenticate with your GitHub PAT
  1. Azure CLI:
   az login
   # Follow prompts to log into your Azure account

Step 3: Create the GitHub Repository

Using the gh CLI, create a new repository on GitHub. This command will also set up GitHub as a remote for your project.

# Create a new GitHub repository and add it as the 'origin' remote
gh repo create <repository-name> --public --confirm

This will automatically configure the GitHub remote as origin in your Git configuration.

Step 4: Configure Azure DevOps for Repository Creation

  1. First, configure your Azure DevOps organization and project to ensure the repository is created in the correct place:
   az devops configure --defaults organization=https://dev.azure.com/<your-organization> project=<your-project>
  1. Next, use the Azure CLI to create a new repository within the specified project:
   az repos create --name <repository-name>
  1. Add Azure DevOps as a second remote, named azure:
   git remote add azure https://dev.azure.com/<your-organization>/<your-project>/_git/<repository-name>

Step 5: Push Code to Both Remotes

Now that both GitHub and Azure DevOps are configured as remotes (origin for GitHub and azure for Azure DevOps), you can push your initial commit to both:

# Push to GitHub
git push origin main

# Push to Azure DevOps
git push azure main

To streamline future pushes, you can push to both remotes in one command:

git push --all origin && git push --all azure

Wrapping Up

With this setup, your local Git repository is configured to automatically push updates to both GitHub and Azure DevOps. You can repeat the git push --all origin && git push --all azure command whenever you want to update both platforms. This workflow is ideal for projects where visibility, redundancy, or backup across different platforms is important.

By automating repository creation and configuring multi-remote pushing, you save time and maintain a consistent codebase on both GitHub and Azure DevOps. This setup will simplify collaboration and keep your code accessible, backed up, and versioned across platforms.

Edvaldo Guimrães Filho Avatar

Published by

Categories: , ,