DEV Community

Cover image for How to remove a folder after pushing it to Github
Grace-00
Grace-00

Posted on

How to remove a folder after pushing it to Github

Let’s say you push a folder to Github but the files within it were not meant to be shared. What do you do?

After panic settles in, here are the steps to follow:

First of all, to avoid this altogether, it’s always better to create and add specific files not meant to be shared via the .𝚐𝚒𝚝𝚒𝚐𝚗𝚘𝚛𝚎 file.

To remove the folder and its contents from Github, use [𝚗𝚊𝚖𝚎 𝚘𝚏 𝚏𝚘𝚕𝚍𝚎𝚛]/

if the damage is already done, you can still proceed with the step above and push it to Github, but then if you check previous commits, you’ll see that the folder and its contents are still there.

How to remove them?

Use these commands:

𝚐𝚒𝚝 𝚏𝚒𝚕𝚝𝚎𝚛-𝚋𝚛𝚊𝚗𝚌𝚑 --𝚒𝚗𝚍𝚎𝚡-𝚏𝚒𝚕𝚝𝚎𝚛 '𝚐𝚒𝚝 𝚛𝚖 --𝚌𝚊𝚌𝚑𝚎𝚍 -𝚛 --𝚒𝚐𝚗𝚘𝚛𝚎-𝚞𝚗𝚖𝚊𝚝𝚌𝚑 𝙰𝙱𝙲' \ --𝚙𝚛𝚞𝚗𝚎-𝚎𝚖𝚙𝚝𝚢 --𝚝𝚊𝚐-𝚗𝚊𝚖𝚎-𝚏𝚒𝚕𝚝𝚎𝚛 𝚌𝚊𝚝 -- --𝚊𝚕𝚕

Assuming you are on the main branch, you should get this result as last line:

𝚁𝚎𝚏 '𝚛𝚎𝚏𝚜/𝚑𝚎𝚊𝚍𝚜/𝚖𝚊𝚒𝚗' 𝚠𝚊𝚜 𝚛𝚎𝚠𝚛𝚒𝚝𝚝𝚎𝚗

Then you can update the interested branch with:

𝚐𝚒𝚝 𝚞𝚙𝚍𝚊𝚝𝚎-𝚛𝚎𝚏 -𝚍 𝚛𝚎𝚏𝚜/𝚘𝚛𝚒𝚐𝚒𝚗𝚊𝚕/𝚛𝚎𝚏𝚜/𝚑𝚎𝚊𝚍𝚜/𝚖𝚊𝚒𝚗

and you can then push the results with 𝚐𝚒𝚝 𝚙𝚞𝚜𝚑 —𝚏𝚘𝚛𝚌𝚎

Use —𝚏𝚘𝚛𝚌𝚎 in case of conflicts between past and present branch.

This will change your git history deleting all references to the previous added folder and maintain the folder within the local structure of your project.

Oldest comments (0)