20

when I delete files from one branch it deletes from all branches. what can I do?

krdzo
  • 201
  • 1
  • 2
  • 3
  • to be able to see the exact problem it would help a lot if you could give the commands you executed (maybe creating a simple demo repository to exhibit the problem: `git init; touch bla; git add bla; git commit -m'bla'; git checkout -b A; git rm bla; …`) – knittl Apr 10 '11 at 17:04

2 Answers2

23

when deleting a file with git and persisting that project state (git commit) it will only be deleted in that commit and its children (speaking: in that branch). when merging that branch into another branch it might well be possible, that the file is going to be deleted (unless changed in the other branch).

when deleting a file, not committing it and then switching branches, git will apply your current set of changes to the other branch, in your case deleting the file there too. committing the delete should avoid the issue you are seeing

knittl
  • 246,190
  • 53
  • 318
  • 364
-4

No, it is not true. I think your confusion comes from switching between the branches. when you do "git checkout " it preserves your working tree so files you just deleted dont reappear after switching. It is possible to switch to different branch and reset all the changes you have done (you delete files). you can do this by for example writing ""git reset --hard" after switching.

Boris Bucha
  • 630
  • 4
  • 6
  • 1
    this is very bad advice. `git reset --hard` will move the current branch pointer to the specified commit. this is very different from what `git checkout` does, namely setting HEAD to a branch (or commit/tag) and checking out that tree, then reapplying the changes (if possible without textual merges) – knittl Apr 10 '11 at 17:05
  • ok, i agree that when talking about basic use case for newcomer it is a bit dangerous to mention "reset --hard". anyway i think that for demonstration purposes this is very good example to understand difference between what is working tree and what is branch (which was more or less question, right?). – Boris Bucha Apr 10 '11 at 17:49