Home > Mobile >  How to auto set-upstream when pushing with push.default current?
How to auto set-upstream when pushing with push.default current?

Time:12-11

I used to delete my branches automatically with the indication of git fetch -p plus some git plumbing command¹, and matching for a [gone] pattern then deleting matched branches.

Some repositories I work with enforce squashing pull requests, therefore merged commit are never made locally, and I have no way of linking content of new commits and contents of my local branches.

Some times ago I switched to push.default current, and the feature is really better than push.default upstream or simple.

Since a few days, I noticed all my branches aren't deleted anymore ; and that comes from the fact when pushing, the upstream is "guessed" but never set.

If I manually set the upstream of a branch (through --set-upstream-to), then I can see my trim script working again ; but that defies the purpose of using push.default set to current.

$ git switch -c my-branch
Switched to a new branch 'my-branch'
$ git push 
Total 0 (delta 0), reused 0 (delta 0)
remote: ...
$ git branch -vv
  main      3b3a404 [origin/main] Merge branch 'test-push' into 'main'
* my-branch 3b3a404 Merge branch 'test-push' into 'main'
$ git push -u origin my-branch 
Branch 'my-branch' set up to track remote branch 'my-branch' from 'origin'.
Everything up-to-date
$ git branch -vv
  main      3b3a404 [origin/main] Merge branch 'test-push' into 'main'
* my-branch 3b3a404 [origin/my-branch] Merge branch 'test-push' into 'main'
$ git push -d origin my-branch # this is done automatically after merging
$ git fetch -p
...
 - [deleted]         (none)     -> origin/my-branch
$ git branch -vv
  main      3b3a404 [origin/main] Merge branch 'test-push' into 'main'
* my-branch 3b3a404 [origin/my-branch: gone] Merge branch 'test-push' into 'main'
                                       ^... this is what helps me automatically trimming merged branches

Is there a way to automate that upstream setting to the pushed-to branch without actually typing it ?

I actually tested to plug a pre-push hook, but if the branch does not exist, then set-upstream cannot work ; and post-push hooks don't exist.

Actually, my end goal is to delete merged-squashed branches, but I see no better candidate that checking if the remote is gone to verify that ; since I have got no garantees of history rewrites

1: git for-each-ref --format '%(upstream:track) %(refname)' refs/heads | awk ' $1 == "[gone]" { sub("refs/heads/", "", $2) ; print $2 }'

CodePudding user response:

If your push.default is set to current, git push -u alone (without the ... origin my-branch) will create the remote branch and set your local branch to track that remote branch.


additional note : %(refname:short) (in git for-each-ref --format) will display the branch name you are used to

  • Related