Oskar
Oskar

Reputation: 899

Does git run "fetch" automatically, or only when I tell it to?

I'm using git in bash, and I have one of those fancy bash prompts that gives me some repository information without having to type git status. It tells me which branch I'm on, how many modified and untracked files, and, relevant to this question, how far ahead/behind the remote I am.

Recently I've begun to notice something strange: often when I press enter and a new prompt appears, it suddenly tells me I'm a few commits behind master. And, in fact, I am, "git status" confirms it. But my question is: how does it know? At no point did I run git fetch or git pull or any other command that would talk to the remote and find out how far/behind my local checkout was. In fact, I've seen it happen where I'm fairly certain I haven't run any git commands in between, except for git status -z --porcelain which my bash prompt script runs to generate the prompt.

It's freaking me out a bit. I mean, the information is certainly helpful, but the idea of something running git commands on my repository without me knowing about it is a bit unsettling. So here are my questions:

  1. Does git do this kind of thing? Run fetches automatically in the background as a result of running some other command? Obviously, push/pull needs to run a fetch to execute, but I'm talking about "local" commands like git status, git add, git commit, git diff, etc.

  2. Is there any kind of "command log" in git, where I can audit which operations were run, when, and by whom?

Upvotes: 3

Views: 1295

Answers (3)

Caleb
Caleb

Reputation: 125007

I'm using git in bash, and I have one of those fancy bash prompts that gives me some repository information without having to type git status.

This seems the most likely culprit, by far. Magicmonty is one example of a git prompt script that runs git fetch (see gitprompt.sh). I bet you'll find the culprit if you look through the script that sets your prompt.

Upvotes: 1

Mark Adelsberger
Mark Adelsberger

Reputation: 45769

Unless you (or someone else) has set up something special on your machine to make it run fetches, it doesn't. The "ahead/behind" count is a comparison of your local branch to the remote reference as updated by the last communication with the server.

So how to explain it? Since it probably has to do with something curious to your installation, I can at best pose speculations.

Certainly I work in multiple windows, so sometimes my prompt in one is stale because I've been working in another, and hitting ENTER updates it. Of course you'd probably know if you were doing this... but a similar case (that should never happen, but I can't rule out from only my knowledge of your system) - if your local repo is in shared storage and another user is working with it as well, you could be seeing the results of their work. (If that's the case, I strongly advise you to get each developer working on a distinct repo that only that one developer accesses; that's exactly what clones are for. But like I said, it's all just speculation anyway...)

The other obvious possibility would be if someone thought it might be helpful to have fetch run at scheduled intervals in the background. I would find this aggravating personally, but I can imagine it happening and it would explain the behavior you're seeing.

Upvotes: 0

Vampire
Vampire

Reputation: 38724

  1. Nope, it does not
  2. Not that I know of. What you can do is to set the environment variable GIT_TRACE, then Git tells you which commands it internally calls.

Upvotes: 0

Related Questions