1

I have several local clones of a remote repo. Working in one of them, I update some branches and push to the remote. I then go to another different clone, and I want to fetch and update my remotes cached here in this other local repo.

I have a procedure which works fine, but is manual. Here's an example. First, working in the first local clone repo, I do some changes to branch1,branch2 and branch3, then push. Next, over in the other local clone of the same remote repo, I am wanting to bring its cached copies of the remotes up to date, so I do this...

   $ git fetch
   ...says remote counting objects, unpacking etc then a series of lines like...
   eb7b3000..67f0ade3  mybranch1      -> origin/mybranch1
   eb7b3000..67f0ade3  mybranch2      -> origin/mybranch2
   eb7b3000..67f0ade3  mybranch3      -> origin/mybranch3
   ...and usually also some [new tag] lines

This identifies the branches that have been updated by the "first" local repo, and I proceed to update each one by one, like this...

   $ git fetch origin mybranch1:mybranch1
   $ git fetch origin mybranch2:mybranch2
   $ git fetch origin mybranch3:mybranch3

This is a slightly painful manual procedure but it works perfectly. Seems that "git fetch --all" or something like that should do this, maybe I'm missing the obvious here, but I can't seem to find a built-in git command that updates those other branches without checking them out. The mybranch1:mybranch1 syntax as above seems to do it perfectly, leaving me wherever I was, but ensuring that a reloaded gitk will now show correct info for all the remote branches, which have been updated by this sequence of fetches.

I have looked at the bash and one-liner solutions to similar but different problems eg here: [How to fetch all Git branches

These solutions identify all branches, and solve a slightly different problem. I want to just fetch and update local branches that are tracking a repo branch if that has changed, not grab all branches including ones I am not tracking locally.

I'm struggling to write the alias or bash script (or expose the 'obvious' trick I may have missed) to solve this. I just want to be able to type a single alias or other command that does the git fetch, identifies all the branches that have been updated and then does that sequence of git fetch origin aaa:aaa etc for each changed remote branch. Any help appreciated.

[Currently using git cmd line version 2.17.1.windows.2, happy to update if that's the problem]

DaveB
  • 27
  • 4

1 Answers1

0

I have several local clones of a remote repo.

Another approach would be to have only one clone of the repo.
And checkout multiple branches in their own folder, with git worktree.

That way, no fetch needed, you are always working in a working tree representing an up-to-date version of your branch.

If that approach is not convenient, then a script (like this one) would be needed to update all your local branches.
A more complete script: "Git - Automatically fast forward all tracking branches on pull".
That involves fast-forward update only, and shy away from any merge (with potential conflicts) updates.
Note: as commented, an initial git fetch would be needed.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yes, I do want to keep two (or more) independent clones of the repo, and go back and forth between them, as this has other advantages for me. So I think it sounds like a script is necessary, but the one you pointed to doesn't really answer the problem of identifying the list of branches that need updating. It focuses on updating one of them via a fast forward, and doing that carefully, which is great, but doesn't really automate the whole job here. – DaveB Feb 20 '19 at 06:25
  • @DaveB I would not check what branch has been updated: I would fetch everything, then loop on each branch to update them: https://stackoverflow.com/a/24451300/6309 – VonC Feb 20 '19 at 07:36
  • VonC pointer to https://stackoverflow.com/a/24451300/6309 works fine, excellent. I tried to also move the shell script to an alias, looking at notes here... https://stackoverflow.com/questions/1309430/how-to-embed-bash-script-directly-inside-a-git-alias but couldn't get that to work. Seemed to be my bad syntax possibly incorrectly escaping or not escaping some of the special chars. I basically added \ to line ends other than last and tried the embedded function definition as discussed there. However, since the shell file works fine, this is just an optional tidyup. – DaveB Feb 22 '19 at 02:37
  • Correction: after more testing I find the https://stackoverflow.com/a/24451300/6309 bash file needs an additional line with simply "git fetch" at its start, to ensure changes are actually fetched ahead of the big fast forward loop. Then it seems to work as needed. – DaveB Feb 22 '19 at 12:09
  • @DaveB OK. I have amended the answer accordingly. – VonC Feb 22 '19 at 12:20