How to sync from Visual Studio to Azure DevOps and GitHub at the same time
A project is clone from Azure DevOps to Visual studio, this project is taken from GitHub to Azure DevOps, Now we what push the changes in Visual Studio, both to Azure DevOps and GitHub Repository.
To push your changes to both Azure DevOps and GitHub simultaneously, you can add both as remotes in your local repository. Here’s how you can set it up in Visual Studio and push changes to both platforms.
Step 1: Clone the Repository from Azure DevOps
If you’ve already cloned your repository from Azure DevOps to Visual Studio, you’re good to go. If not:
1.Open Visual Studio.
2.Go to Git > Clone Repository.
3.Enter the URL of your Azure DevOps repository and clone it.
Step 2: Add GitHub as a Second Remote
1.Open the Git Repository Settings in Visual Studio:
(In the Team Explorer window, go to Settings > Repository Settings.)
2.Add the GitHub remote:
• Under the Remotes section, click Add.
• Name the new remote something like github.
• Enter the GitHub repository URL (e.g., https://github.com/username/repository.git).
• Click Save. as following figure:
You now have two remotes:
• origin (for Azure DevOps).
• github (for GitHub).
Step 3: Push to Both Remotes
Visual Studio doesn’t directly support pushing to multiple remotes at the same time, but you can use the Git Command Line integrated with Visual Studio or any terminal.
1.Open the Git Bash or Command Prompt in the repository directory.
2.Push to both remotes: one for one:
> git push origin main
> git push github main
Alternatively, you can create a Git alias to push to both remotes at once (optional):
1.Open your Git configuration file:
git config –global -e
2.Add an alias like this:
In Windows:
Go to path: C:\Program Files\Git\etc and open the file: gitconfig with notepad++ as admin and in the end of file add the following:
[alias]
pushall = !git push origin main && git push github main (main shall be master branch)
3.Use the new alias:
- go to the project repository folder (exmaple: C:\Utvecklingprogram\DevOps2) and run the following command:
> git pushall
Step 4: Automate Push in Visual Studio
If you prefer working entirely within Visual Studio, follow these steps:
1.Push to the first remote (e.g., Azure DevOps) using the Push option in the Git Changes window.
2.Manually switch to the second remote:
• Go to Git Repository Settings, set the github remote as the default, and then push again.
Bonus: Use a Script
For frequent pushes, consider creating a script that pushes to both remotes automatically:
#!/bin/bash
git push origin main
git push github main
You can execute this script whenever you want to push to both repositories.
This setup ensures that your code stays synchronized between Azure DevOps and GitHub.
This post is part of Azure DevOps Agile Boards step by step