将新创建的Git分支发布到远程存储库可能比您预期的要容易。
介绍
在各种Git工作流程模型中,经常会出现创建新分支并将其推送(发布)到远程存储库的情况。 多数人创建了许多新分支。 仅用于初始化请求(合并)请求,向远程工作人员显示代码或仅在一夜之间备份本地更改。
不幸的是,它在Git中并不像它那么容易:
~ /C/my-fancy-project (master|✓) $ git checkout -b featureX Switched to a new branch 'featureX' ~ /C/my-fancy-project (featureX|✓) $ git push fatal: The current branch featureX has no upstream branch. To push the current branch and the remote as upstream, use set the remote as upstream, use
git push -- -upstream origin featureX set -upstream origin featureX
嗯,只需复制粘贴给定的行,就可以设置好:
~ /C/my-fancy-project (featureX|✓) $ git push -- -upstream origin featureX set (featureX|✓) $ git push -- -upstream origin featureX Total 0 (delta 0), reused 0 (delta 0) To /tmp/my-fancy-project-remote/
* [new branch] featureX -> featureX Branch 'featureX' set up to track remote branch from 'origin' up to track remote branch 'featureX' 'origin' .
当然,您可能会在一段时间后记住它(但是,我观察到很多人没有记住),甚至使用较短的语法:
~ /C/my-fancy-project (featureX|✓) $ git push -u origin featureX Total 0 (delta 0), reused 0 (delta 0) To /tmp/my-fancy-project-remote/
* [new branch] featureX -> featureX Branch 'featureX' set up to track remote branch from 'origin' up to track remote branch 'featureX' 'origin' .
但是,对我而言,要键入的字符很多,尤其是重复多次,尤其是在具有一个远程存储库(通常称为origin
)的典型工作流程中。
解
对我来说,完美的解决方案只是一个命令。 像git publish
一样的东西。
~ /C/my-fancy-project (master|✓) $ git checkout -b featureY Switched to a new branch 'featureY' ~ /C/my-fancy-project (featureY|✓) $ git publish Total 0 (delta 0), reused 0 (delta 0) To /tmp/my-fancy-project-remote/
* [new branch] featureY -> featureY Branch 'featureY' set up to track remote branch from 'origin' up to track remote branch 'featureY' 'origin' .
岂不是很好?
正如您从我以前的文章中可能知道的那样,我是全面自动化(例如CI / CD)或至少不可能(或可行)的半自动化(至少是“使事情变得容易”)的狂热爱好者。 因此,当时,我开始寻找可能的改进。 Git由开发人员为开发人员编写,并提供了不同的自定义方式。 最简单的是写一个别名。 在这种情况下,就像添加到~/.gitconfig
一样简单:
[ alias ]
# Pushes given tag to remote 'origin' repo (or the remote passed as the second parameter)
publishtag = "!sh -c 'git push ${2:-origin} $1' -" publishtag = "!sh -c 'git push ${2:-origin} $1' -"
因此,除了基本情况外(将上游分支设置为原点(如果需要),并将分支从当前分支推送到原点):
$ git publish
也可以发布到其他远程存储库:
$ git publish myOtherRemote
打扫干净
作为git publish
的副本,很容易实现git unpublish
:
[ alias ]
# Removes given tag from remote 'origin' repo (or the remote passed as the second parameter)
unpublishtag = "!sh -c 'git push ${2:-origin} :refs/tags/$1' -"
从远程存储库中删除当前分支( origin
或作为第二个参数传递):
~ /C/my-fancy-project (featureNoLongerNeeded|✓) $ git unpublish To /tmp/my-fancy-project-remote/
- [deleted] featureNoLongerNeeded
代替:
~ /C/my-fancy-project (featureNoLongerNeeded|✓) $ git push origin --delete featureNoLongerNeeded To /tmp/my-fancy-project-remote/
- [deleted] featureNoLongerNeeded
要么
~ /C/my-fancy-project (featureNoLongerNeeded|✓) $ git push origin :featureNoLongerNeeded To /tmp/my-fancy-project-remote/
- [deleted] featureNoLongerNeeded
再次,更短,更容易记住。
摘要
我已经使用git publish
(和git unpublish
)很多年了,我真的很喜欢它。 以写这个的机会的Git技巧的系列博文 ,我决定与他人分享(也爱上了在命令行:-))。 但是请记住,它现在已成为GitKurka (或其未经审查的上游项目 )的一部分-Git的一组有用且富有成效的调整和别名。
顺便说一句,我不再进行Git培训,但是想要进一步提高Git技能的人们可以考虑Bottega(PL / EN) 的现场 课程 ,Maciej Aniserowicz( devstyle.pl )(PL)的在线课程或一本全面的Pro Git书 (EN)。
根据Iva Balk的工作发布的主照片, Pixabay ,Pixabay许可。
翻译自: https://www.javacodegeeks.com/2019/09/convenient-pushing-git-branch-remote-repository.html