Skip to content

How to Remove Files from Git Repository without Deleting Local Copies

A Guide to removing files from Git repositories without deleting local copies. How to remove files from fit repository without deleting local copies

Steps
6
Covers
Git
Versions & support
About Git →
On this page 6 steps

In certain situations, you may want to remove a file from your Git repository but keep the local copy of the file. This could be useful when dealing with sensitive information, large files, or configuration files that need to be customized per user. In this article, we will walk you through the process of removing a file from your Git repository while keeping the local version intact.

Step 1 of 6Open the terminal

Begin by opening your terminal window. You can use the default terminal application for your operating system or any other terminal emulator you prefer.

Step 2 of 6Navigate to your Git repository

Using the terminal, navigate to the root directory of your Git repository. You can do this using the cd command followed by the path to your repository:

cd path/to/your/repository 

Step 3 of 6Remove the file from the Git repository

To remove the file from your Git repository without deleting the local copy, use the git rm command with the --cached option:

git rm --cached file_to_remove.txt 

Replace file_to_remove.txt with the relative path to the file you want to remove from the repository. The --cached option tells Git to remove the file only from the repository and not from the local file system.

Step 4 of 6Commit the changes

After removing the file from the Git repository, commit the changes to record the removal of the file:

git commit -m "Removed file_to_remove.txt from the repository" 

Replace the commit message with an appropriate description of the change.

Step 5 of 6Add the file to .gitignore (optional)

If you want to prevent the file from being accidentally added to the repository in the future, you can add the file to your .gitignore file. If you don’t have a .gitignore file in your repository, create one using the following command:

touch .gitignore 

Then, open the .gitignore file using your preferred text editor and add the relative path to the file you removed:

Save and close the .gitignore file.

Step 6 of 6Commit the .gitignore changes (optional)

After updating the .gitignore file, stage and commit the changes:

git add .gitignore 
git commit -m "Updated .gitignore to exclude file_to_remove.txt" 

Replace the commit message with an appropriate description of the change.

Conclusion

By following these steps, you have successfully removed a file from your Git repository while keeping the local version of the file. This can be useful for managing sensitive data, large files, or configuration files that need to be customized per user. Keep in mind that if the file was previously tracked by Git, its history will still be available in the repository. To completely remove the file and its history, you may need to use a more advanced method, such as git filter-branch or the BFG Repo-Cleaner.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top