Search code examples
gitdeploymentbitbucketpleskcontinuous-deployment

How to commit only a specific directory to a remote branch? (deleting all other content)


I have a branch named deployment and I want to push only the contents of the folder dist to this branch in my remote repository, deleting all other content.

I have tried so far

  • Flag git push with --prefix
  • cd dist and git push

Observations

  1. A warning that says it is already up-to-date
  2. Remote has everything in there (and not just the 'dist' folder)

How do I select only dist/ folder in my commit?


Solution

  • As I understand, you want to

    Commit only a specific directory to a remote branch (deleting all other content)

    Since deployment is created (branched out) from a branch that has all the files, you will need to delete unwanted files in the deployment branch. You can get to the desired state with the following steps:

    1. Pull the latest changes: git reset --hard HEAD; git fetch
    2. Go to your branch: git checkout deployment
    3. Delete unwanted files/directories (other than dist/): find * -maxdepth 0 -name 'dist' -prune -o -exec rm -rf '{}' ';'
    4. Stage deleted changes: git add .
    5. Commit & push to remote: git commit -m "Commit message"; git push origin deployment

    Hope it helps!