这里非常懒惰,不喜欢打太多字,特别是不喜欢记住我在哪个分支上 pull 和推送。我经常错误地从非主分支执行 git pull
,或者更糟的是执行 push 而目标分支不正确,我留下了未推送或未提交的更改。
我在 .bashrc
中创建了以下别名以提供帮助:
alias gitr='git status | grep -Po "On branch \K[^ ]+" | xargs git push origin'
因此在任何 git 仓库上执行 gitr
都会将已提交的更改推送到正确的分支。
同样在做:
alias gitp='git status | grep -Po "On branch \K[^ ]+" | xargs git pull origin'
将从我当前所在的正确分支中提取最新更改。
最后这个将所有本地更改提交为“Cosmetics”并将它们推送到正确的分支:
alias gitcr='git commit -a -m "Cosmetics" && git status | grep -Po "On branch \K[^ ]+" | xargs git push origin'
有没有办法打印所有中间命令而不用在每个命令中打印?有没有办法让这些命令更健壮?
UPDATE 通过打印中间命令我的意思是显示以下内容以确保它正在运行正确的命令:
git status
git pull origin somebranch
最佳答案
至少,with Git 2.22 ,你可以使用:
git branch --show-current
那会避免一些 grep/awk/sed
此外,如果您想要推送,您需要与当前分支相关联的远程跟踪分支的名称(您可以将远程“bar”分支与本地“foo”分支相关联'
即 [<branchname>]@{push}
, e.g. master@{push}, @{push}
git rev-parse --abbrev-ref --symbolic-full-name @{push}|cut -d / -f 2
又是后缀@{push}
如果 git push
,则报告分支“我们将推送到的地方”在 checkout branchname 时运行。
备注:git rev-parse --abbrev-ref --symbolic-full-name @{push}|cut -d / -f 1
会给你 Remote 的名字,不总是origin
.
它不与[<branchname>]@{upstream}
相同,例如master@{upstream}
, @{u}
.
后缀@{upstream}
到 branchname
(缩写 <branchname>@{u}
)是指由 branchname
指定的分支设置为构建在(配置为 branch.<name>.remote
和 branch.<name>.merge
)之上。
即您正在从中 pull 的分支。
Git 2.5+ @{push}
参见“Viewing Unpushed Git Commits”快捷符号。
Git 2.38(2022 年第 3 季度)阐明了 @{upstream}
捷径。
参见 commit 8cdab69 (2022 年 6 月 23 日)作者:Tao Klerks ( TaoK
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 9a13943,2022 年 7 月 13 日)
rev-parse
: documentation adjustment - mention remote tracking with@{u}
Signed-off-by: Tao Klerks
The documentation explained the conversion from remote branch path to local tracking ref path for
@{push}
, but not for@{upstream}
.Add the explanation to
@{upstream}
, and reference it in@{push}
to avoid undue repetition.
revisions
现在包含在其 man page 中:
A branch
B
may be set up to build on top of a branchX
(configured withbranch.<name>.merge
) at a remoteR
(configured withbranch.<name>.remote
).
B@{u}
refers to the remote-tracking branch for the branchX
taken from remoteR
, typically found atrefs/remotes/R/X
.
关于linux - 不必记住我要 pull 和推的分支的最佳别名是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56713349/