Dao De Code

git remote: make it easy

Recently I was installing new version of Linux Mint, my preferred Linux distro, and dragged some handy aliases I use from my old .bashrc. One of my recent additions was the one I called gitr from git remote. I work in a team, and from time to time I find myself in a need to add a team mate's fork as a remote. Doing that is simple, as well as removing it later. But it can be even better :)
Here's the code, which also can be found in this gist:

gitr() {  
  if [[ $# == 1 ]]; then
    local name=$1
    if [[ $name == -* ]]; then # starts with -, e.g. "-joe"
      echo "removing remote $name";
      git remote remove $name;
      echo "adding remote $name";
      git remote add $name $(git remote -v | head -n 1 | awk '{print $2}' | sed "s/:.*\//:$name\//");
      git fetch $1;
    git remote -v

Typing gitr with no (or more than 1) arguments will just print remotes in verbose mode.
Typing gitr joe will add Joe's fork of the same repo as a remote and fetch it.
Typing gitr -joe will remove remote named joe.

Well, I know, it's not an alias, it's a function. It just lives in same file as aliases.
Nevertheless, now I don't need to type git remote --help to figure out whether name or url should come first :)

comments powered by Disqus