简体   繁体   中英

How can I reinitialize heads from tracking branches in git config?

If I run,

$ git branch -t 86 upstream/86
Branch '86' set up to track remote branch '86' from 'upstream'.

I can see that two things happen

  • an entry is added to .git/config

     [branch "86"] remote = upstream merge = refs/heads/86
  • a ref file is created for the corresponding branch, ./.git/refs/heads/86

If I delete this file, ./.git/refs/heads/86 is the remaining entry in the .git/config of any use? Is there any command I can run to recreate all of the heads (files in the directory)? Or must I rerun all of the commands that I originally ran to create them?

If you've got that branch checked out, git reset --soft origin/86 will do ( --soft does only the HEAD update, the default reset, --mixed , also reloads the index entries; --hard additionally resets the work tree).

If you don't (or either way, really, this works regardless), it's core-command time: git update-ref refs/heads/86 origin/86 .

edit: to do this in a batch you can generate update commands from the config entries, git update-ref has a --stdin argument and a little command interpreter for doing bulk updates.

I'm using ansible to provision git config files with remote tracking information, and this is the only case where putting stuff inside the config file is not sufficient because of a synchronization issue between the git config and the remote refs. So i want to recreate the remote refs from the .gitconfig.

That should have been your lead paragraph.

It's reasonable to assume you're using the standard refspecs, +refs/heads/*:refs/remotes/$remote/* , slightly less reasonable to assume everything's coming from origin , but I'll leave generalizing this to multiple remotes or custom refspecs as an exercise for the reader:

git config --get-regexp ^branch\..*\..merge \
| awk -F'[. /]' '{print "update refs/heads/"$2" origin/"$NF}' \
| git update-ref --stdin

To actually create the remote refs, you have to run git fetch , going through git ls-remote and creating the refs manually won't help if you don't have the objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM