Skip to content

Instantly share code, notes, and snippets.

@mu88
Last active March 1, 2018 16:37
Show Gist options
  • Save mu88/45c5c393d09fe3fabfc2654528ef35a6 to your computer and use it in GitHub Desktop.
Save mu88/45c5c393d09fe3fabfc2654528ef35a6 to your computer and use it in GitHub Desktop.
Git - Create develop-Branch in several Git repositories
Clear-Host
$ErrorActionPreference = 'Stop'
# get all existing Git repositories => For this, C:\Git get filtered for all folders ending on 'v' and a digit because only those folders are accesibble via Git-Server
$Git_Repos = Get-ChildItem C:\Git | ?{ $_.PSIsContainer } | Where-Object {$_.Name -match '\w*v\d\b'} | Select-Object Name
$Git_Server = 'https://www.MyGitServer.com'
$Git_Branch = 'develop'
$Git_LocalCloneDirectory = 'C:\temp'
ForEach($Git_Repo in $Git_Repos)
{
cd $Git_LocalCloneDirectory
$Git_Repo_Name = $Git_Repo.Name
$Git_Repo_AbsoluteURI = "$Git_Server/$Git_Repo_Name.git"
# get all remote branches and check whether a develop-branch already exists
$Git_Repo_RemoteBranches = git ls-remote $Git_Repo_AbsoluteURI
$Git_Repo_ContainsDevelopBranch = $FALSE
ForEach($Git_Repo_RemoteBranch in $Git_Repo_RemoteBranches)
{
# the remote-branch-information is tab-delimited string => second part is the branch name
$Git_Repo_RemoteBranchName = $Git_Repo_RemoteBranch.Split("`t")[1]
if ($Git_Repo_RemoteBranchName -like "*$Git_Branch*")
{
$Git_Repo_ContainsDevelopBranch = $TRUE
break
}
}
# if the develop branch already exists, it doesn't have to be created
if ($Git_Repo_ContainsDevelopBranch)
{
continue
}
"Git repo '$Git_Repo_Name' does not have a $Git_Branch`-branch"
$Git_Repo_LocalFolder = Join-Path -Path $Git_LocalCloneDirectory -ChildPath $Git_Repo_Name
"Cloning Git repo '$Git_Repo_Name' to '$Git_Repo_LocalFolder'"
Try
{
git clone $Git_Repo_AbsoluteURI -q
Start-Sleep 2
}
Catch
{
}
cd $Git_Repo_LocalFolder
"Creating new branch '$Git_Branch'"
git checkout -b $Git_Branch -q
"Push branch '$Git_Branch' to origin"
git push origin $Git_Branch -q
Start-Sleep 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment