Thay đổi SSH key

Thêm cái này vào .bashrc hoặc .zshrc

function git-change() {
  # Enable ksh_arrays option for Zsh
  if [ "$ZSH_VERSION" ]; then
    setopt ksh_arrays
  fi

  # Collect all SSH keys in an array
  keys=()
  while IFS="" read -r line; do keys+=("$line"); done < <(
    find ~/.ssh/ -name "id_rsa" -o -name "id_rsa_*" \
    | sed 's@//@/@' \
    | awk '{ print length, $0 }' \
    | sort -n \
    | cut -d" " -f2-
  )

  # Grep keys if the 1st is not a number
  if [ "$1" ] && ! [[ "$1" =~ ^[0-9]+$ ]]; then
    grep_keys=()
    while IFS="" read -r line; do grep_keys+=("$line"); done < <(
      printf -- '%s\n' "${keys[@]}" \
      | grep "$1"
    )
    keys=("${grep_keys[@]}")
  fi

  # Check if any SSH keys were found
  if [ ${#keys[@]} -eq 0 ]; then
    echo "No SSH keys found."
    return 1
  fi

  # If only 1 SSH key available, auto select it
  if [ ${#keys[@]} -eq 1 ]; then
    item="${keys[0]}"
    echo "Selecting the only SSH key: $item"
    echo "export GIT_SSH_COMMAND=\"ssh -i $item -o IdentitiesOnly=yes\""
    export GIT_SSH_COMMAND="ssh -i $item -o IdentitiesOnly=yes"
    return 0
  fi

  # Check if an argument was provided
  if [ "$1" ] && [[ "$1" =~ ^[0-9]+$ ]]; then
    index="$1"
    # Check if the provided index is within the range of available keys
    if [ "$index" -lt 1 ] || [ "$index" -gt "${#keys[@]}" ]; then
      echo "Invalid index: Index out of range."
      return 1
    fi
    # Set selected key using the provided index
    item="${keys[$((index-1))]}"
    echo "Automatically selecting SSH key: $item"
  else
    # Prompt the user to select an SSH key
    echo "Select an SSH key:"
    select item in "${keys[@]}"; do
      if [ -n "$item" ]; then
        break
      else
        echo "Invalid selection."
        return 1
      fi
    done
  fi

  # Set the selected key as GIT_SSH_COMMAND
  echo "export GIT_SSH_COMMAND=\"ssh -i $item -o IdentitiesOnly=yes\""
  export GIT_SSH_COMMAND="ssh -i $item -o IdentitiesOnly=yes"
}

Git tree

Trái: trước

Phải: sau

{{:git:git_log_custom.png?nolink|}}

Cop cái này vào ~/.bashrc hoặc ~/.zshrc

git() {
    if [[ $@ == "log" ]]; then
        command git log --oneline --decorate --graph
    else
        command git "$@"
    fi
}

Specify a specific SSH private key for git pull/git clone

https://ma.ttias.be/specify-a-specific-ssh-private-key-for-git-pull-git-clone/

I’ve been moving some projects around lately and found myself in need of a weird thing I hadn’t considered before: specifying a specific SSH private key for running things like git clone or git pull.

Luckily, it wasn’t that hard.
Using a specific environment variable

You can overwrite the SSH command that’s being used by git, by giving it a custom environment variable.

Consider this example:

$ GIT_SSH_COMMAND='ssh -i /var/www/html/ma.ttias.be/.ssh/id_rsa' git pull

Nếu bị lỗi thì cần thêm ‘-o IdentitiesOnly=yes’

$ GIT_SSH_COMMAND='ssh -i /var/www/html/ma.ttias.be/.ssh/id_rsa -o IdentitiesOnly=yes' git pull

This runs the git pull command, but it does so by using a very specific private key located in /var/www/html/ma.ttias.be/.ssh/id_rsa.

The GIT_SSH_COMMAND is available in modern git versions and can be used to overwrite the identity (-i) or things like the SSH port.
A custom SSH config

An alternative approach, but one I found less useful for quick-and-dirty git operations, is to modify your SSH client config.

You can specify an alias for SSH operations and use that to point to the correct identity file.

$ cat ~/.ssh/config
Host yourserver
    Hostname something.domain.tld
    IdentityFile /var/www/html/ma.ttias.be/.ssh/id_rsa
    IdentitiesOnly yes

Now, if you git clone from that specific alias, it will use your private key.

$ git clone git@yourserver:yourrepo.git

The yourserver translates to the alias used in ~/.ssh/config.

Proxy config

git clone -c "http.proxy=socks5h://127.0.0.1:9999" git@git02.xxx.com:yyy/zzz.git