Asked  6 Months ago    Answers:  5   Viewed   26 times

Is it possible to deploy a website using git push? I have a hunch it has something to do with using git hooks to perform a git reset --hard on the server side, but how would I go about accomplishing this?

 Answers

16

I found this script on this site and it seems to work quite well.

  1. Copy over your .git directory to your web server
  2. On your local copy, modify your .git/config file and add your web server as a remote:

    [remote "production"]
        url = username@webserver:/path/to/htdocs/.git
    
  3. On the server, replace .git/hooks/post-update with this file (in the answer below)

  4. Add execute access to the file (again, on the server):

    chmod +x .git/hooks/post-update
    
  5. Now, just locally push to your web server and it should automatically update the working copy:

    git push production
    
Tuesday, June 1, 2021
 
Manmay
answered 6 Months ago
10

Deploying to Heroku should work regardless of where you host your code as the Heroku CLI adds it's own git remote for the sake of deployments. In fact, you can even git pull from Heroku, so you could technically use Heroku as a private git repository instead (though not recommended). As for using the GitHub client to connect to bitbucket, simply change the repository remote to the URL provided by bitbucket in the settings tab of the client.

Sunday, July 11, 2021
 
janlindso
answered 5 Months ago
60
cd old_version
git rm -rf .
git commit -m "Removing the old code"
git remote add version2 "/path/to/new_version/.git"
git pull version2 master

That should fully preserve both histories and have the new version be the HEAD of orginal repo. I would recommend backing up repos up before you try this.

Wednesday, August 4, 2021
 
altermativ
answered 4 Months ago
72

Assuming the script you execute on each file can't be set up to fail immediately when it fails on one argument, you can't use that xargs setup. You'll could do something like this:

#!/bin/bash

# for a pre-commit hook, use --cached instead of HEAD^ HEAD
IFS=$'n'
git diff --name-only HEAD^ HEAD | grep '.php$' |
while read file; do
    # exit immediately if the script fails
    my_script "$file" || exit $?
done

But perhaps phpmd will do this for you already? Couldn't quite understand from your question, but if it does, all you have to do is make sure it's the last command in the hook. The exit status of a pipeline is the exit status of the last command in it (phpmd), and the exit status of a shell script is the exit status of the last command it ran - so if phpmd exits with error status, the hook script will, and if it's a pre-commit hook, this will cause git to abort the commit.

As for an option to git-commit to control invocation of this hook, you're kind of out of luck, unless you consider --no-verify (which suppresses all commit hooks) good enough. An alias... the cleanest way to do that would be to set an environment variable in the alias, probably:

# gitconfig
[alias]
    commitx = "!run_my_hook=1; git commit"

# your script
#!/bin/bash
# if the variable's empty, exit immediately
if [ -z "$run_my_hook" ]; then
    exit 0;
fi
# rest of script here...
Saturday, August 14, 2021
 
Razvan N
answered 4 Months ago
38

Your error is because you're not quoting $mf. Change it to "$mf". Though there are perhaps better ways than grepping the output of a human-readable command... you could have a look at git status --porcelain for example. Or even git diff --cached <path>, and just examine the exit code, e.g.:

if ! git diff --quiet --cached <path>; then
     # the file was modified; do stuff
fi

I think Amber may have misled you: you should use --cached, because if the changes are not staged, then as far as this commit is concerned, there are no changes, so I assume you don't want to do anything else.

And of course, I don't know your project, but I'm not sure why you're doing something like this - usually you don't want to check in machine-generated content, just make it easy to rebuild from what is checked in.

As for your last problem, the file being modified but not added to the commit, I can't reproduce it with a toy example. I made this as a pre-commit hook:

#!/bin/bash
touch z
git add z

and made a commit, and z was as expected created, added, and committed.

Saturday, August 21, 2021
 
nfechner
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share