简体   繁体   中英

How do I remove a file from local git repository and local file system but not from remote repository?

I have been trying to delete a file from my local file system and local git repository.

I am using git rm --cached <file name>

But whenever I am working with another file and push the changes git also deletes the locally removed file from my remote repository. I want the file to stay on my GitHub.

I would really appreciate some help.

git update-index --skip-worktree <file name>

This is a little hacky, but it will allow you to edit (or delete) the file without it showing as changed.

You can easily revert this later, when needed:

git update-index --no-skip-worktree <file name>

Git Docs :

--[no-]skip-worktree

When one of these flags is specified, the object name recorded for the paths are not updated. Instead, these options set and unset the "skip-worktree" bit for the paths.

SKIP-WORKTREE BIT

Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

If you need a specific version for your local setup, and a different one for the remote, using git alone: you can create two branches.

Another possibility is to use language features to avoid including a part of the code.

With C++ files, this may be an #ifdef (or #ifndef ) section in the file, and a compile option which you would use on your local machine to exclude the content of the file.

When you remove a file locally and also update the index (staging area) by running git rm --cached <file name> , the deletion gets staged.

If you run git status after running git rm --cached <file name> , you will see the deletion has got staged for the next commit. Now if you make changes to other files and add those files for commit, the deletion is still staged. And when you finally commit and push changes, the deletion is done in the remote also.

However, if you still want to make this work anyways, don't stage the deletion .

Don't run git rm --cached <file name> . Just delete the file manually. That will do the job. However, it will always get listed in un-staged changes when you run git status like the below image.

在此处输入图像描述

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