首页 文章

使用Git管理WordPress的本地版,开发版和实时版

提问于
浏览
2

我正在寻找使用git来管理WordPress项目的版本控制 .

我通常有一个本地版本,一个Web服务器上的开发版本,客户端可以访问和测试,然后是一个实时版本 .

我通常会在本地开发然后将我的更改推送到服务器上的repo,然后SSH到服务器上的dev文件夹并从repo中拉出来 . 当我想在实际站点上发布更改时,我将SSH连接到Web服务器上的live文件夹并从repo中提取 .

这是我的困境 .

我希望通过WordPress管理员(图片上传等)添加到本地版本的媒体被忽略,但我希望跟踪添加到Dev版本的媒体,所以当我拉到现场网站时,来自开发站点的媒体文件被拉 .

有关制作此类作品的最佳方法的任何想法?

1 回答

  • 5

    可能有更好的解决方案,但在类似的情况下,这两个选项对我来说都很好 . 假设您在本地工作的分支称为 master ,并且在所有存储库中,远程 origin 指的是您推送到的裸存储库 .

    • 如果您的所有媒体整齐地落入一个目录,您可以拥有一个单独的存储库来跟踪媒体,并将其作为submodule添加到开发人员和实时存储库中 . 在您的本地副本中,您应该:

    • [在你想要本地媒体的情况下]注意不要(a)在你的子模块中提交,(b)从它推送,(c)将子模块的新版本添加到你的 master 分支

    • [在您本地副本中根本不需要这些媒体的情况下]根本不更新和初始化子模块 - 只需将其路径添加到 .git/info/exclude

    即使您的媒体位于各种目录中,您仍然可以使用此方法,但管理子模块有点痛苦,如果您有很多这些子模块,则会更加恼人 .

    • 或者,您可以将您的dev和live存储库保存在不同的分支上,例如称为 with-media ,它始终与 master 相同,但提交会在历史记录末尾添加媒体 . 您可以使用 git rebase 维护此情况 . 例如,如果您刚刚在本地进行了一些想要推送的更改,那么您可以:
    git push origin master
    
    ssh server
    cd dev
    # git branch should show you that you're on 'with-media'
    git fetch origin
    git rebase origin/master
    

    现在假设您将一些文件上传到dev存储库,并想要提交它们:

    ssh server
    cd dev
    git add [whatever-media-files]
    git commit
    git push origin with-media
    

    现在要更新您的实时存储库,您可以这样做:

    ssh server
    cd live
    # git branch should show that you're on 'with-media'
    git fetch origin
    git merge origin/with-media
    

    要首先设置这些分支,您可以:

    ssh server
    cd dev
    # git branch should show that you're on 'master', since you haven't created
    # the other branch yet:
    git checkout -b with-media
    git push origin with-media
    
    cd ~/live
    git fetch origin
    git checkout -t origin/with-media
    

    最后一点,假设您在dev存储库中进行了一些更改,这些更改不仅仅是添加媒体,而且您希望在 master 分支中更改这些代码 . 然后(在你推动任何东西之前!)你需要用 git rebase -i origin/master 重新排序历史 .

    ssh server
      cd dev
      git rebase -i origin/master
      # Reorder the lines so that the non-media commits are first (earliest) in the file.
      # Save the file and exit your editor.
    
      # Now find the SHA1 sum (object name) of the last commit that has non-media changes.
      # (The object name of the commits will have been changed by the rebase.)
      git log --oneline
    
      # Let's say that was commit f414f3l.  Then you can push just the history up to and
      # including that commit back to the master branch with:
      git push origin f414f3l:master
    
      # Also push your 'with-media' branch back:
      git push origin with-media
    

    在理想的世界中,你不需要经常做最后的步骤,但在实践中,知道如何做到这一点很好:)

相关问题