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?

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
cade galt
  • 3,843
  • 8
  • 32
  • 48
  • You may want to reconsider your work flow. Git usually isn't used for automatically created content. – PyRulez Jan 11 '16 at 21:56

2 Answers2

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This does not work, git is telling me ... `fatal: Not a git repository: '~/root.git' ` – cade galt Jan 11 '16 at 19:28
  • @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 (http://stackoverflow.com/a/7632497/6309). Try though with `git -c 'core.bare=false' --work-tree=~/public_html --git-dir=~/root.git checkout -f` (http://stackoverflow.com/a/12548255/6309) – VonC Jan 11 '16 at 19:39
  • @cadegalt I meant `git -c 'core.bare=false' --work-tree=~/public_html --git-dir=~/root.git add -- path/to/userfile` – VonC Jan 11 '16 at 19:46
  • @cadegalt the working tree path is relative indeed to `~/public_html`. I will edit the answer. – VonC Jan 11 '16 at 19:49
  • @cadegalt What version of git are you using? – VonC Jan 11 '16 at 20:04
  • @cadegalt please upgrade to a more recent version of git: this one is more than 5 years old. – VonC Jan 11 '16 at 20:16
  • @cadegalt If upgrading git is not an option, I have added an alternative. – VonC Jan 11 '16 at 20:38
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

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 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. – cade galt Jan 09 '16 at 21:56
  • Ok, so update your question accordingly, its not clear – CodeWizard Jan 09 '16 at 21:57
  • `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. – cade galt Jan 09 '16 at 21:58
  • Of course. you can only add on your local branch not on a bare repo. – CodeWizard Jan 09 '16 at 21:59
  • 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? – cade galt Jan 09 '16 at 22:04
  • Nope, its something you can do, you can have bare repo checkout specific branch or commit as well – CodeWizard Jan 09 '16 at 22:05
  • Read more here: https://feeding.cloud.geek.nz/posts/setting-default-git-branch-in-bare/ – CodeWizard Jan 09 '16 at 22:07
  • yea but , read my one line hook above, I tell git to checkout on a push, and it does, and it calls it a working tree and than later tells me it is not a working tree ... why this ambiguity ? – cade galt Jan 09 '16 at 22:08
  • Of course you have to add the parameters with the path to the work tree to every Git call you do, not only the checkout. Are you doing this, when you execute e.g. a "git add"? – dunni Jan 10 '16 at 00:10