我想在默认情况下推拉所有分支,包括新创建的分支。

我可以为它定义一个设置吗?

否则,当我在本地添加一个新分支时,我想从服务器中获取它,最简单的方法是什么?

我创建了一个具有相同名称的新分支,并试图拉,但它不起作用。要求我提供分支机构的所有远程配置。怎么设置呢?


当前回答

添加新的远程回购,最后一步将在您推送时排除HEAD分支

git clone <url_of_old_repo>
cd <name_of_old_repo>
git remote add new-origin <url_of_new_repo>
git ls-remote . | grep 'refs/remotes/origin/' | grep -v 'HEAD' | awk -F 'origin/' '{print $2}' | xargs -i git push -f new-origin  --tags refs/remotes/origin/{}:refs/heads/{}

其他回答

在现代git中,你总是获取所有的分支(作为远程跟踪分支到refs/remotes/origin/*命名空间中,使用git branch -r或git remote show origin可见)。

默认情况下(请参阅push.default配置变量的文档),您会推送匹配的分支,这意味着首先您必须执行git push origin branch,以便git总是在git push上推送它。

如果你想一直推送所有分支,你可以设置push refspec。假设远程名为origin,你可以使用git配置:

$ git config --add remote.origin.push '+refs/heads/*:refs/heads/*'
$ git config --add remote.origin.push '+refs/tags/*:refs/tags/*'

或者直接编辑.git/config文件,得到如下内容:

[remote "origin"]
        url = user@example.com:/srv/git/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        fetch = +refs/tags/*:refs/tags/*
        push  = +refs/heads/*:refs/heads/*
        push  = +refs/tags/*:refs/tags/*

要查看所有没有使用git branch -a的分支,你应该执行:

for remote in `git branch -r`; do git branch --track $remote; done
git fetch --all
git pull --all

现在你可以看到所有的分支:

git branch

要推动所有的分支试试:

git push --all

对我来说,转移所有分支和标签的完整过程是,结合@vikas027和@kumarahul的答案:

~$ git clone <url_of_old_repo>
~$ cd <name_of_old_repo>
~$ git remote add new-origin <url_of_new_repo>
~$ git push new-origin --mirror
~$ git push new-origin refs/remotes/origin/*:refs/heads/*
~$ git push new-origin --delete HEAD

最后一步是因为一个名为HEAD的分支由于通配符出现在新的远程中

我使用下面的命令将所有分支迁移到新的存储库。

~$ git clone --mirror <url_of_old_repo>
~$ cd <name_of_old_repo>
~$ git remote add new-origin <url_of_new_repo>
~$ git push new-origin master
~$ git push new-origin --mirror

注意:当从Atlassian Stash克隆一个repo到AWS CodeCommit(空白repo)时,我不得不使用第二个最后(即推主第一个)命令。我不确定原因,但在push (git push new-origin——mirror) default branch指的是master以外的一些其他分支。

解决方案没有硬编码的配置来源

在全局gitconfig中使用以下命令

[remote]
    push = +refs/heads/*
    push = +refs/tags/*

这将推送所有分支和所有标签

为什么你不应该硬编码的来源配置?

如果你硬编码:

你最终会在所有的回购中使用origin作为遥控器。你不能添加origin,你需要使用set-url。 如果一个工具用不同的名称创建了一个远程,则push all配置将不应用。然后你将不得不重命名远程,但重命名将不起作用,因为原点已经存在(从点1)记住:)

现代git已经在处理抓取了

美国每年Jakub刚纳ębski的答案:

使用现代git,你总是获取所有分支(作为远程跟踪分支)到refs/remotes/origin/*命名空间