我曾在一個類似的問題,我想使我的~/.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
所以我得到,沒有辦法使用'config'文件中的環境變量?我一定會給這個腳本一個嘗試,我希望它能在Git中運行在windows – Anthony 2014-12-02 13:11:53
之上,據我所知,不,你不能在配置文件中設置env var,因爲它具有相對於你工作目錄的絕對路徑,這張貼是另一個可行的解決方案,我希望它無論如何都適合您的需求! – DRC 2014-12-02 13:26:52
被Git 2.5中添加的git-worktree命令所取代。見http://nuclearsquid.com/writings/git-new-workdir/ – amynbe 2016-04-27 12:09:54