设置 git 来拉取和推送所有分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1914579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:31:19  来源:igfitidea点击:

Set up git to pull and push all branches

gitversion-controlbranchpush

提问by Lakshman Prasad

I'd like to push and pull all the branches by default, including the newly created ones.

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

Is there a setting that I can define for it?

是否有我可以为其定义的设置?

Otherwise, when I add a new branch, locally and I want to pull it from the server, what is the simplest way to do it?

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

I created a new branch with the same name and tried to pull but it doesn't work. Asks me for all the remote config of the branch. How do I set it.

我创建了一个同名的新分支并试图拉,但它不起作用。向我询问分支的所有远程配置。我该如何设置。

回答by brimble2010

The simplest way is to do:

最简单的方法是:

git push --all origin

This will push tags and branches.

这将推送标签和分支。

回答by Jakub Nar?bski

With modern git you always fetch all branches(as remote-tracking branches into refs/remotes/origin/*namespace, visible with git branch -ror git remote show origin).

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

By default (see documentation of push.defaultconfig variable) you push matching branches, which means that first you have to do git push origin branchfor git to push it always on git push.

默认情况下(请参阅push.default配置变量的文档)您推送匹配的分支,这意味着首先您必须git push origin branch让 git 将其推送到 always on git push

If you want to always push all branches, you can set up push refspec. Assuming that the remote is named originyou can either use git config:

如果你想总是推送所有分支,你可以设置推送 refspec。假设远程已命名,origin您可以使用git config

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

or directly edit .git/configfile to have something like the following:

或直接编辑.git/config文件以具有如下内容:

[remote "origin"]
        url = [email protected]:/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/*

回答by Mark Reed

Including the + in the push spec is probably a bad idea, as it means that git will happily do a non-fast-forward push even without -f, and if the remote server is set up to accept those, you can lose history.

在推送规范中包含 + 可能是一个坏主意,因为这意味着即使没有 -fgit 也会很乐意进行非快进推送,如果远程服务器设置为接受这些,您可能会丢失历史记录。

Try just this:

试试这个:

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

回答by vikas027

I had used below commands to migrate all branches to the new repository.

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

~$ 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

NOTE: I had to use second last (i.e. push master first) command while cloning a repo from Atlassian Stashto AWS CodeCommit(blank repo). I am not sure the reason, but after pushing (git push new-origin --mirror) default branch was referring to some other branch than master.

注意:在将存储库从 Atlassian Stash克隆到 AWS CodeCommit(空白存储库)时,我必须使用倒数第二(即先推主控)命令。我不确定原因,但在 push ( git push new-origin --mirror) 之后,默认分支指的是除master.

回答by Lance Cleveland

If you are moving branches to a new repo from an old one and do NOT have all the old repo branches local, you will need to track them first.

如果您要将分支从旧仓库移动到新仓库,并且没有本地所有旧仓库分支,则需要先跟踪它们。

for remote in `git branch -r | grep -v '\->'`; do git branch --track $remote; done

Then add your new remote repo:

然后添加你的新远程仓库:

git remote add bb <path-to-new-repo>

Then you can push all using this command:

然后您可以使用此命令推送所有内容:

git push -u bb --all

Or you can configure the repo using the git config commands noted in the other responses here if you are not doing this one time or are only looking to move local branches.

或者,如果您一次不这样做或只想移动本地分支,则可以使用此处其他响应中提到的 git config 命令配置存储库。

The important point, the other responses only push all LOCAL branches. If the branches only exist on an alternate REMOTE repository they will not move without tracking them first. The for loop presented here will help with that.

重要的一点是,其他响应只会推送所有 LOCAL 分支。如果分支仅存在于备用 REMOTE 存储库中,则在不首先跟踪它们的情况下它们不会移动。此处介绍的 for 循环将对此有所帮助。

回答by tokhi

To see all the branches with out using git branch -ayou should execute:

要在不使用的情况下查看所有分支,git branch -a您应该执行:

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

Now you can see all the branches:

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

git branch

To push all the branches try:

要推送所有分支,请尝试:

git push --all

回答by abulka

If you are moving all branches to a new repo from an old one then in your local repo you need to set up tracking of each branch to existing origin branches, before pushing to the new repo, otherwise allyour origin branches won't appear in the new origin. Do this manually by tracking or checking out each branch, or use the one liner:

如果您要将所有分支从旧仓库移动到新仓库,那么在本地仓库中,您需要在推送到新仓库之前设置每个分支到现有原始分支的跟踪,否则所有原始分支都不会出现在新的起源。通过跟踪或检查每个分支手动执行此操作,或使用一个班轮:

for remote in `git branch -r | grep -v '\->' | grep -v master`; do git branch --track `echo $remote|sed 's=origin/=='` `echo $remote`; done

This one line command is based on versions of it in other answers on this page, but is arguably better because:

这个单行命令基于本页其他答案中的版本,但可以说更好,因为:

  1. it correctly sets up the branch tracking, unlike some older variants of this command on this page which only supply one parameter to --track and thus each branch ends up tracking master - not good
  2. names the local branches without the prefix “origin/” which I personally don't want - and is consistent with what happens when you checkout a branch normally.
  3. skips tracking master since that is already happening
  4. doesn't actually checkout anything thus is fast
  5. avoids stumbling over the -> in the output of git branch -r
  1. 它正确设置了分支跟踪,这与此页面上此命令的一些旧变体不同,后者仅向 --track 提供一个参数,因此每个分支最终都会跟踪 master - 不好
  2. 命名本地分支时不带前缀“origin/”,这是我个人不想要的 - 并且与您正常结帐分支时发生的情况一致。
  3. 跳过跟踪大师,因为这已经发生了
  4. 实际上不结帐任何东西因此很快
  5. 避免在 git branch -r 的输出中绊倒 ->

Next, if you are switching origins, replace the link to the old origin and point to a new remote. Ensure you create the new remote first, using bitbucket/github GUI, but don't add any files to it or there will be a merge problem. E.g.

接下来,如果您要切换原点,请替换旧原点的链接并指向新的遥控器。确保首先使用 bitbucket/github GUI 创建新的远程,但不要向其中添加任何文件,否则会出现合并问题。例如

git remote set-url origin [email protected]:YOUR/SOMEREPO.git

Now push. Note the second command is needed to push the tags as well:

现在推。请注意,还需要第二个命令来推送标签:

git push -u --all origin
git push --tags origin

回答by Dheeraj Bhaskar

Solution without hardcoding originin config

origin在配置中没有硬编码的解决方案

Use the following in your globalgitconfig

在您的全局gitconfig 中使用以下内容

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

This pushes all branches and all tags

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

Why should you NOT hardcode originin config?

为什么不应该origin在配置中硬编码?

If you hardcode:

如果你硬编码:

  1. You'll end up with originas a remote in all repos. So you'll not be able to add origin, but you need to use set-url.
  2. If a tool creates a remote with a different name push all config will not apply. Then you'll have to rename the remote, but rename will not work because originalready exists (from point 1) remember :)
  1. 你最终会origin成为所有 repos 中的一个遥控器。因此您将无法添加原点,但您需要使用set-url.
  2. 如果工具使用不同的名称创建遥控器,则推送所有配置将不适用。然后你必须重命名遥控器,但重命名不起作用,因为origin已经存在(从第 1 点开始)记住:)

Fetching is taken care of already by modern git

获取已经由现代 git 处理了

As per Jakub Nar?bski's answer:

根据 Jakub Nar?bski 的回答:

With modern git you always fetch all branches (as remote-tracking branches into refs/remotes/origin/* namespace

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