2014-12-02 116 views
4

我有一個使用--serparate-git-dir選項創建的git repo。我經常通過指定--git-dir--work-tree作爲參數來使用不同的工作樹的相同回購表格。.git/config中的環境變量

我有兩個工作樹,我經常切換,所以我在指向存儲庫目錄的輔助工作樹中添加了一個.git文件。但是,由於存儲庫的config文件指向主工作樹,我仍然必須明確指定它,否則它將使用主工作樹。

我試過的int .git/config文件worktree值設置爲$PWD但這會導致以下錯誤: fatal: Could not chdir to '$PWD': No such file or directory

有沒有一種方法,使worktree動態?

回答

1

編輯05-2016 作爲@amynbe意見,混帳> = 2.5已經混帳worktree https://git-scm.com/docs/git-worktree


我有一個腳本,有人在freenode傳來傳去,我不知道的筆者但我知道我可以分享,它是用來創建基於分支不同的工作copyes,我認爲它可能適合你的使用情況:

#!/bin/sh 

usage() { 
     echo "usage:" [email protected] 
     exit 127 
} 

die() { 
     echo [email protected] 
     exit 128 
} 

if test $# -lt 2 || test $# -gt 3 
then 
     usage "$0 <repository> <new_workdir> [<branch>]" 
fi 

orig_git=$1 
new_workdir=$2 
branch=$3 

# want to make sure that what is pointed to has a .git directory ... 
git_dir=$(cd "$orig_git" 2>/dev/null && 
    git rev-parse --git-dir 2>/dev/null) || 
    die "Not a git repository: \"$orig_git\"" 

case "$git_dir" in 
.git) 
     git_dir="$orig_git/.git" 
     ;; 
.) 
     git_dir=$orig_git 
     ;; 
esac 

# don't link to a configured bare repository 
isbare=$(git --git-dir="$git_dir" config --bool --get core.bare) 
if test ztrue = z$isbare 
then 
     die "\"$git_dir\" has core.bare set to true," \ 
       " remove from \"$git_dir/config\" to use $0" 
fi 

# don't link to a workdir 
if test -h "$git_dir/config" 
then 
     die "\"$orig_git\" is a working directory only, please specify" \ 
       "a complete repository." 
fi 

# don't recreate a workdir over an existing repository 
if test -e "$new_workdir" 
then 
     die "destination directory '$new_workdir' already exists." 
fi 

# make sure the links use full paths 
git_dir=$(cd "$git_dir"; pwd) 

# create the workdir 
mkdir -p "$new_workdir/.git" || die "unable to create \"$new_workdir\"!" 

# create the links to the original repo. explicitly exclude index, HEAD and 
# logs/HEAD from the list since they are purely related to the current working 
# directory, and should not be shared. 
for x in config refs logs/refs objects info hooks packed-refs remotes rr-cache svn 
do 
     case $x in 
     */*) 
       mkdir -p "$(dirname "$new_workdir/.git/$x")" 
       ;; 
     esac 
     ln -s "$git_dir/$x" "$new_workdir/.git/$x" 
done 

# now setup the workdir 
cd "$new_workdir" 
# copy the HEAD from the original repository as a default branch 
cp "$git_dir/HEAD" .git/HEAD 
# checkout the branch (either the same as HEAD from the original repository, or 
# the one that was asked for) 
git checkout -f $branch 
+0

所以我得到,沒有辦法使用'config'文件中的環境變量?我一定會給這個腳本一個嘗試,我希望它能在Git中運行在windows – Anthony 2014-12-02 13:11:53

+0

之上,據我所知,不,你不能在配置文件中設置env var,因爲它具有相對於你工作目錄的絕對路徑,這張貼是另一個可行的解決方案,我希望它無論如何都適合您的需求! – DRC 2014-12-02 13:26:52

+0

被Git 2.5中添加的git-worktree命令所取代。見http://nuclearsquid.com/writings/git-new-workdir/ – amynbe 2016-04-27 12:09:54

1

我曾在一個類似的問題,我想使我的~/.gitconfig可以跨平臺移植,這樣我就可以在我的Macbook和Linux VM上使用相同的gitconfig。我需要credential.helper根據平臺而有所不同。

我最終編寫了一個generate_gitconfig腳本,並將其添加到我的bashrc中,以便在啓動每個shell會話時自動生成~/.gitconfig

編寫您gitconfig通過腳本給你,你可以動態地根據環境變量的值更大的靈活性,是否安裝了某些命令,該機器的主機名等

這裏是我generate_gitconfig腳本,作爲一個例子:

#!/bin/bash 

cat <<EOF > $HOME/.gitconfig 
# This gitconfig was generated via ~/.bin/generate_gitconfig. 
# Edit that file, not this one! 

[user] 
    name = Dave Yarwood 
    email = [email protected] 
[push] 
    default = simple 
[core] 
    autocrlf = input 
    editor = nvim 
    excludesfile = $HOME/.gitignore_global 
[rerere] 
    enabled = true 
EOF 

if [[ "$(uname)" == Darwin ]]; then 
    CREDENTIAL_HELPER="osxkeychain" 
elif [[ -n "$(which gnome-keyring-daemon)" ]]; then 
    CREDENTIAL_HELPER="/usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring" 
fi 

if [[ -n "$CREDENTIAL_HELPER" ]]; then 
    cat <<EOF >> $HOME/.gitconfig 
[credential] 
    helper = $CREDENTIAL_HELPER 
EOF 
fi