Git 创建分支别名并推送它

Git 创建分支别名并推送它,git,github,version-control,Git,Github,Version Control,如何创建一个分支别名,使其可以被推拉而不被视为单独的分支。 一些背景: 我们有一家名为生产部的分公司。由于各种原因,我不能只重命名分支。 $ git branch --all * master remotes/origin/HEAD -> origin/master remotes/origin/Production remotes/origin/master 有时我会错误地git checkout production,而没有注意到这是一个新分支。没有远程分支生产。也没有r

如何创建一个分支别名,使其可以被推拉而不被视为单独的分支。 一些背景:

我们有一家名为生产部的分公司。由于各种原因,我不能只重命名分支。

$ git branch --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/Production
  remotes/origin/master
有时我会错误地
git checkout production
,而没有注意到这是一个新分支。没有远程分支
生产
。也没有ref
产地/产地

$ git checkout production
Branch production set up to track remote branch production from origin.
Switched to a new branch 'production'
$ git status
On branch production
Your branch is up-to-date with 'origin/production'.

nothing to commit, working directory clean
$ git branch --all
  master
* production
  remotes/origin/HEAD -> origin/master
  remotes/origin/Production
  remotes/origin/master
Git创建了一个本地分支
生产
,并声称正在跟踪远程分支
生产
。现在,如果我提交并推送或只是推送,git将推送一个新分支。不是我想要的

$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:path/repo.git
 * [new branch]      production -> production
$ git push
Everything up-to-date
然后我必须后退一步,扭转局面

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git branch -d production
warning: deleting branch 'production' that has been merged to
         'refs/remotes/origin/production', but not yet merged to HEAD.
Deleted branch production (was bc6d7a2).
$ git push origin :production
To git@github.com:path/repo.git
 - [deleted]         production
$ git checkout Production
error: pathspec 'Production' did not match any file(s) known to git.
不知何故,现在我已经失去了
制作
,直到我再次拉它。git是否区分大小写

$ git branch --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git pull
From github.com:path/repo
 * [new branch]      Production -> origin/Production
Already up-to-date.
$ git checkout Production
Branch Production set up to track remote branch Production from origin.
Switched to a new branch 'Production'
Your branch is up-to-date with 'origin/Production'.
现在推也不会推新的分支。事实上我想要什么

$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:path/repo.git
 * [new branch]      production -> production
$ git push
Everything up-to-date
我的第一个想法是为
Production
分支创建一个别名,名为
Production
,但我希望它能够被推拉,就好像这两个分支实际上是一样的。这是我不知道该怎么做的事。其他问题中的所有别名方法似乎都只是本地别名

如果你对我的问题有不同的解决办法,我也想听听


作为补充说明,如果您能告诉我为什么git有时区分大小写,有时不区分大小写,那么您将获得额外的分数。

OP在评论a中提到

这确实在本地解决了问题,并且比下面提到的挂钩更简单。
这些挂钩可以确保两个分支保持同步

在这两种情况下,都需要对所有团队成员进行本地配置,尽管最后我提到了“templatedir”,以自动化钩子传播


,您可以创建一个
post commit
钩子,该钩子将确保本地分支
production
的提交设置为与
production
分支相同的提交。(或者,如果您在
生产
分支上提交,
生产
被设置为与
生产
分支相同的提交)
这样,两个分支总是引用同一个提交,您可以从其中任何一个进行推送

(git 1.8.2+)可以:

  • 检查推送哪个分支(生产或生产)
  • 触发对另一个分支的推送
这样,即使将
git config push.default
设置为
simple
(意味着您只推当前分支),即使推错了一个(
production
=>
origin/production
),预推钩子也可以推另一个分支(
生产
=>
产地/生产

由于两个分支都被推送,因此拉送操作将始终至少更新其中一个分支。
post-commit钩子将确保另一个钩子能够跟上

我们的目标是不再关心您正在处理哪个分支(
Production
Production



您可以在中添加这些钩子,这将允许任何团队成员在其新克隆的repo中获得这些钩子,前提是他们的
init.templatedir
全局配置引用了该共享文件夹。

作为未来读者的备选方案,我考虑创建一个
产品
,并放置一个文件,以便在他走错了路

git checkout --orphan production
touch wrong-branch
这将是可移植的,但不会使
production
分支像我真正想要的那样跨克隆执行
production
分支(而且似乎不可能)


如果(出于未知原因)有人在live server上错误地签出了git产品,这可能会导致live server停机。

你的操作系统是什么?你的git版本是什么?Mac OSX 10.9.4,
git版本1.8.5.2(Apple git-48)
,不过,如果可能的话,我希望它可以移植到Windows。谢谢。这是一个非常有创意的解决方案。这在本地解决了我的问题,但是a也会在本地解决问题。它看起来像git挂钩,因此如果没有每个成员的手动工作,它无法解决整个团队的问题。在我的赏金描述中,我忘记了提到可移植性问题,所以如果我在赏金到期之前没有得到答案,我还是会给你。中git分支别名的命令是
git-symbolic-ref-refs/heads/trunk-refs/heads/master
,所以我想我可以做
git-symbolic-ref-refs/heads/production-refs/heads/production
“我没有试过,所以我不确定它是否比hook@VonC更有效。@Nate看起来比hook更简单(我在答案中引用了它)。我编辑了答案,提出了一种钩子定义相互化的方法。听起来似乎没有简单、可移植的方法来实现我想要的工作方式。我想我有我的答案。谢谢@VonC的帮助。