创建分支别名并推送

Nat*_*ate 5 git version-control github

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

我们有一个名为Production. 由于各种原因,我不能只是重命名分支。

$ git branch --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/Production
  remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

有时我会git checkout production错误地没有注意到它是一个新分支。没有远程分支production。也没有 ref origin/production

$ 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
Run Code Online (Sandbox Code Playgroud)

Git 创建了一个本地分支production并声称它正在跟踪远程分支production。现在,如果我提交并推送或只是推送 git 将推送一个新分支。不是我想要的。

$ git push
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:path/repo.git
 * [new branch]      production -> production
Run Code Online (Sandbox Code Playgroud)

然后我必须退后一步并扭转局面

$ 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.
Run Code Online (Sandbox Code Playgroud)

不知何故,现在我已经输了,Production直到我再次拉它。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'.
Run Code Online (Sandbox Code Playgroud)

并且推送现在不会推送新分支。其实我想要的。

$ git push
Everything up-to-date
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是创建一个Production被调用分支的别名,production但我希望它能够被推拉,就好像两者实际上是同一件事。这是我不知道该怎么做的事情。其他问题中的所有别名方法似乎都只是本地别名。

如果您对我的问题有不同的解决方案,我也想听听。

附带说明一下,如果您能告诉我为什么 git 有时区分大小写有时不区分大小写,则会加分。

Von*_*onC 3

OP 在评论中提到了分支别名

git symbolic-ref refs/heads/production refs/heads/Production
Run Code Online (Sandbox Code Playgroud)

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

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


使用 hooks,您可以创建一个post-commit钩子,以确保本地分支production设置为与Production分支相同的提交。(或者,如果您在production分支上提交,则将Production设置为与分支相同的提交production
这样,两个分支始终引用相同的提交,并且您可以从其中任何一个进行推送。

pre-pushhook (git 1.8.2+) 可以:

  • 检查哪个分支被推送(生产或生产)
  • 触发另一个分支的推送

这样,即使设置git config push.defaultsimple(意味着您只推送当前分支),即使您推送了错误的分支(production=> origin/production),pre-push钩子也可以负责推送另一个分支(Production=> origin/Production

由于两个分支都是推送的,因此拉取总是会更新至少其中一个分支。
提交后挂钩将确保另一个挂钩能够赶上。

目标是不再关心您正在哪个分支(Productionproduction)上工作。


您可以将这些挂钩添加到共享模板 Git 存储库文件夹中,这将允许任何团队成员在其新克隆的存储库中获取这些挂钩,前提是他们的init.templatedir全局配置引用该共享文件夹。


归档时间:

查看次数:

1608 次

最近记录:

11 年,6 月 前