How would I create a script that would take a list of file names in a git repository (I was planning on using a .txt file with one file name on each line, but I am open to other suggestions) as an input, and then remove those specified files from the repository?

I was thinking about using the 'cat' command and a pipe, so that in the terminal the commands would look something like this:

cd my-git-repo
cat fileNames.txt | myShellScript.sh
git push origin master

And then 'myShellScript.sh' would be:

git rm $0
git add .
git commit -m "removed unused file"

Also I know there should be some type of 'read' command to read fileNames.txt line by line.

  • 1
    Have you attempted anything so far? This question as written is probably too broad. Your question would be much better received if you attempted to write it (even if it's a poor attempt) and then asked question(s) here about any specific problem(s) you encounter. I definitely recommend creating a test repository to work with though in order to avoid unintentionally deleting the wrong data. – Jesse_b Feb 21 at 22:04

You can deploy your code from a git repo with rsync. A file e.g. exclude-rsync.txt could contain all files and directories (one per line) you don't want to deploy to the target (could be a new directory, too).



rsync command

 rsync -avz --delete --exclude-from=exclude-rsync.txt . foo@

CI / CD pipelines

With gitlab, travis ci (& jenkins) you can define ci/cd piplines usually in a yaml file. Whenever something is pushed to git(lab) or a branch is merged to master, the pipeline will start automatically. For gitlab you would add a file called .gitlab-ci.yml and it could look like this:

  - deploy
  stage: deploy
  environment: production
    - master@my_repo
    - yum -y install rsync    
    - |
       rsync -avz --delete --exclude-from=exclude-rsync.txt . foo@

But in a simpler case you would add a shell file e.g. deploy.sh having in mind that those pipelines exist and it might be good practice.

rsync -avz --delete --exclude-from=exclude-rsync.txt . foo@

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.