1

I use a push to deploy strategy on linux servers.

Works great.

The problem I have is that users can add files - user created files.
I need these files added to the repo.

What is a strategy for doing this.

Here is my push to deploy code in hooks/post-receive

#!/bin/sh
git --work-tree=~/public_html --git-dir=~/root.git checkout -f
echo ""
echo ""
echo ""
echo "success-"
echo ""
echo ""
echo ""

When a user adds a file it is in public_html and is in no way tracked by git.

How can I fix this?

1
  • You may want to reconsider your work flow. Git usually isn't used for automatically created content. Commented Jan 11, 2016 at 21:56

2 Answers 2

1

You should add and commit your user files first in your hook:

git --work-tree=~/public_html --git-dir=~/root.git add -- path/to/userfile
git commit -m "add user file"

Here, path/to/userfile is a relative path, relative to ~/public_html which is the root folder of the working tree.

Then you can go on and checkout the all repo:

git -c 'core.bare=false' --work-tree=~/public_html --git-dir=~/root.git checkout -f

The -c 'core.bare=false' allows to override the config core.bare of the bare repo, in order to allow git add to proceed, considering ~/public_html as its working tree.

If the '-c' option does not work (because the OP uses git 1.7.1 - released in Dec. 2010!), try first (if upgrading git is not an option) to change the setting, then restore it:

git --git-dir=~/root.git config core.bare false
git --work-tree=~/public_html --git-dir=~/root.git add -- path/to/userfile
git --git-dir=~/root.git config core.bare true

Note that this is a strange practice, since it makes a git push result in a branch with an extra commit. Don't forget to git pull just after the git push, in order to get that new commit.

Sign up to request clarification or add additional context in comments.

7 Comments

This does not work, git is telling me ... fatal: Not a git repository: '~/root.git'
@cadegalt yes indeed: you can checkout a bare repo, but you cannot add files, because there is no existing working tree allowed with a bare repo (stackoverflow.com/a/7632497/6309). Try though with git -c 'core.bare=false' --work-tree=~/public_html --git-dir=~/root.git checkout -f (stackoverflow.com/a/12548255/6309)
@cadegalt I meant git -c 'core.bare=false' --work-tree=~/public_html --git-dir=~/root.git add -- path/to/userfile
@cadegalt the working tree path is relative indeed to ~/public_html. I will edit the answer.
@cadegalt What version of git are you using?
|
0

If you want to add all your files simply use:

# `git add -A` is equivalent to  `git add . && git add -u`.
git add -A

It will add all your modified + renamed files to the index and then you can commit.

# on a single line it should be: 
git add . -A && git commit

This command will add your files unless they are in your .gitignore or marked with the assume-unchanged flag

9 Comments

sorry ... this is not relevant ... I'm using push to deploy and in this method ... I push to a bare repo and than checkout to a working directory.
Ok, so update your question accordingly, its not clear
fatal: This operation must be run in a work tree ... is the message I get when I do this manually. However I am in a working tree as defined by my post update hook so I'm not sure why git contradicts itself.
Of course. you can only add on your local branch not on a bare repo.
But these are user defined files , I'm guessing I need to make the "working tree" a live repo by running git init on it? Or would that be crazy?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.