Git: Ignore files for public repository, but not for private

后端 未结 8 1063
心在旅途
心在旅途 2020-12-01 07:54

I\'m deploying a Rails app on Heroku (for now) via git, and would also like to have a public version for people to look at. Some files are sensitive and should only be commi

8条回答
  •  Happy的楠姐
    2020-12-01 08:12

    You could create a pre-commit hook in your local repo, in here you can write a script to check the currently checked out branch and delete the offending files if they are present before the commit is processed. This avoids the files ever being recorded in the Git history of the wrong branch.

    #!/bin/bash
    current_branch="$(git branch | sed -e 's/^*//')"
    if [ $current_branch != "heroku" ]; then 
        // Delete sensitive files before commit
        rm -f dir1/dir2/exclude_from_public
        rm -f dir1/dir2/exclude_from_public_also
    fi
    exit 0
    

    Alternatively, the script could just check for the files and return exit code "1", notifying you that the commit cannot proceed because it contains sensitive files.

    The caveat is that you will need to hand this script to anyone who is working on the "privileged" heroku branch, and always have it included in your own local repo.

    Ideally you would have this check done server-side as well; but unfortunately GitHub only offers a Web variant of the post-receive hook, so unless you do you're own repo hosting this approach can only be performed locally.

提交回复
热议问题