0

I want to commit some file from a script to a git server I own, and this needs to be done automatically.

My script (that resides in ~/bin) does something like this

rsync -arE --delete --files-from=~/files.list / ~/repo
git --git-dir ~/repo/.git add ~/repo/*
git --git-dir ~/repo/.git commit -m "automatic commit #123"
git --git-dir ~/repo/.git push

I want it to copy the files that are listed in the file ~/files.list to ~/repo (rsync handles this) and then add this files to the repository, commit and push.

However, it is unable to correctly add files to the git commit, particularly it always grabs the files that are in the working directory (in this case ~/bin), so I would need a way to either:

  1. change the running directory for the git commands OR
  2. tell git to add files from a directory other than the working one
3
  • You could add a .gitignore. Blacklist everything in bin Commented Apr 4, 2019 at 19:01
  • the problem is that it ONLY adds files in ~/bin Commented Apr 4, 2019 at 19:15
  • git add -A to add everything. Use in conjunction with a .gitignore. Commented Apr 4, 2019 at 20:10

2 Answers 2

2

It sounds like what you want is Git's -C option, which changes the current directory before running. You can write something like the following:

rsync -arE --delete --files-from=~/files.list / ~/repo
git -C ~/repo/ add .
git -C ~/repo/ commit -m "automatic commit #123"
git -C ~/repo/ push

Of course, you can also write this with as a cd and a subshell, but the above may be a little easier:

rsync -arE --delete --files-from=~/files.list / ~/repo
(cd ~/repo/ &&
 git add .
 git commit -m "automatic commit #123"
 git push)

I assume here that your rsync command is already doing what you want it to.

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

Comments

2

No need to copy, just tell git where your working files are.

git -C ~/repo --work-tree="$PWD" add .
git -C ~/repo commit -m etc
git -C ~/repo push

Put your files.list list in .gitignore format, ignore everything but the files you want git to hunt down, and include it as the repo's .git/info/exclude:

# To include only this, and/this, and/that but ignore everything else:
# ignore everything
*
# except
!this
!and/this
!and/that
!etc
# do search inside directories, don't let ignores stop later searches inside
!*/

A note, the paths on the command line are taken relative to the work tree if you're not already inside it, so if you're in a repo and want to add content from elsewhere you can git --work-tree=/path/to/elsewhere add . and it'll add it all.

Comments

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.