Git pushing to non-bare repo, save current worktree

How can I push to a non-bare git repository, automatically add and commit any changes in the working tree, and then re-checkout the current branch to reflect the changes from that push?

I was thinking of something like this:

  • On conflict, GitHub for Windows puts me in “rebasing” state, how to go from there?
  • PHP filtering files and paths according .gitignore
  • git except a sub directory and it's files of a ignored directory
  • Git pull origin/master branch to local/master, when in local/develop
  • Authenticate Jenkins CI for Github private repository
  • Pushing to Github from RStudio
  • Add a hook on the remote (the non-bare repo) to run git add . && git commit -m "Automated commit" && git reset --hard

    Are there any drawbacks to that method? Is there another way of doing this?


    (Disclaimer: I know that this isn’t the most ideal situation, but this is what we have at my company and I want to make it as streamlined as possible without needing everyone to completely change the way they do things)

    Thanks!

  • “git rebase origin” vs.“git rebase origin/master”
  • How do I create an orphan branch from the Github API?
  • git svn cherry pick ignored warning
  • How to use git-svn properly, svn-ers checkout svnrepo and git-ers clone gitrepo, committing carelessly
  • Extract changes from diff file to current branch
  • Is it a good idea to put db/schema.rb to .gitignore list ??
  • One Solution collect form web for “Git pushing to non-bare repo, save current worktree”

    After messing around I found a pretty good solution.

    Pre-receive hook:
    Checks for any changes in working directory, adds/commits them, then alerts the user he/she needs to merge before pushing

    #!/bin/sh
    cd ../
    unset GIT_DIR
    CHANGED=$(git diff-index --name-only HEAD --)
    if [ -n "$CHANGED" ]; then
        git add -u .
        git commit -m "Automated commit"
        echo "There were uncommitted changes in the working directory...please pull them and push your changes again"
        exit 1
    fi 
    

    Post-receive hook: Force checks-out the current branch so changes will be shown in working directory. This will overwrite any changes in the working directory, but those will have already been added/committed/merged by the pre-receive hook.

    #!/bin/sh
    cd ../
    unset GIT_DIR
    branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
    
    git checkout -f $branch
    
    
    echo "Update pushed to branch $branch and checked out in the website directory" 
    
    Git Baby is a git and github fan, let's start git clone.