Search code examples
gitgit-fetch

Equivalent of updateInstead for git fetch -u


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.


Solution

  • 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.