Thread (24 messages) flat view 24 messages, 9 authors, 2016-06-15

Re: best way to fastforward all tracking branches after a fetch

From: Sitaram Chamarty <hidden>
Date: 2016-06-15 22:52:36

2011/12/11 Jakub Narebski [off-list ref]:
Don't remove people from Cc, please.

Gelonida N [off-list ref] writes:
quoted
On 12/11/2011 03:22 AM, Sitaram Chamarty wrote:
quoted
On Sat, Dec 10, 2011 at 01:26:32PM +0100, Gelonida N wrote:
quoted
quoted
So what you want would boil down to this script (untested):

    #!/bin/bash
    git status --porcelain -uno | grep . && {echo dirty tree, exiting...; exit 1; }

    for b in `git for-each-ref '--format=%(refname:short)' refs/heads`
    do
        git checkout $b
        git merge --ff-only @{u}
    done
Is there no way to distinguish tracking branches from other branches?
without checking them out?

In order to save time I'd like to avoid checking out local branches.
You can use 'upstream' field name in git-for-each-ref invocation,
for example

 git for-each-ref '--format=%(refname:short) %(upstream:short)' refs/heads |
       grep -e ' [^ ]' |
       sed  -e 's/ .*$//

This could probably be done using only sed -- grep is not necessary.
quoted
Ideally I would even like to avoid checking out branches, which don't
need to be forwarded.
You can use git-update-ref plumbing, but you would have to do the
check if it does fast-forward yourself, and provide reflog message
yourself too.
if it's not the currently checked-out branch, 'git branch -f foo
origin/foo' seems to work fine.  However, it only updates the branch
reflog, not the HEAD reflog also, naturally.

FWIW...
Something like

 git for-each-ref '--format=%(refname) %(upstream)' |
 while read refname upstream
 do
       # there is upstream
       test -n "$upstream" || break
       # and if fast-forwards
       test $(git merge-base $refname $upstream) = $(git rev-parse $refname) || break
       git update-ref -m "$message" $refname $upstream
 done
quoted
I also had to remember on which branch I was in order to avoid, that I
am at a random branch after running the script.

I could imagine something like my snippet below , though I guess,
there's something more elegant.

git stash
mybranch=`git branch | sed -n 's/\* *//p'`
# do_script . . .
git checkout $mybranch
git stash apply
Don't use git-branch in scripting.  See __git_ps1 function in
contrib/completion/git-completion.bash how it can be done:

 b="$(git symbolic-ref HEAD 2>/dev/null)" ||
 b="$(git rev-parse --verify HEAD)"

Nb. the second part is here only if there is possibility that you are
on detached HEAD (unnamed branch).

HTH (hope that helps)
--
Jakub Narębski


-- 
Sitaram
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help