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

后端 未结 8 1002
心在旅途
心在旅途 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条回答
  • 2020-12-01 08:21

    Here are some other StackOverflow questions and answers along the line of "how do you do a merge while ignoring some files":

    • Git: merging public and private branches while while keeping certain files intact in both branches
    • git merge should ignore one directory
    • How do you merge selective files with git-merge?
    • How do you make Git ignore files without using .gitignore?
    • Public and private code in a single Git repository
    • How do I tell git to always select my local version for conflicted merges on a specific file?

    The simplest I can think of, is to use an alias'ed merge that will remove the private files before doing the merge commit. This would work if you're willing to live with non-fast-forward merges. Here's the alias:

    git config alias.merge-master-exclude-private '!git merge --no-commit --no-ff master && (git diff --name-only HEAD..master | grep -f private_files | while read f; do git reset HEAD -- "$f"; rm -f "$f"; done; git commit -m "Merge master, excluding private files.")'
    

    Then edit the private_files file and add file patterns that are private; for example secret_file.*$. You could replace private_files in the alias with "$(git rev-parse --show-toplevel)"/private_files to read private_files from the top-level directory.

    Use git merge-master-exclude-private to do the merge. This will execute a non-fast-forward merge without committing, find files matching patterns in the private_files file, reset the index of any private files found, remove private files in the working directory, then commit. This should handle files that have whitespace in their names.

    If you don't want to do the commit, giving you a chance to edit the commit message, remove -m "Merge master, excluding private files." from the alias.

    0 讨论(0)
  • 2020-12-01 08:22

    Create 2 branches. The one branch that has the private files will not be pushed to the public repo. After a merge, restore the files in question with git checkout HEAD^ -- files that should not have been merged, rm other files, git add -A and git commit --amend -C HEAD. I'm not sure what the difference in the files in question is but you get the idea. Make a small script for this and you're good to go. You could even commit a sensitive file list that you commit at the root and the script could act off of that.

    0 讨论(0)
提交回复
热议问题