Created
November 5, 2012 15:20
-
-
Save glenbot/4017733 to your computer and use it in GitHub Desktop.
Switch git context in shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Configuration files | |
TEMP_CONFIG="/tmp/gitswitchrc" | |
ALT_SSH_CONF="$HOME/.ssh/id_rsa_gitswitch" | |
BASHRC="$HOME/.bashrc" | |
# PS1 prompt pre-text | |
# Places some text behind your command prompt to give | |
# a context clue that you are within another GIT context. | |
# This must be configured in your .bashrc | |
# Example: | |
# export PS1="$PRE_PS1 $PS1" | |
PRE_PS1="[context-git]" | |
# Git Configuration | |
GIT_NAME="your_git_username" | |
GIT_EMAIL="[email protected]" | |
# Generate an ssh key for gitswitch if it doesn't exist | |
if [ ! -f "$ALT_SSH_CONF" ]; then | |
echo "Could not find public key for gitswitch creating one ..." | |
ssh-keygen -t rsa -f "$ALT_SSH_CONF" | |
fi | |
# Write a temporary bash config file that: | |
# | |
# * overrides the git environment variables | |
# * creates a new ssh-agent with the gitswitch key | |
# * prepends $PRE_PS1 to the PS1 prompt | |
# * adds a function called kill_ssh_agent and binds | |
# it to control-a that kills the ssh-agent and | |
# exits this prompt | |
# | |
cat <<EOF > $TEMP_CONFIG | |
eval `ssh-agent -s ` | |
ssh-add $ALT_SSH_CONF | |
source $BASHRC | |
export EMAIL="$GIT_EMAIL" | |
export GIT_COMMITTER_NAME="$GIT_NAME" | |
export GIT_COMMITTER_EMAIL="$GIT_EMAIL" | |
export GIT_AUTHOR_NAME="$GIT_NAME" | |
export GIT_AUTHOR_EMAIL="$GIT_EMAIL" | |
export PRE_PS1="$PRE_PS1 " | |
function kill_ssh_agent() { | |
echo "Exiting ALGIT" | |
kill -9 \$SSH_AGENT_PID | |
exit | |
} | |
bind -x '"\C-a": kill_ssh_agent' | |
EOF | |
exec bash --rcfile $TEMP_CONFIG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment