设置git拉取和推送所有分支
我想默认情况下推拉所有分支,包括新创建的分支。
我可以为其定义一个设置吗?
否则,当我在本地添加一个新分支并且我想从服务器中提取它时,最简单的方法是什么?
我创建了一个同名的新分支并尝试拉取,但它不起作用。要求我提供分支的所有远程配置。我该如何设置呢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
最简单的方法是:
这将推送标签和分支。
The simplest way is to do:
This will push tags and branches.
使用现代 git,您始终获取所有分支(作为远程跟踪分支到
refs/remotes/origin/*
命名空间,通过gitbranch -r
可见> 或git Remote show origin
)。默认情况下(请参阅
push.default
配置变量的文档),您推送匹配分支,这意味着您首先必须执行git推送原始分支 git 始终将其推送到 git Push 上。
如果您想始终推送所有分支,您可以设置推送引用规范。假设远程名称为
origin
,您可以使用 git config:或者直接编辑
.git/config
文件,使其内容如下:With modern git you always fetch all branches (as remote-tracking branches into
refs/remotes/origin/*
namespace, visible withgit branch -r
orgit remote show origin
).By default (see documentation of
push.default
config variable) you push matching branches, which means that first you have to dogit push origin branch
for git to push it always ongit 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:or directly edit
.git/config
file to have something like the following:我使用以下命令将所有分支迁移到新存储库。
注意:在从 Atlassian 克隆存储库时,我必须使用倒数第二个(即先推送 master)命令 存储 到 AWS CodeCommit(空白回购)。我不确定原因,但在推送(
git push new-origin --mirror
)后,默认分支引用了master
之外的其他分支。I had used below commands to migrate all branches to the new repository.
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 thanmaster
.在推送规范中包含 + 可能是一个坏主意,因为这意味着 git 会很乐意执行非快进推送即使没有 -f,并且如果远程服务器设置为接受那些,你可能会失去历史。
试试这个:
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 命令配置存储库。
重要的一点是,其他响应仅推送所有本地分支。如果分支仅存在于备用远程存储库上,则在不首先跟踪它们的情况下它们不会移动。这里介绍的 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.
Then add your new remote repo:
Then you can push all using this command:
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.
如果您要将所有分支从旧分支移动到新存储库,那么在本地存储库中,您需要在推送到新存储库之前设置每个分支到现有原始分支的跟踪,否则所有原始分支不会出现在新的原点中。通过跟踪或检查每个分支来手动执行此操作,或使用单行命令:
这一行命令基于本页其他答案中的版本,但可以说更好,因为:
,如果要切换源,请替换旧源的链接并指向新的远程。确保首先使用 bitbucket/github GUI 创建新的远程,但不要向其中添加任何文件,否则会出现合并问题。例如
现在推。请注意,还需要第二个命令来推送标签:
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:
This one line command is based on versions of it in other answers on this page, but is arguably better because:
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.
Now push. Note the second command is needed to push the tags as well:
我找到了最好和最简单的方法这里,就像@kumarahul 发布,对我来说就像一个魅力,它将所有标签和分支从原点推送到新的远程:
我使用了“git push --all -u newremote”,但它只将签出的分支推送到新的远程。
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:
I used 'git push --all -u newremote', but it only push the checkouted branches to the newremote.
要在不使用 gitbranch -a 的情况下查看所有分支,您应该执行:
现在您可以看到所有分支:
要推送所有分支,请尝试:
To see all the branches with out using
git branch -a
you should execute:Now you can see all the branches:
To push all the branches try:
对我来说传输 ALL 分支和标签的完整过程是,结合 @vikas027 和 @kumarahul 的答案:
最后一步是因为名为
HEAD
的分支出现在由于通配符而导致的新遥控器The full procedure that worked for me to transfer ALL branches and tags is, combining the answers of @vikas027 and @kumarahul:
The last step is because a branch named
HEAD
appears in the new remote due to the wildcard如果您要从一个远程源推送到另一个远程源,您可以使用以下命令:
这对我有用。请参阅:https://www.metaltoad.com/博客/git-push-all-branches-new-remote
If you are pushing from one remote origin to another, you can use this:
This worked for me. Reffer to this: https://www.metaltoad.com/blog/git-push-all-branches-new-remote
2012 年答案 提到
git push --all origin
为“这将推送标签和分支。 ”嗯...不:它将 仅推送所有分支(即
refs/heads/
下的参考文献,不是在refs/tags/
下的参考文献)这就是为什么在2023年, Git 2.41,“设置 git 拉动和推送所有分支”的答案是:
因为使用 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 日)git Push
现在包含在其 手册页: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 underrefs/tags/
)That is why in 2023, with Git 2.41, the answer to "Set up git to pull and push all branches" would be:
Because with Git 2.41 (Q2 2023), "
git push --all
"(man) gained an aliasgit 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)git push
now includes in its man page:git push
now includes in its man page:在配置中不硬编码
origin
的解决方案在global gitconfig中使用以下内容
这会推送所有分支和所有标签
为什么你不应该在配置中硬编码
origin
?如果您进行硬编码:
origin
作为远程。因此您将无法添加来源,但您需要使用set-url
。origin
已经存在(从第 1 点开始)记住:)现代 git 已经处理了提取
按照 Jakub Narębski 的回答:
Solution without hardcoding
origin
in configUse the following in your global gitconfig
This pushes all branches and all tags
Why should you NOT hardcode
origin
in config?If you hardcode:
origin
as a remote in all repos. So you'll not be able to add origin, but you need to useset-url
.origin
already exists (from point 1) remember :)Fetching is taken care of already by modern git
As per Jakub Narębski's answer:
添加新的远程存储库,最后一步将在推送时排除 HEAD 分支
Add your new remote repo and the last step will exclude the HEAD branch when you push
首先将远程 git 添加到您的 loacl 中
,然后您只需使用以下命令即可完成此操作
first add the remote git to your loacl with
and after you just need to do it with the following command
我从关闭的服务器上进行了备份。当我想将所有分支发送到新服务器时,我找不到任何命令。当我检查 git Branch -a 时,有remotes/origin/master 条目。我从代码更改中意识到这些分支存在于我的语言环境中。通过 git checkout,我可以推送通过的每个分支。为了解决这个不必要的 git 命令复杂性,我用 gpt 编写了以下脚本。
列出所有分支并推送
Push 标签
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
Push tags