[英]Git: Output the currently tracked branch name without the remote name on it
I'm working on a git alias to perform a git push --force
-like command in a repository where you can't push force, but you can delete remote branches. 我正在使用git别名在无法推送强制但可以删除远程分支的存储库中执行
git push --force
-like命令。 So which has this two steps: 因此,有以下两个步骤:
The script must work in the case of the local branch and the remote branch have different names. 该脚本必须在本地分支和远程分支具有不同名称的情况下工作。 There is only one remote (origin) so it can be hardcoded.
只有一个远程(来源),因此可以进行硬编码。 The alias is to be run in a bash shell only.
别名只能在bash shell中运行。
I'm getting stuck in getting the right upstream branch name. 我被困在获取正确的上游分支名称中。 I know that I can get the upstream tracked branch on git with the command:
我知道我可以使用以下命令在git上获得上游跟踪分支:
git rev-parse --abbrev-ref --symbolic-full-name @{upstream}
However that returns the name of the branch with the format <remote_name>/<branch_name>
eg: origin/my_working_branch 但是,它以
<remote_name>/<branch_name>
格式返回分支的名称,例如: origin / my_working_branch
and would make the push fail as it would issue the following commands: 并会使推送失败,因为它将发出以下命令:
# this fails as the branch origin/my_working_branch does not exist
git push origin :origin/my_working_branch
# this will create a branch named 'origin/my_working_branch' in the remote, which is not what I want
git push origin my_working_branch:origin/my_working_branch
Is there any way to make git rev-parse
to output the name of the branch without the remote/ part? 有什么方法可以使
git rev-parse
输出分支的名称而无需remote / part?
Is there any way to make
git rev-parse
to output the name of the branch without the remote/ part?有什么方法可以使
git rev-parse
输出分支的名称而无需remote / part?
No, but you don't need to. 不,但是您不需要。 You want to know what the name of the branch is on the upstream , and that's the second half of the two-part thing that
git rev-parse
works with. 您想知道分支的名称在上游 ,这就是
git rev-parse
使用的两部分的后半部分。
That is, for branch B , the upstream is set in two parts: branch. B .remote
也就是说,对于分支B ,上游设置为两部分:
branch. B .remote
branch. B .remote
is the name of the remote, and branch. B .merge
branch. B .remote
是远程和branch. B .merge
的名称branch. B .merge
branch. B .merge
is the name of the branch on the remote. branch. B .merge
是遥控器上的分支的名称。 So if my_working_branch
has origin/badbranch
as its upstream, and everything else is the way it normally is, then: 因此,如果
my_working_branch
将origin/badbranch
作为其上游, 并且其他所有操作都与往常一样,则:
git config --get branch.my_working_branch.remote
will output origin
, and: 将输出
origin
和:
git config --get branch.my_working_branch.merge
will output the string you want: refs/heads/badbranch
. 将输出您想要的字符串:
refs/heads/badbranch
。
Note that one or both of these may be un -set, in which case there is no defined upstream; 请注意,这两个或其中之一可能未设置,在这种情况下,上游没有定义。 and if
branch. B .remote
如果
branch. B .remote
branch. B .remote
is a literal dot .
branch. B .remote
是一个文字点.
character, the upstream is your own repository. 字符,上游是您自己的存储库。
(It's a good idea to use the fully-qualified refs/heads/ branch
format when deleting and re-creating a branch. Also, be sure whoever controls the upstream won't complain that you're sidestepping the anti-force provisions. :-) ) (删除和重新创建
refs/heads/ branch
时,使用完全合格的refs/heads/ branch
格式是一个好主意。此外,请确保控制上游的任何人都不会抱怨您回避了反强制条款。 -))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.