0

As documented and discussed elsewhere, git fetch refuses to fetch the current branch unless you supply the -u option.

This behavior makes perfect sense in most cases, and is obviously the correct default. However, it is not the behavior I want. Instead, I would like something like what the configuration option receive.denyCurrentBranch=updateInstead does: if the current working tree and index match HEAD, then allow the fetch and update working tree; otherwise, refuse the fetch with a similar message to the default. Is there any way to do this without writing my own porcelain command using git fetch -u?


The reason I want this, in case a better workflow is available, is that I am working on a number of projects stored in separate Git repositories. Most of them are just on the dev local branch, which has the dev/master remote branch as its upstream. When I am actually doing development, I do it on a branch named after whatever feature I’m working on, not the dev branch. I want all the remote branches available, so I need to do a git fetch --all -p regularly, and I additionally need to do a git pull in the projects I’m not building so they have the latest code. Because there are a lot of projects, the easiest way is to just loop through them in Bash and run git fetch --all -p; git pull on all of them. Unfortunately, that also runs git pull on the projects I’m currently making changes on, and that might lead to merge conflicts instead of just getting the latest dependencies.

Instead, I would like the projects I’m not currently working on have the working tree updated automatically; then I only need to run git pull on repositories I am currently working on. That way, I can run it only when it’s appropriate, and not worry about merge conflicts when I just want to build against the latest source of the dependent projects.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
  • `git fetch` itself never updates the index and work-tree. I think you probably do need to write your own porcelain here. Note that `git-sh-setup` provides a way to test whether the index and work-tree are "clean" (match the `HEAD` commit), including submodules, without having to parse the output of `git status`. Note also that `git pull` is just `git fetch && git merge`, more or less, so instead of `git pull` you can test, then maybe-run `git merge`. – torek Jun 01 '17 at 15:22
  • @torek I have not actually written my own Git commands before; is there a good guide to that anywhere? – Daniel H Jun 01 '17 at 16:39
  • There are plenty of *bad* ones :-) I'm not really sure of any good ones. Many parts of Git are written as shell scripts, though. Run `git --exec-path` and look in the resulting location, you'll find `stash` and `filter-branch` as examples of huge shell scripts. I'll add that if you write a script named `git-foo` and put it anywhere in your `$PATH`, `git foo` will run your script (with `git --exec-path` added at the front of `$PATH` so that you can `. git-sh-setup`, for instance). – torek Jun 01 '17 at 17:19

2 Answers2

1

I additionally need to do a git pull in the projects I’m not building so they have the latest code ... Unfortunately, that also runs git pull on the projects I’m currently making changes on

You can run git pull --ff-only, which fails immediately if there are some local commits.

max630
  • 8,762
  • 3
  • 30
  • 55
  • I really should have thought of this myself and don’t know why I didn’t. It still doesn’t do exactly what I asked for, but it’s close enough and I think it does what I actually want. – Daniel H Jun 05 '17 at 16:41
  • It wasn’t quite that simple—I use pull-with-rebase by default because rebasing is expected where I work, and I needed to add `-c rebase.autoStash=no` to the command line—but now it works exactly as I wanted, and I can make an alias that does the fetch and that at the same time. – Daniel H Jun 05 '17 at 16:58
0

You can start to use git hooks and run

git fetch -a

any time it is necessary

sensorario
  • 20,262
  • 30
  • 97
  • 159
  • None of the hooks seem to be run on fetch, unless the ones which run on remote push also run on local fetch but I don’t see that documented anywhere. – Daniel H Jun 02 '17 at 11:49