Geindzo
Geindzo

Reputation: 47

How to make Git not remove files from local repo

Git deletes files when I checkout a branch

git checkout <myAnotherBranch>

All files that were committed and then checkout to a different branch were marked as deleted. But, if I checkout the branch again, all files come back to my local repo.

I also noticed that its behavior is not common for all branches in my repo, some of them still stay in my local repo even I checkout to another branch. How I can make git not to delete my files from branсh which I checkout?

Upvotes: 0

Views: 39

Answers (1)

VonC
VonC

Reputation: 1323833

First, this is what git checkout is supposed to do:

Updates files in the working tree to match the version in the index or the specified tree.

Since the index is updated to reflect the new branch HEAD, any versioned file not part said new branch would be removed.
Any private (not yet versioned) files would remain though, as Git does not clean up private files when switching branch, in order to not lose any work you have not added/committed yet.

Second, consider using git switch (Git 2.23+, Q3 2019), which only deals with branches. The effect will be the same, but there won't be any confusion with git checkout files vs. git checkout branch.

Upvotes: 2

Related Questions