Skip to content

Sync your fork with our repository (upstream)

Shawn Melton edited this page Nov 8, 2019 · 1 revision

Git clients such as GitHub Desktop, GitKraken, and even Visual Studio Code allow you to keep your forked repository of dabtools in sync with sqlcollaborative/dbatools repository by using a merge process in git. This commonly causes your commit history to include this message:

Merge branch 'development' of github.com:sqlcollaborative/dbatools into development

Overtime you may have a ton of these commit messages that keep showing up in your fork. A way to prevent these is to do a bit of advanced git command line. You can use the following three commands to perform a "reset" with your fork to the upstream branch, and it prevents the need for merges to be done:

git fetch upstream
git reset --hard upstream/development
git push origin development --force

You can check the git man pages if you want to know more about what reset does.

I have created a simple function you can add to your PowerShell $PROFILE script to easily handle this task in one command if you would like.

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"
    }
}