0

How can I make git commands (e.g. git status) not operate on a repository if my working directory is ignored by that repository? For example:

  1. I run git init in ~ to create a repo to track dotfiles

  2. I add a .gitignore file in ~, which looks like this:

    *
    !.gitignore
    !.bashrc
    

    and I commit .gitignore and .bashrc

  3. I move to ~/src/test, which is not a git repository

If I now run git status, the message I would like to see returned is

fatal: Not a git repository (or any of the parent directories): .git

Instead, I will see

# On branch master
nothing to commit (working directory clean)

because git status went up the directory tree and found the .git file in my home directory, even though I have ignored the working directory in that repo.

The only solution I see is to set the GIT_DIR for the repo in my home directory to something like .home.git, and then run all of my git commands for that repo as

git --git-dir=~/.home.git --work-tree=~ <command>

I could alias that, but I feel like there is probably a better way to configure git to do this.

Rose Kunkel
  • 3,102
  • 2
  • 27
  • 53
  • Is this specifically for the dotfiles example, or a general question? – Leigh Nov 11 '13 at 01:53
  • It's a general question, but the example is the most immediate thing I'd be using it for. I'm aware of other solutions for keeping dotfiles in git, but this seems like the simplest, if I can make it work. – Rose Kunkel Nov 11 '13 at 02:16
  • I personally use a git repository in `~/.dotfiles/` which has a bootstrap script to copy them across to `~/` to avoid that issue; I think it's grabbed from http://dotfiles.github.io/ or one of those pages. For a general case, though, I hadn't read too much up on it, sorry. – Leigh Nov 11 '13 at 05:40
  • Did you ever find a solution to this? I have similar request [here](http://stackoverflow.com/questions/23306871/commands-in-a-git-script-functions-windows-not-working-as-expected-in-sub-fold). – Terry Apr 30 '14 at 15:33
  • I ended up just using an alias, although I'm on a Windows box for now and I'm pretty much just not using it. – Rose Kunkel Apr 30 '14 at 21:30

1 Answers1

0

I would recommend to you to take a look at blog post Using Git and Github to Manage Your Dotfiles. The point is to create separate dir for git-project and make link to needed dotfiles:

home/
|
|--dotfiles/      <-- git working copy
|  `--vimrc
|  `--bashrc
|--.vimrc->dotfiles/vimrc
|--.bashrc->dotfiles/bashrc
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
  • I am aware of that blog post. It seems highly inelegant and I would prefer to move my GIT_DIR before I would do that. That being said, I feel like there has to be a better way than moving GIT_DIR. – Rose Kunkel Nov 11 '13 at 05:41