Skip to content

Instantly share code, notes, and snippets.

@wsmelton
Last active November 8, 2019 14:31
Show Gist options
  • Save wsmelton/2f78b2020b9856e7112175d7cbb435ca to your computer and use it in GitHub Desktop.
Save wsmelton/2f78b2020b9856e7112175d7cbb435ca to your computer and use it in GitHub Desktop.
Function you can add to your profile for resetting a given branch in your fork to the upstream branch.
function Reset-BranchToUpstream {
<#
.SYNOPSIS
Command to reset the branch to your upstream remote
.DESCRIPTION
Assumes you remote is setup as origin is your fork, and "upstream" is the remote source. You can pass in a branch, and it assumes you have this branch checked out on your origin or local repository.
.PARAMETER Branch
The desired branch you want to sync
.EXAMPLE
Reset-BranchToUpstream -Branch development
Reset my currently checked out branch to the upstream/development branch of my remote
#>
[cmdletbinding()]
param([string]$Branch)
if (Test-Path '.git') {
if ( (git remote -v | Select-String "upstream") ) {
$cmd = "git fetch upstream"
Invoke-Expression $cmd
$cmd = "git reset --hard upstream/$branch"
Invoke-Expression $cmd
$cmd = "git push origin $branch --force"
Invoke-Expression $cmd
} else {
Write-Output "No upstream remote found"
}
} else {
Write-Output "No git folder found"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment