How do I force a subtree push to overwrite remote changes?

后端 未结 4 1126
夕颜
夕颜 2021-01-31 08:03

We use a subtree deployment a lá this Gist to deploy a subdirectory of our Yeoman project. In our case, the branch is called production, not gh-pages.

相关标签:
4条回答
  • 2021-01-31 08:43

    There is actually a solution that is much more simple.

    Source: https://gist.github.com/cobyism/4730490#gistcomment-2337463

    Setup

    $ rm -rf dist
    $ echo "dist/" >> .gitignore
    $ git worktree add dist gh-pages
    

    Making changes

    $ make # or what ever you run to populate dist
    $ cd dist
    $ git add --all
    $ git commit -m "$(git log '--format=format:%H' master -1)"
    $ git push origin production --force
    $ cd ..
    
    0 讨论(0)
  • 2021-01-31 08:50

    The trick was to chain the subtree split into a forced push:

    git push origin `git subtree split --prefix dist master`:production --force
    

    I got this from the Addendum of http://clontz.org/blog/2014/05/08/git-subtree-push-for-deployment/, who actually references this answer on Stack Overflow. I had skimmed this one earlier but Steve Clontz's post made it click for me.

    0 讨论(0)
  • 2021-01-31 08:54

    There is a simpler solution

    This always works for me.

    1. Set up a bash script which helps you do the following:
    cd dist
    git init
    git add .
    git commit -am "deploy"
    git push --force <remote URL> master
    rm -rf .git
    cd ..
    

    This creates a new git repository in the subdirectory you specify and force pushes everything from there.

    1. profit
    0 讨论(0)
  • 2021-01-31 08:54

    I've just enhanced the answer of ele adding generic expressions <> to make it more clear:

    git push <remote> `git subtree split --prefix <local-folder> <remote-branch>`:<remote-branch> --force
    

    Example values

    <remote>

    origin
    other-origin-name
    https://github.com/full-url/of-repo
    

    <local-folder>

    Local folder relative to repo root (execute this command in root), e.g.: /dist

    <remote-branch>

    master
    any-other-branchname
    
    0 讨论(0)
提交回复
热议问题