git:一个快速命令去工作树的根

标签 git bash scripting

我想要一个简单的 git 命令去到存储库的“根”。

我从一个脚本开始,但发现我无法更改 shell 的事件目录,我必须执行一个函数。不幸的是,例如,我无法使用非破折号形式“git root”直接调用它。

function git-root() {
 if [ -d .git ]; then
  return 0
 fi

 A=..
 while ! [ -d $A/.git ]; do 
  A="$A/.."
 done
 cd $A
}

你有更好的解决方案吗? (功能写的很快,欢迎指教)

最佳答案

这已经被问过了,Is there a way to get the git root directory in one command?复制@docgnome 的回答,他写道

cd $(git rev-parse --show-cdup)

如果你愿意,可以做一个别名:

alias git-root='cd $(git rev-parse --show-cdup)'

关于git:一个快速命令去工作树的根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1571461/

相关文章:

文件名中带空格的 Linux 脚本

linux - for循环与来自变量的bash

Git 瓷器命令将单个文件恢复到其 HEAD 状态但保持其暂存状态?

git - 我可以不安装就使用git吗?

git - 推送到 master 时出错

regex - 赛德。如何删除与其周围的模式和字符串匹配的行?

html - 用于文件列表的 Bash 脚本(html 输出)

Github - 对组织中团队的存储库没有写入权限

c - system() 不断返回 127

linux - 用于下载远程文件并启动的 shell 脚本以及用 PHP 编写的其他脚本