As a consultant, starting a new project with a client can be a daunting task. One way to make the transition smoother is by cloning all the repositories on your first day. This allows you to have quick access to all the necessary files and resources, enabling you to perform your job efficiently and effectively. In this blog post, we will explore the benefits of cloning repositories, a script for doing so, and some common pitfalls to avoid.
Organizing your Git repos
When working for multiple clients or even just having private projects next to your client projects it can come in handy to organize your git repositories. For some Frontend repositories, the path with node_modules was too long and that forced me to place my folders on the Disk level. A path for a project for me would look like C:\Git\{ClientName}\{RepositoryName}
.
|
|
Using workspaces in Git Fork
Fork is a tool that will help you focus on the right workload. Using the structure as discussed with Fork, you can focus on the right repositories. Cloned new repositories but not seen by Fork? Reload the whole folder using right-click and ‘Rescan repositories’. Get Git Fork from git-fork.com.
Use Fork Workspaces to focus on the current environment. It will also help you work on private projects outside of work hours on the same workstation. You can also create workspaces for different domains or teams if you are for example the lead or architect in a project.
Configure your git username
Depending on the network infra, you will need to configure your commit username to the email of your client. Some instances block all git pushes from committers with a different domain.
|
|
In the script to clone all repositories, you can also enable the script to set the committer email for every repository.
Clone all repositories
To clone all repositories in Azure DevOps we can use the REST API to find all existing repositories. The code example consists of a Powershell script and a configuration file with settings and Authorization.
Configuration
Make sure to create a file named: CloneAllRepos.config
with the contents written below. Make sure every parameter is configured as your workspace.
|
|
💡 Don’t know where to find a Personal Access Token in Azure DevOps? Read: Microsoft’s docs on personal access tokens.
On 2024 april 17, I updated the script to get an access token using the current session of the az cli. see Azure DevOps API Authentication.
CloneAllRepos.ps1
When I first encountered the idea to clone all repos idea it was on a corporate wiki. After some backtracing, I found the source: Script to clone all Git repositories from your Azure DevOps collection.
The PowerShell script below does a git pull
for existing repositories and performs a git clone
on untracked repositories.
I edited the script to fit my needs with some extra parameters.
- It puts the repos in the given directory in settings.
- It prunes local branches when
PruneLocalBranches
is set to true. - It sets the git username email to the configured
GitUsername
under GitOptions, it’s ignored when empty.
|
|
Rewrite this PowerShell script to also <insert new Feature>. Here is the current version of the PowerShell script: <insert PowerShell script>.
. Let me know if you thought of a clever solution.Run it
Run the script it using a PowerShell prompt for example using for example Windows Terminal.
|
|
Using scripting for common tasks
In the world of microservices, we choose to duplicate some of the plumbing. When you want to change multiple repos knowledge on scripting can be helpful. In this series, I explored how to automate git tasks with PowerShell.
Some examples are:
- Updating multiple NuGet packages.
- Enforcing certain
Nuget.
config
configurations. - Renaming business terminology on multiple branches.
Automating
With this structure, you could automate actions over multiple repositories. In the code below I wrote an example of automating script for changing the Nuget.config file in every repository. If your packages have the same layout changes can be done easier and faster. Also, please check out my article using binary replace.
|
|
Conclusion and discussion
Make your workflow faster with scripting and your knowledge of the Git CLI. When you have to do repetitive tasks such as updating a single package on multiple (microservice-like) repositories, try to automate it. It may for the first occurrence not be profitable, but after three times, you will be faster than doing it manually. It can also help you clean up your workspace and be more tidy.