0

I have some git repositories on my local machine that are hobby projects (hosted on GitHub), and some that are work-related (hosted elsewhere). Because I am doing it wrong (don't ask, it's irrelevant) I periodically have to rm -rf and re-clone a repo, which also wipes out its .git/config file.

I would like to put something in my ~/.gitconfig that would set user.email differently for my work projects than my hobby projects. (Remember, I can't use .git/config because it periodically gets wiped out.) This could be based on the project name, or based on the URL of the remote repo I'm cloning.

As a separate but related issue, I would love to be able to specify a global pre-commit hook that applied to all my repositories, rather than having to remember to modify .git/hooks/pre-commit every time I clone a repo. (I am aware of git clone --template.)

I suppose I could write a Bash script aliased to git that would do all this setup whenever it noticed me doing a git clone. Is that the only answer, then?

Quuxplusone
  • 19,419
  • 5
  • 72
  • 137

3 Answers3

0

If you use any of the Bash extensions which change your prompt when you change directories, you might want to hook into those to set GIT_AUTHOR_NAME, GIT_COMMITTER_NAME, and the associated email environment variables depending on which repository you're in.

In .bashrc, you could do something like this:

__git_ps1() {
   BRANCH=`git branch 2>/dev/null`
   if [ "$?" == "0" ]; then
      # Do something with your prompt
      if [[ `git remote -v` =~ $WORK_URL_REGEX ]]; then
         export GIT_AUTHOR_EMAIL="me.working@work.com"
      else
         export GIT_AUTHOR_EMAIL="me.playing@home.org"
      fi
   endif
}

This is obviously very rough (I haven't tested it, either) but should give you a good starting place.

Greyson
  • 3,430
  • 1
  • 19
  • 22
0

no idea why you would /have/ to reclone..

git reset --hard && git clean -xdf

is good enough and will give you a clean working directory. This will preserve your config changes.

If you have a mess with your references, you can clean up remote branches with

git remote prune

in other words, I /am/ asking why you are "doing it wrong" :)

Adam Dymitruk
  • 109,813
  • 21
  • 138
  • 137
  • I just tried `git reset --hard && git clean -xdf` on a local repo, and it didn't seem to do anything different from `git checkout --`. In particularly, it didn't wipe out commits I had already made to my local repo and now wanted to get rid of. ...Oh wait, you mean `git reset --hard origin/master`. Yeah, that works, I think. (But you have to do it once per branch; `git --reset hard origin` does *not* do what I expected! And once per repo, as opposed to `rm -rf *`; not that that's a big disadvantage.) – Quuxplusone Jun 01 '12 at 17:56
  • There are 2 things that you are thinking of. Cleaning your working dir and setting your branches back. `git reset --hard origin` is ambiguous. You need to say which branch. You can script this and iterate over each branch without checking out each one. `git push . origin/master:master -f` will reset the branch even though it is not checked out. – Adam Dymitruk Jun 01 '12 at 19:01
0

If it were me, I'd probably wrap the raw rm -rf call with a script that copies the content I want to save to some temp location, deletes everything, clones the repo, and copies the content back in.

ellotheth
  • 3,803
  • 1
  • 15
  • 28