简体   繁体   中英

How to mirror a devops repository to GitHub, but exclude 1 folder

I have a private Microsoft Azure-Devops/TFS Pipeline. This pipeline builds my code and also mirrors it's content to a public github repository.

Now I'd like the pipeline to not mirror some folders, since I want to keep these folders private. I can't just change the git-ignore files and I fail to build a script that deletes the folder before commiting it to github.

So how do I handle this type of problem? Is there any way to pull-push a repository, excluding one specific folder, without the gitignore?

- task: CmdLine@2
  inputs:
    script: 'git pull https://$(githubpersonaltoken)@github.com/MyRepo/MyFile.git master'

- task: DeleteFiles@1
  displayName: 'Delete folder'
  inputs:
    SourceFolder:
    Contents: '.deletedFolderName*'
    RemoveSourceFolder: true


- task: CmdLine@2
  inputs:
    script: 'git push https://$(githubpersonaltoken)@github.com/MyRepo/MyFile.git head:master'

Expected result: Changes in "deletedFolderName" would not be mirrored by the push task. Result: Everything is pushed.

Create a new branch that includes the folder, remove/purge everything else, then don't ever push this branch . There are various methods to prevent accidental pushing of branches - here's one specific to GitHub

#create new branch
git checkout -b SecretSauce

#optional - track only "SecretFolder", ignore everything else via .gitignore
echo '/*' > .gitignore
echo '!.git*' >> .gitignore
echo '!/Secret Folder' >> .gitignore

#apply standard git ignore behavior only to current index, not working directory (--cached)
#if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
git add *
git commit -m "ignored everything but SecretFolder"

If the folder in question is now/ever was contained in the repository, and that repo was subsequently pushed to GitHub, the folder is now accessible via GitHub revision history, in whatever state it was at the time it was included/pushed. You can see this for yourself - from the GitHub repo, click 'commits', and then click the < > next to a commit that might've included your folder. This allows you to browse the repository as it appeared for that commit. If this is a problem, refer to this answer .

For each branch, will need to git checkout branch and then modify the /.git/info/exclude file to match the .gitignore file as it appears in the respective branch, run the git filter-branch command without the --all switch, and then revert /.git/info/exclude afterward, to ensure this procedure properly applies the desired ignorations (different per-branch)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM