[英]git clone and cd into it
如果我想在一行中創建一個目錄並將目錄更改為所有目錄,我可以執行以下操作:
mkdir dir_name && cd $_
我怎樣才能對git clone
做同樣的事情?
命令git clone repo_url && cd $_
顯然不起作用,因為沒有像repo_url
這樣的目錄。 但是有可能在一行中完成嗎?
如果您想自動查找名稱,您可以嘗試以下操作:
git clone http://repo_url.git && cd "$(basename "$_" .git)"
這樣您就不必為git
指定文件夾名稱。
您可以為 git clone 命令添加目錄名稱:
git clone repo_url my_repo_dirname && cd "$_"
將以下內容添加到您的~/.bashrc
。 別忘了來源!
function gccd { git clone "$1" && cd "$(basename $1)"; }
export -f gccd
gccd 代表 git clone 和 cd。 這是別名的等效功能。 現在您可以輸入: gccd <repo>
。 它會做你想要的。
您可以使用以下
git clone http://repo_url.git && cd "!$:t:r"
Imp:不要忘記cd
命令中的雙引號,否則它在其他一些 shell 或 Windows 中的 Git Bash 中不起作用。
它是如何工作的?
第一個命令是顯而易見的git clone
命令。 第二個cd
命令很有趣。
現在有一種稱為命令歷史的字指示符
!$
是最后一個命令運行的最后一部分這里最后運行的命令是git clone http://repo_url.git
。 該命令由三部分組成。 1. git
, 2. clone
和 3. http://repo_url.git
。 而http://repo_url.git
是最后一部分。 因此!$
==> http://repo_url.git
然后有一個叫做Word Modifiers的東西,它修改它前面的字符串。
:t
刪除所有前導文件名組件,留下尾部所以這里!$:t
會被讀成(!$):t
。 因此!$:t
==> repo_url.git
:r
從abcd.xyz
等文件名中刪除尾隨后綴,留下abcd
所以這里!$:t:r
會被讀成{(!$):t}:r
。 因此!$:t:r
==> repo_url
所以它會cd
到repo_url
要自己調試,請使用:p
,它只打印前面的命令而不執行它。 相當於echo
按確切順序運行以下命令
git clone http://repo_url.git
!$:p
==> http://repo_url.git
(或echo !$
)!$:t:p
==> repo_url.git
(或echo !$:t
)!$:t:r:p
==> repo_url
(或echo !$:t:r
)重擊功能:
m.git.clone(){
local url="${1}" dir="${2}"
if [[ "${dir}" == "" ]]; then
dir="$(basename "${url}" .git)"
fi
git clone "${url}" "${dir}" && cd "${dir}"
}
可以使用:
m.git.clone git@github.com:user/path.to.git
或者
m.git.clone git@github.com:user/path.to.git customDir
僅在成功時才可以工作和 cd。
我不太喜歡cdls
或gitclone
等具有特殊名稱的別名,因為在新系統上我不記得原來的命令了。 這就是為什么我喜歡以下別名解決方案的原因:
## git extra features
# - clone + cd
function git() {
if [ $1 = "clone" ]
then
command git $@ && cd "$(basename "$_" .git)"
fi
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.