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

发布于 2024-08-14 17:57:41 字数 157 浏览 4 评论 0 原文

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

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

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

我创建了一个同名的新分支并尝试拉取,但它不起作用。要求我提供分支的所有远程配置。我该如何设置呢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

迷乱花海 2024-08-21 17:57:42

最简单的方法是:

git push --all origin

这将推送标签和分支。

The simplest way is to do:

git push --all origin

This will push tags and branches.

许你一世情深 2024-08-21 17:57:42

使用现代 git,您始终获取所有分支(作为远程跟踪分支到 refs/remotes/origin/* 命名空间,通过 gitbranch -r 可见> 或git Remote show origin)。

默认情况下(请参阅 push.default 配置变量的文档),您推送匹配分支,这意味着您首先必须执行 git推送原始分支 git 始终将其推送到 git Push 上。

如果您想始终推送所有分支,您可以设置推送引用规范。假设远程名称为 origin,您可以使用 git config

$ 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 = [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/*

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

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

If you want to always push all branches, you can set up push refspec. Assuming that the remote is named origin you can either use 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/config file to have something like the following:

[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/*
指尖微凉心微凉 2024-08-21 17:57:42

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

~$ 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 克隆存储库时,我必须使用倒数第二个(即先推送 master)命令 存储 到 AWS CodeCommit(空白回购)。我不确定原因,但在推送(git push new-origin --mirror)后,默认分支引用了 master 之外的其他分支。

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 Stash to 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.

可爱暴击 2024-08-21 17:57:42

在推送规范中包含 + 可能是一个坏主意,因为这意味着 git 会很乐意执行非快进推送即使没有 -f,并且如果远程服务器设置为接受那些,你可能会失去历史。

试试这个:

$ 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/*'

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.

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/*'
画中仙 2024-08-21 17:57:42

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

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

然后添加新的远程存储库:

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

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

git push -u bb --all

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

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

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.

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.

往日 2024-08-21 17:57:42

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

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

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

  1. 它正确地设置了分支跟踪,与某些分支跟踪不同 不好
  2. 此页面上此命令的旧版本仅向 --track 提供一个参数,因此每个分支最终都会跟踪 master -没有我个人不想要的前缀“origin/”的本地分支 命名 - 并且是一致的当你正常结帐分支时会发生什么。
  3. 跳过跟踪主机,因为这种情况已经发生,
  4. 实际上并没有检查任何内容,因此速度很快,
  5. 可以避免绊倒 ->接下来

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

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

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

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

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 all your 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

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.

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
又怨 2024-08-21 17:57:42

我找到了最好和最简单的方法这里,就像@kumarahul 发布,对我来说就像一个魅力,它将所有标签和分支从原点推送到新的远程:

git remote add newremote new-remote-url

git push newremote --tags refs/remotes/origin/*:refs/heads/*

我使用了“git push --all -u newremote”,但它只将签出的分支推送到新的远程。

Git:将所有分支推送到新的远程

作者:Keith Dechant,软件架构师

下面是一些人可能在使用 Git 时遇到过的情况
存储库。你有一个 Git 存储库的工作副本,比如来自旧版本的
服务器。但你只有工作副本,而源不是
可以访问。所以你不能只是分叉它。但你想推动整个
存储库和所有分支历史记录到您的新远程。

如果您的工作副本包含跟踪分支,则这是可能的
来自旧遥控器(origin/branch1、origin/branch1 等)。如果你这样做,
您拥有整个存储库和历史记录。

但是,就我而言,有数十个分支机构,并且部分或全部
我从来没有在当地检查过它们。推动他们所有人似乎是
重升。那么,如何进行呢?

我确定了两个选项:

选项 1:检查每个分支并推送 我可以做到这一点,而且我可以
甚至编写一个 Bash 脚本来提供帮助。然而,这样做会改变我的
每次结帐时都会创建工作文件,并会创建一个本地分支
每个远程跟踪分支。对于大的情况来说这会很慢
回购协议。

选项 2:推送而不更改工作副本还有第二个
另一种方法不需要检查每个分支,也不需要
在工作副本中创建无关的分支,甚至不
修改工作副本中的文件。

如果您的旧的、不再有效的遥控器称为“oldremote”,并且您的
新的遥控器称为“newremote”,您可以只推送遥控器
使用此命令跟踪分支:

git Push newremote refs/remotes/oldremote/*:refs/heads/*

在某些情况下
在这种情况下,也可以仅推送分支的子集。如果
分支名称以斜杠命名(例如,
oldremote/features/branch3、oldremote/features/branch4 等),您可以
仅推送名称以以下开头的远程跟踪分支
“旧远程/功能”:

git Push newremote refs/remotes/oldremote/features/*:refs/heads/features/*

无论你
推送所有分支或仅其中一些分支,Git 将执行
整个操作无需创建任何新的本地分支机构,也无需
更改您的工作文件。每个跟踪分支
匹配您的模式将被推送到新的遥控器。

有关该主题的更多信息,请查看 Stack 上的此线程
溢出。

发布日期:2017 年 10 月 9 日

I found the best and simplest method here, just as @kumarahul posted, works like a charm for me, it will push all the tags and branches from origin to the new remote:

git remote add newremote new-remote-url

git push newremote --tags refs/remotes/origin/*:refs/heads/*

I used 'git push --all -u newremote', but it only push the checkouted branches to the newremote.

Git: Push All Branches to a New Remote

by Keith Dechant , Software Architect

Here's a scenario some of you might have encountered with your Git
repositories. You have a working copy of a Git repo, say from an old
server. But you only have the working copy, and the origin is not
accessible. So you can't just fork it. But you want to push the whole
repo and all the branch history to your new remote.

This is possible if your working copy contains the tracking branches
from the old remote (origin/branch1, origin/branch1, etc.). If you do,
you have the entire repo and history.

However, in my case there were dozens of branches, and some or all of
them I had never checked out locally. Pushing them all seemed like a
heavy lift. So, how to proceed?

I identified two options:

Option 1: Checkout every branch and push I could do this, and I could
even write a Bash script to help. However, doing this would change my
working files with each checkout, and would create a local branch for
each of the remote tracking branches. This would be slow with a large
repo.

Option 2: Push without changing your working copy There is a second
alternative, which doesn't require a checkout of each branch, doesn't
create extraneous branches in the working copy, and doesn't even
modify the files in the working copy.

If your old, no-longer-active remote is called "oldremote" and your
new remote is called "newremote", you can push just the remote
tracking branches with this command:

git push newremote refs/remotes/oldremote/*:refs/heads/*

In some
cases, it's also possible to push just a subset of the branches. If
the branch names are namespaced with a slash (e.g.,
oldremote/features/branch3, oldremote/features/branch4, etc.), you can
push only the remote tracking branches with names beginning with
"oldremote/features":

git push newremote refs/remotes/oldremote/features/*:refs/heads/features/*

Whether you
push all the branches or just some of them, Git will perform the
entire operation without creating any new local branches, and without
making changes to your working files. Every tracking branch that
matches your pattern will be pushed to the new remote.

For more information on the topic, check out this thread on Stack
Overflow.

Date posted: October 9, 2017

苄①跕圉湢 2024-08-21 17:57:42

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

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

现在您可以看到所有分支:

git branch

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

git push --all

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

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
萌化 2024-08-21 17:57:42

对我来说传输 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 的分支出现在由于通配符而导致的新遥控器

The full procedure that worked for me to transfer ALL branches and tags is, combining the answers of @vikas027 and @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

The last step is because a branch named HEAD appears in the new remote due to the wildcard

缱绻入梦 2024-08-21 17:57:42

如果您要从一个远程源推送到另一个远程源,您可以使用以下命令:

git push newremote refs/remotes/oldremote/*:refs/heads/*

这对我有用。请参阅:https://www.metaltoad.com/博客/git-push-all-branches-new-remote

If you are pushing from one remote origin to another, you can use this:

git push newremote refs/remotes/oldremote/*:refs/heads/*

This worked for me. Reffer to this: https://www.metaltoad.com/blog/git-push-all-branches-new-remote

懷念過去 2024-08-21 17:57:42

2012 年答案 提到 git push --all origin 为“这将推送标签和分支。 ”

嗯...不:它将 仅推送所有分支(即refs/heads/下的参考文献,不是refs/tags/下的参考文献)

这就是为什么在2023年, Git 2.41,“设置 git 拉动和推送所有分支”的答案是:

git push -u --branches

因为使用 Git 2.41 (Q2 2023),“git push --all"(man) 获得了别名 < a href="https://github.com/git/git/blob/f37da977232ff490343d480dfbcd6afcc4d5523e/Documentation/git-push.txt#L149-L152" rel="nofollow noreferrer">git push --branches(man ”。

请参阅 提交 022fbb6(2023 年 5 月 12 日),作者:伊利亚·纽伦 (newren)
请参阅 提交 425b4d7(2023 年 5 月 6 日),作者:腾龙 (dyrone)
(由 Junio C Hamano -- gitster -- 合并于 提交 f37da97,2023 年 5 月 15 日)

push:引入“--branches”选项

签字人:腾龙

git-push( man) 内置 cmd 支持将所有分支(refs/heads 下的引用)推送到远程。
使用该功能后,用户可以轻松完成一些场景,例如分支同步、批量上传等。

'--all'已经被引入很长时间了,同时git支持自定义refs/下的存储位置。
当新的 Git 用户看到类似“git push origin --all”的用法时,我们可能会觉得我们正在推送所有引用,而不仅仅是分支,而无需查看在文档中,直到我们找到它的相关描述或“--mirror”。

为了保证兼容性,我们不能直接将 '--all' 重命名为其他名称,一种方法是,我们可以尝试添加一个新选项 '--heads ' 与 '--all' 的功能相同,让用户更清楚地理解表示的含义。
实际上,我们或多或少已经以这种方式命名了选项,例如,在 'git-show-ref' 和 'git ls-remote'(man)

git Push 现在包含在其 手册页

<代码>'git Push' [--all | --分支机构| --镜子| --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=]

git Push 现在包含在其 手册页

--分支

The 2012 answer mentions git push --all origin as "This will push tags and branches."

Well... no: it will push only all branches (i.e. refs under refs/heads/, not under refs/tags/)

That is why in 2023, with Git 2.41, the answer to "Set up git to pull and push all branches" would be:

git push -u --branches

Because with Git 2.41 (Q2 2023), "git push --all"(man) gained an alias git push --branches(man)".

See commit 022fbb6 (12 May 2023) by Elijah Newren (newren).
See commit 425b4d7 (06 May 2023) by Teng Long (dyrone).
(Merged by Junio C Hamano -- gitster -- in commit f37da97, 15 May 2023)

push: introduce '--branches' option

Signed-off-by: Teng Long

The '--all' option of git-push(man) built-in cmd support to push all branches (refs under refs/heads) to remote.
Under the usage, a user can easlily work in some scenarios, for example, branches synchronization and batch upload.

The '--all' was introduced for a long time, meanwhile, git supports to customize the storage location under refs/".
When a new Git user see the usage like, 'git push origin --all', we might feel like we're pushing all the refs instead of just branches without looking at the documents until we found the related description of it or '--mirror'.

To ensure compatibility, we cannot rename '--all' to another name directly, one way is, we can try to add a new option '--heads' which be identical with the functionality of '--all' to let the user understand the meaning of representation more clearly.
Actually, We've more or less named options this way already, for example, in 'git-show-ref' and 'git ls-remote'(man).

git push now includes in its man page:

'git push' [--all | --branches | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]

git push now includes in its man page:

--branches

猫性小仙女 2024-08-21 17:57:42

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

global gitconfig中使用以下内容

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

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

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

如果您进行硬编码:

  1. 您最终将在所有存储库中将 origin 作为远程。因此您将无法添加来源,但您需要使用set-url
  2. 如果工具创建具有不同名称的远程,则所有配置将不适用。然后你必须重命名远程,但重命名将不起作用,因为 origin 已经存在(从第 1 点开始)记住:)

现代 git 已经处理了提取

按照 Jakub Narębski 的回答:

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

Solution without hardcoding origin in config

Use the following in your global gitconfig

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

This pushes all branches and all tags

Why should you NOT hardcode origin in config?

If you hardcode:

  1. You'll end up with origin as 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 origin already exists (from point 1) remember :)

Fetching is taken care of already by modern git

As per Jakub Narębski's answer:

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

浅浅淡淡 2024-08-21 17:57:42

添加新的远程存储库,最后一步将在推送时排除 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/{}

Add your new remote repo and the last step will exclude the HEAD branch when you push

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/{}
爱格式化 2024-08-21 17:57:42

首先将远程 git 添加到您的 loacl 中

git remote add remote_name remote_address

,然后您只需使用以下命令即可完成此操作

git push --all remote_name

first add the remote git to your loacl with

git remote add remote_name remote_address

and after you just need to do it with the following command

git push --all remote_name
两个我 2024-08-21 17:57:42

我从关闭的服务器上进行了备份。当我想将所有分支发送到新服务器时,我找不到任何命令。当我检查 git Branch -a 时,有remotes/origin/master 条目。我从代码更改中意识到这些分支存在于我的语言环境中。通过 git checkout,我可以推送通过的每个分支。为了解决这个不必要的 git 命令复杂性,我用 gpt 编写了以下脚本。

列出所有分支并推送

for branch in $(git branch -r | grep -v '\->' | sed 's/ *origin\///'); do
    git checkout -B $branch origin/$branch
    git push origin $branch
done

Push 标签

git push --tags origin

I took a backup from a server that was shut down. When I wanted to send all branches to the new server, I could not find a single command for this. When I checked with git branch -a, there were remotes/origin/master entries. I realized from the code changes that these branches existed in my locale. With git checkout, I could push each branch that was passed. To solve this unnecessary git command complexity, I wrote the following script with gpt.

List all branches and push

for branch in $(git branch -r | grep -v '\->' | sed 's/ *origin\///'); do
    git checkout -B $branch origin/$branch
    git push origin $branch
done

Push tags

git push --tags origin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文