Mercurial有一种打印根目录(包含.hg)的方法
hg root
git中是否有等价的东西来获取包含.git目录的目录?
Mercurial有一种打印根目录(包含.hg)的方法
hg root
git中是否有等价的东西来获取包含.git目录的目录?
Yes:
git rev-parse --show-toplevel
如果你想更直接地复制Mercurial命令,你可以创建一个别名:
git config --global alias.root 'rev-parse --show-toplevel'
现在git根和hg根一样。
注意:在子模块中,这将显示子模块的根目录,而不是父存储库。如果您使用的是Git >=2.13或更高版本,有一种方法可以让子模块显示超级项目的根目录。如果你的git比这更老,请参阅另一个答案。
“git rev-parse——git-dir”怎么样?
F:\prog\git\test\copyMerge\dirWithConflicts>git rev-parse --git-dir
F:/prog/git/test/copyMerge/.git
——git-dir选项似乎可以工作。
它甚至可以在裸库中工作,而git rev-parse——show- topllevel会触发(在裸库中)“致命:此操作必须在工作树中运行”。
从git rev-parse手册页:
--git-dir
Show $GIT_DIR if defined else show the path to the .git directory.
您可以在这个git setup-sh脚本中看到它的作用。
如果你在子模块文件夹中,Git >=2.13,使用:
git rev-parse --show-superproject-working-tree
如果你正在使用git rev-parse——show-toplevel,请确保使用的是git 2.25+ (Q1 2020)。
不管你是否已经在git仓库的顶层,cd $(git rev-parse——show-cdup)将带你回家(只是cd)。CD ./$(git rev-parse——show-cdup)是解决这个问题的一种方法。
git-config的手册页(在Alias下)说:
如果别名展开以感叹号作为前缀,则它将被视为外壳 命令。[…注意shell 命令将从存储库的顶级目录执行,这可能不是 必须是当前目录。
在UNIX上,你可以这样做:
git config --global --add alias.root '!pwd'
要计算当前git根目录的绝对路径,比如在shell脚本中使用,使用readlink和git rev-parse的组合:
gitroot=$(readlink -f ./$(git rev-parse --show-cdup))
Git-rev-parse——show-cdup给出正确的“..”’s to get 从你的cwd到根目录,如果你在根目录,则返回空字符串。 然后“prepend”。/"来处理空字符串大小写并使用 Readlink -f转换为完整路径。
你也可以在PATH中创建一个git-root命令作为shell脚本来应用这个技术:
cat > ~/bin/git-root << EOF
#!/bin/sh -e
cdup=$(git rev-parse --show-cdup)
exec readlink -f ./$cdup
EOF
chmod 755 ~/bin/git-root
(上面的代码可以粘贴到终端中创建git-root并设置执行位;实际的脚本在第2、3和4行。)
然后你可以运行git root来获得当前树的根。 注意,在shell脚本中,使用“-e”使shell在rev-parse失败时退出,这样如果不在git目录中,就可以正确地获得退出状态和错误消息。
今天必须自己解决这个问题。在c#中解决了它,因为我需要它的程序,但我猜它可以轻松重写。考虑到这是公共领域。
public static string GetGitRoot (string file_path) {
file_path = System.IO.Path.GetDirectoryName (file_path);
while (file_path != null) {
if (Directory.Exists (System.IO.Path.Combine (file_path, ".git")))
return file_path;
file_path = Directory.GetParent (file_path).FullName;
}
return null;
}
正如其他人所注意到的,解决方案的核心是使用git rev-parse——show-cdup。然而,也有一些边缘情况需要解决:
When the cwd already is the root of the working tree, the command yields an empty string. Actually it produces an empty line, but command substitution strip off the trailing line break. The final result is an empty string. Most answers suggest prepending the output with ./ so that an empty output becomes "./" before it is fed to cd. When GIT_WORK_TREE is set to a location that is not the parent of the cwd, the output may be an absolute pathname. Prepending ./ is wrong in this situation. If a ./ is prepended to an absolute path, it becomes a relative path (and they only refer to the same location if the cwd is the root directory of the system). The output may contain whitespace. This really only applies in the second case, but it has an easy fix: use double quotes around the command substitution (and any subsequent uses of the value).
正如其他答案所指出的,我们可以做cd”。/$(git rev-parse——show-cdup))”,但这会在第二个边缘大小写中中断(如果去掉双引号,则会在第三个边缘大小写中中断)。
许多shell将cd ""视为无操作,因此对于这些shell,我们可以执行cd "$(git rev-parse——show-cdup)"(双引号保护第一个边大小写中的空字符串作为参数,并在第三个边大小写中保留空白)。POSIX说cd ""的结果是未指定的,所以最好避免做这种假设。
在上述所有情况下工作的解决方案需要某种类型的测试。显式完成后,它可能是这样的:
cdup="$(git rev-parse --show-cdup)" && test -n "$cdup" && cd "$cdup"
第一个边不做cd操作。
如果运行cd可以接受。对于第一个边情况,则条件可以在参数展开中执行:
cdup="$(git rev-parse --show-cdup)" && cd "${cdup:-.}"
——show- topllevel是最近才添加到git rev-parse的吗?为什么没有人提到它?
从git rev-parse手册页:
--show-toplevel
Show the absolute path of the top-level directory.
alias git-root='cd \`git rev-parse --git-dir\`; cd ..'
其他所有东西都会在某一时刻失败要么是到主目录,要么就是悲惨地失败。这是返回GIT_DIR的最快和最短的方法。
如果你正在寻找一个好的别名来执行此操作,如果你不在git目录下,则不要放大cd:
alias ..g='git rev-parse && cd "$(git rev-parse --show-cdup)"'
下面是我编写的处理这两种情况的脚本:1)带工作区的存储库,2)裸存储库。
https://gist.github.com/jdsumsion/6282953
Git-root(可执行文件在你的路径):
#!/bin/bash
GIT_DIR=`git rev-parse --git-dir` &&
(
if [ `basename $GIT_DIR` = ".git" ]; then
# handle normal git repos (with a .git dir)
cd $GIT_DIR/..
else
# handle bare git repos (the repo IS a xxx.git dir)
cd $GIT_DIR
fi
pwd
)
希望这对你们有帮助。
不管你是在git子目录下,还是在顶层,这个shell别名都有效:
alias gr='[ ! -z `git rev-parse --show-toplevel` ] && cd `git rev-parse --show-toplevel || pwd`'
更新为使用现代语法而不是反撇号:
alias gr='[ ! -z $(git rev-parse --show-toplevel) ] && cd $(git rev-parse --show-toplevel || pwd)'
在这里写一个简单的答案,这样我们就可以用
git root
要完成这项工作,只需使用
git config --global alias.root "rev-parse --show-toplevel"
然后你可能想在~/.bashrc中添加以下内容:
alias cdroot='cd $(git root)'
这样你就可以用croot到回购的顶部。
以防万一,如果您要将此路径提供给Git本身,请使用:/
# this adds the whole working tree from any directory in the repo
git add :/
# and is equal to
git add $(git rev-parse --show-toplevel)
git-extras
添加$ git根目录 看到https://github.com/tj/git-extras/blob/master/Commands.md git-root
$ pwd
.../very-deep-from-root-directory
$ cd `git root`
$ git add . && git commit
git-extra的可用性
Homebrew (osx)/linuxbrew(linux) $ brew安装git-extras Debian /ubuntu repos (https://packages.debian.org/sid/git-extras) $ apt-get安装git-extras
在钩子和.git目录中使用子模块的简短解决方案
这是大多数人想要的简短答案:
r=$(git rev-parse --git-dir) && r=$(cd "$r" && pwd)/ && echo "${r%%/.git/*}"
这将在git工作树中的任何地方工作(包括在.git目录中),但假设存储库目录被称为.git(这是默认的)。对于子模块,这将转到包含最外层存储库的根。
如果你想获得当前子模块的根,使用:
echo $(r=$(git rev-parse --show-toplevel) && ([[ -n $r ]] && echo "$r" || (cd $(git rev-parse --git-dir)/.. && pwd) ))
为了在你的根模块中轻松地执行一个命令,在你的.gitconfig中的[alias]下,添加:
sh = "!f() { root=$(pwd)/ && cd ${root%%/.git/*} && git rev-parse && exec \"$@\"; }; f"
这允许您轻松地执行git sh ag <string>之类的操作
健壮的解决方案,支持不同命名或外部。git或$GIT_DIR目录。
注意,$GIT_DIR可能指向外部的某个地方(而不是被称为.git),因此需要进一步检查。
把这个放在你的。bashrc中:
# Print the name of the git working tree's root directory
function git_root() {
local root first_commit
# git displays its own error if not in a repository
root=$(git rev-parse --show-toplevel) || return
if [[ -n $root ]]; then
echo $root
return
elif [[ $(git rev-parse --is-inside-git-dir) = true ]]; then
# We're inside the .git directory
# Store the commit id of the first commit to compare later
# It's possible that $GIT_DIR points somewhere not inside the repo
first_commit=$(git rev-list --parents HEAD | tail -1) ||
echo "$0: Can't get initial commit" 2>&1 && false && return
root=$(git rev-parse --git-dir)/.. &&
# subshell so we don't change the user's working directory
( cd "$root" &&
if [[ $(git rev-list --parents HEAD | tail -1) = $first_commit ]]; then
pwd
else
echo "$FUNCNAME: git directory is not inside its repository" 2>&1
false
fi
)
else
echo "$FUNCNAME: Can't determine repository root" 2>&1
false
fi
}
# Change working directory to git repository root
function cd_git_root() {
local root
root=$(git_root) || return 1 # git_root will print any errors
cd "$root"
}
通过输入git_root来执行它(在重新启动shell后:exec bash)
在Shell框架中预先配置的Shell别名
如果你使用shell框架,可能已经有一个可用的shell别名:
$ GRT in oh-my-zsh (68k) (cd $(git rev-parse——show-toplevel || echo ".")) $ git-root in prezto (8.8k)(显示工作树根的路径) $ g . .Zimfw (1k)(将当前目录更改为工作树的顶层。)
从Git 2.13.0开始,它支持一个新的选项来显示根项目的路径,即使在子模块内部使用也可以工作:
git rev-parse --show-superproject-working-tree
如果有人需要POSIX兼容的方式来做到这一点,而不需要git可执行文件:
git-root:
#$1: Path to child directory
git_root_recurse_parent() {
# Check if cwd is a git root directory
if [ -d .git/objects -a -d .git/refs -a -f .git/HEAD ] ; then
pwd
return 0
fi
# Check if recursion should end (typically if cwd is /)
if [ "${1}" = "$(pwd)" ] ; then
return 1
fi
# Check parent directory in the same way
local cwd=$(pwd)
cd ..
git_root_recurse_parent "${cwd}"
}
git_root_recurse_parent
如果你只是想把这个功能作为脚本的一部分,删除shebang,并把最后一行git_root_recurse_parent替换为:
git_root() {
(git_root_recurse_parent)
}
我想进一步阐述丹尼尔·布罗克曼的精彩评论。
定义git配置全局别名。exec”!Exec '允许你做像git Exec make这样的事情,因为man git-config说:
如果别名展开以感叹号作为前缀,则它将被视为shell命令。[…注意,shell命令将从存储库的顶级目录执行,而不一定是当前目录。
知道$GIT_PREFIX将是相对于存储库的顶级目录的当前目录的路径也很方便。但是,知道这只是战斗的一半。Shell变量展开使得它很难使用。所以我建议像这样使用bash -c:
git exec bash -c 'ls -l $GIT_PREFIX'
其他命令包括:
git exec pwd
git exec make