Reputation: 1
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.
Upvotes: 0
Views: 125
Reputation: 852
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.
Upvotes: 0
Reputation: 9787
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>
--[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.
Upvotes: 1
Reputation: 51890
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.
Upvotes: 0