3

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:

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!

Matt Kantor
  • 1,704
  • 1
  • 19
  • 37
jraede
  • 6,846
  • 5
  • 29
  • 32

1 Answers1

1

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" 
jraede
  • 6,846
  • 5
  • 29
  • 32
  • 1
    I feel much dislike about those scripts as they are lame: 1) Why no `set -e -u`? What happens if any part of the script fails? 2) Why `git-branch | sed` acrobatics instead of just `git rev-parse HEAD`? 3) Where in the docs it's said a certain current directory is assumed when a hook is run? Why `GIT_DIR` is explicitly unset? – kostix May 15 '13 at 10:24
  • So provide an answer if you have a better one. I took the git branch | sed from another question on Stack Overflow. I did a check to see what the current directory is, turns out to be the root of the .git directory. There is an issue where if you CD, you explicitly have to unset GIT_DIR if you want git commands to use the current directory instead of the value of GIT_DIR. – jraede May 16 '13 at 02:38
  • I don't have an offhand answer, I wanted to point out that [cargo cult programming](http://en.wikipedia.org/wiki/Cargo_cult_programming) and mindless copying and pasting of random bits of code is not a good practice. That's why it's a comment -- it does not relate to the question per se. – kostix May 16 '13 at 07:56
  • Great, well the reason I asked the question here is because I didn't know how to do it. I'm not going to spend hours to understand the difference between `git-branch | sed` and `git rev-parse` when the answer is on stack overflow and it works. – jraede May 16 '13 at 18:45
  • The first hook gives me `ref updates forbidden inside quarantine environment ` – Fanky Sep 28 '19 at 19:58