linux - 不必记住我要 pull 和推的分支的最佳别名是什么?

标签 linux bash git scripting

这里非常懒惰,不喜欢打太多字,特别是不喜欢记住我在哪个分支上 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>.remotebranch.<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 branch X (configured with branch.<name>.merge) at a remote R (configured with branch.<name>.remote).

B@{u} refers to the remote-tracking branch for the branch X taken from remote R, typically found at refs/remotes/R/X.

关于linux - 不必记住我要 pull 和推的分支的最佳别名是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56713349/

相关文章:

linux - 如果 shell 脚本中发生错误,通过电子邮件通知

linux - 在 Raspberry Pi 上永远安装 node.js/npm 的问题

linux - 在 Godaddy 的共享主机帐户上安装 beanstalkd

bash - 此命令只能由 root 使用

git - 当我使用 SSH 时,我使用 -i 开关,我是否也需要为我的 GIT 远程执行此操作?

linux - 黑客有可能通过 http 访问主目录吗?

linux - 执行命令而不将其保留在历史记录中

regex - 从终端中的命令输出中提取单行

Git:如何提交手动删除的文件?

linux - 从 GitHub pull 时的权限问题