如何使用 git 创建生产环境和开发环境

How make prod and dev envs using git

我想改进我的工作流程,所以我决定自己使用 git。

到目前为止,对于我的项目,在我的 Web 服务器上,我有 2 个文件夹,分别是 prod 和 dev。

修改完成后,我会同步我的两个文件夹。

我试过这样使用 git :

// on my prod folder
$ git init 
$ git add *
$ git commit -a

然后在父文件夹

$ git clone prod dev

工作起来很有魅力我得到了我的开发文件夹作为产品的完美婴儿床 但是现在一旦我的修改没问题,我就不能承诺生产了。我怎样才能做到这一点?!感谢

编辑

这是我的网络服务器上的组织树:

home
|--git
   |--.ssh
   |--repositories
      |-- myfirstrepo.git
          |-- index.html

我要实现的目标来自 /var/www

$ git clone /home/git/repositories/myfirstrepo.git
Initialized empty Git repository in /var/www/myfirstrepo_wc/.git/

我在

中获得了我的存储库的克隆
var
|--www
   |--myfirstrepo_wc
      |--index.html

所以现在我在 /var/www/myfirstrepo_wc 中更改我的 index.html 文件,然后

$ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   www/index.html
#
no changes added to commit (use "git add" and/or "git commit -a")

提交

$ git commit -a
[master d19749d] first commit from working copy
Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

git commit --amend --author='Your Name <you@example.com>'

1 files changed, 1 insertions(+), 0 deletions(-)

然后想拉到我在 /home/git/repositories/myfirstrepo.git

中的回购
$ git pull origin master
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 358 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /git/myfirstrepo.git
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/git/myfirstrepo.git'

Two folders with two .git folders 意味着你有两个 git 仓库,你想要的是一个文件夹有一个 .git 文件夹和两个分支,"prod" 用于生产,"dev" 用于开发,并在两者之间切换 "checkout " 您想要查看的分支。一旦你有了这个,一旦你完成了对开发的更改并且它们已准备好用于生产,你就可以将更改从开发拉到生产,虽然这不是理想的开始。 在您的情况下,您可以:

  1. 我们有 2 个文件夹,每个文件夹都有 .git,让我们从 dev
  2. 中删除 .git 文件夹
  3. 在 prod 文件夹中打开你的 git 控制台,你会发现你在分支 master 中。
  4. 键入 git branch dev ,这将创建一个基于 master 的名为 dev 的新分支(假设 master = prod)
  5. 输入git checkout dev移动到dev分支
  6. 现在将dev文件夹的内容复制到prod文件夹中。
  7. 在控制台中键入 git status,您会注意到已更改的文件被标记为这样。
  8. 键入 git add -a 将所有更改添加到您的开发分支
  9. 输入git commit -m ""
  10. 现在您在一个存储库中有两个分支。

现在假设您对 dev 中的更改感到满意并希望将它们添加到 master,

  1. 使用 git checkout master
  2. 将您的分支更改为 master
  3. 类型git pull dev
  4. 假设没有冲突,现在 master 与 develop 同步。

请记住,这只是为了让您开始使用 git,尝试使用玩具存储库进行试验并使用补充工具。

好的,我找到了解决方案:

$git remote add origin git@localhost:path/to/myfirstrepo.git
$git push origin master