kroe761
kroe761

Reputation: 3534

Remove a file from git, but keep it locally on git pull?

We have some project configuration files that are checked into our git repos that we want to remove. however, when someone else run a git pull, we don't want git to remove them from their local system. We're just trying to fix the problem of having them in the repo in the first place. We tried this:

git rm --cached file
echo "file" >> .gitignore
git add .gitignore
git commit -m "removing files from git"
git push

But when someone else pulled from master the files were deleted from their local instance. Thank you!

Upvotes: 0

Views: 26

Answers (1)

matt
matt

Reputation: 535316

What you are looking at in your working directory is the git working tree. These are the files and folders that you are able to see. They do not belong to you! They belong to Git. Basically, the rule is that every time you do a git checkout, Git makes the index (staging area) and work tree look like what you just checked out, so that they are all in sync.

Ok, so git pull is git merge. And git merge involves implicit checkout of a new commit — either the newly created merge commit, or (if fast-forwarding) the commit at the end of the remote-tracking branch. And any time you do a checkout, you should assume that the work tree will be completely rewritten to match what was just checked out.

Well, you committed a deletion — that is, you deleted a file, you added the fact of that deletion to the index, and you committed. That literally means that to check out this commit implies an absence of that file! So whoever checks out this commit will cause that deletion in the work tree, to match it. And that is exactly what is happening.

You can't prevent it. Git is just doing its job. If you want to protect against it, tell everyone to copy the work tree somewhere else before pulling, just in case. That way, they can bring back the configuration file manually.

Upvotes: 1

Related Questions