Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Git post-receive not working as expected ?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Git post-receive not working as expected ?

Hi, I am trying to publish to a remote git repo. On local I have added it as , say call it web-app which should publish to remote "master" branch from a local branch named "public"

Now issue is that whenever push as following then it is publishing to "public" branch on remote repo while I want it to be on "master'.

git push web-app public

I have following in post-receive hook:

#! /bin/sh
GIT_WORK_TREE=/path/to/remote/web/app git checkout -qf master

short version:
how to make sure that any local git branch is synced / pushed to remote "master" always.

Comments

  • ralfralf Member
    edited December 2022

    Have you remembered to chmod a+rx post-receive? Is it definitely in the repo's hooks directory? Are you running the repo as a different user? Maybe you don't have permission to write to the directory you're checking out to. Maybe git isn't in your default path.

    FWIW, I do something similar in my repo, but instead of doing stuff in there directly (which would slow down every push to the repo), I instead just leave a marker somewhere (especially as I run my git as an isolated user using gitolite):

    blahrepo.git$ cat hooks/post-receive
    #!/bin/sh
    BUILDDIR=/home/ralf/.blah-build
    BUILD=`grep build |cut -d" " -f2`
    if [ -n "$BUILD" ]
    then
            touch $BUILDDIR/$BUILD
    fi
    

    Then I have a crontab as my own user which kicks off a build on a different build machine by running this script every minute:

    #!/bin/sh
    BUILDDIR=/home/ralf/.blah-build
    BUILD=`ls -t $BUILDDIR | head -1`
    if [ -n "$BUILD" ]
    then
            (
            echo Building $BUILD...
            rm -f $BUILDDIR/$BUILD
            ssh 192.x.x.x time ./build $BUILD
            ) 2>&1 | mail -s "Building blah $BUILD" [email protected]
    fi
    

    Oh, and of course /home/ralf/.blah-build has permissions drwxrwxr-x and chown'd to ralf:git, so that the git user can write into it.

    This way is good for debugging, as you can watch the directory to see the file being created, and also manually trigger a build by creating the empty file yourself.

    • Yes, permissions are fine, if I push local master branch, it is updating remote master.
    • It is in proper hooks directory as updates are being pushed to correct folders ie the path specified hooks dir.
    • can confirm git paths are fine else would not update if I use master.

    I am not power user of git, just enough to fulfill my need, were using locally, now wanted to use remote and facing problem I mentioned.

  • git push origin master, but you have to set up the correct direction before.

  • Oh, you're trying to push to a remote repo? Then you're only doing half of the thing.

    You confused my by calling it web-app and doing a checkout to a different work tree, so it looked like you were trying to deploy the branch onto a web server.

    @Anayx said:
    Hi, I am trying to publish to a remote git repo. On local I have added it as , say call it web-app which should publish to remote "master" branch from a local branch named "public"

    So, I mean, the easiest way is just git push web-app localbranch:master

    This pushes localbranch to web-app's branch called master.

    Now issue is that whenever push as following then it is publishing to "public" branch on remote repo while I want it to be on "master'.

    git push web-app public
    

    So, this is pushing the local branch public to web-app's branch called public.

    I have following in post-receive hook:

    #! /bin/sh
    GIT_WORK_TREE=/path/to/remote/web/app git checkout -qf master
    

    And this is checking out the branch called master to some random directory. This is your problem. If you want the public branch in that directory, change master to public.

    If what you want is for your repo to push the latest branch to another repo called master, you can do that. Then, it'd look like mine.

    BUILD=`grep build |cut -d" " -f2`
    # now $BUILD is the commit of the last thing pushed
    # you could also use -f3 to get the name of the branch instead
    git push -f another_repo $BUILD:master
    

    This will force a push to this repo on any branch to be pushed to the branch master on another_repo.

    I'm still not sure this is what you want, it sounds a bit crazy to me. Maybe you just want:

    git push -f another_repo public:master
    

    short version:
    how to make sure that any local git branch is synced / pushed to remote "master" always.

    This is a different question again to what you're asking. It also sounds very much NOT like what you want to do, because it would mean you could never have a temporary branch where you're working on things because any push to any branch would change master.

    I'd advise you to manually do what I suggested at the top of this post:

    git push web-app localbranch:master

  • Is your profile picture AI generated?

  • @ralf said:
    Oh, you're trying to push to a remote repo? Then you're only doing half of the thing.

    You confused my by calling it web-app and doing a checkout to a different work tree, so it looked like you were trying to deploy the branch onto a web server.

    @Anayx said:
    Hi, I am trying to publish to a remote git repo. On local I have added it as , say call it web-app which should publish to remote "master" branch from a local branch named "public"

    So, I mean, the easiest way is just git push web-app localbranch:master

    This pushes localbranch to web-app's branch called master.

    Now issue is that whenever push as following then it is publishing to "public" branch on remote repo while I want it to be on "master'.

    git push web-app public
    

    So, this is pushing the local branch public to web-app's branch called public.

    I have following in post-receive hook:

    #! /bin/sh
    GIT_WORK_TREE=/path/to/remote/web/app git checkout -qf master
    

    And this is checking out the branch called master to some random directory. This is your problem. If you want the public branch in that directory, change master to public.

    If what you want is for your repo to push the latest branch to another repo called master, you can do that. Then, it'd look like mine.

    BUILD=`grep build |cut -d" " -f2`
    # now $BUILD is the commit of the last thing pushed
    # you could also use -f3 to get the name of the branch instead
    git push -f another_repo $BUILD:master
    

    This will force a push to this repo on any branch to be pushed to the branch master on another_repo.

    I'm still not sure this is what you want, it sounds a bit crazy to me. Maybe you just want:

    git push -f another_repo public:master
    

    short version:
    how to make sure that any local git branch is synced / pushed to remote "master" always.

    This is a different question again to what you're asking. It also sounds very much NOT like what you want to do, because it would mean you could never have a temporary branch where you're working on things because any push to any branch would change master.

    I'd advise you to manually do what I suggested at the top of this post:

    git push web-app localbranch:master

    Oh man, sorry for the confusion, I am not well-versed with git, just know it enough to find my way around but your explanation is quite helpful, I just wanted this one git push web-app localbranch:master

    :+1:

    Thanked by 1ralf
Sign In or Register to comment.