发布现有的回购

时间:2015-08-28 19:22:24

标签: git

在这里,我再次使用我一周的回购,并担心我的开发计算机将落入游泳池。现在我想将我的本地仓库发送到我拿着我的裸存储库的主机,我必须这样做,再次,以下内容:

git clone --bare my-repo my-repo.git
ssh git@myserver "mkdir /opt/git/placeholder-dir"
scp -r my-repo.git git@myserver:/opt/git/praceholder-dir
cd my-repo
git add remote origin myserver-blah-blah

是不是有更好的(更短,更容易,更不容易出错)的方法呢?

2 个答案:

答案 0 :(得分:3)

您正在做一些不必要的工作。我会跑:

ssh git@myserver git init --bare /opt/git/placeholder-dir

然后,从my-repo目录中:

git add remote origin git@myserver:placeholder-dir
git push origin master

这似乎更容易。

<强>更新

如果您是人推送到您的远程服务器,您可以将以下内容添加到名为gitwrapper的脚本中:

#!/bin/bash

[ "$SSH_ORIGINAL_COMMAND" ] || exit 1

eval set -- $SSH_ORIGINAL_COMMAND

# if this script is triggered by a push operation
if [ "$1" = "git-receive-pack" ] ; then
    if [[ "$2" == */* ]]; then
            echo "*** Repository names must not contain '/'" >&2
            exit 1
    fi

    if [[ "$2" == .* ]]; then
            echo "*** Repository names must not start with '.'" >&2
            exit 1
    fi

    if [[ "$2" != *.git ]]; then
            echo "*** Repository names must end with '.git'" >&2
            exit 1
    fi

    if ! [ -d "$2" ]; then
            echo "Creating repository $2" >&2
            git init --bare "$2" >&2
    fi
fi

exec "$@"

然后在ssh_config文件中,指定command=选项:

command="/home/git/bin/gitwrapper" ssh-rsa AAAA...

(用{的实际路径替换/home/git/bin/gitwrapper gitwrapper脚本)。

如果有脚本,此脚本将自动创建目标存储库 不存在。所以我可以这样做:

$ git push git@localhost:myrepo.git

见:

Creating repository myrepo.git
Initialized empty Git repository in /home/git/myrepo.git/
Counting objects: 301, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (290/290), done.
Writing objects: 100% (301/301), 159.68 KiB | 0 bytes/s, done.
Total 301 (delta 156), reused 0 (delta 0)
To git@localhost:myrepo.git
 * [new branch]      master -> master

特别注意该输出的前两行。当然, 拼写错误是一个问题:

$ git push git@localhost:mtrepo.git

糟糕。

希望不言而喻,但如果除了你以外的任何人 推送到你的远程服务器这个脚本会很糟糕, 可怕的主意。它没有做任何路径检查,所以它会愉快 在目标用户具有写访问权限的任何位置创建存储库。它 甚至可以允许任意命令执行。

答案 1 :(得分:3)

您可以创建一个用于备份的远程仓库并将所有分支和标记推送到它:

ssh git@myserver "git init --bare /opt/git/placeholder-dir"
git add remote backup myserver-blah-blah

当您需要备份本地仓库时

git push backup --all
git push backup --tags

您发送的更改不会被推送到备份

相关问题