2

Ok, when I switch between branches, and delete a file from one branch it is carried over into the other.

I created a new repo, and pushed to a remote with a file "README.md".

I then switch to a feature branch git checkout -b feature/hello and add a file hello.md. I add this file git add hello.md and commit git commit -m "hello added". I then push this branch and everything is great! git push origin feature/hello.

Now I want to switch branch to do some work, and on this feature branch we are going to remove hello.md and add a new file goodbye.md.

  • git checkout -b feature/goodbye
  • rm hello.md
  • git checkout feature/hello

Now in the feature/hello branch I have the files README.md goodbye.md.

Why does deleting files in one branch remove them from another?

Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28
WishIHadThreeGuns
  • 1,225
  • 3
  • 17
  • 37
  • 1
    It's not deleting the file. You've deleted the file in your working directory then switched branches, the two things aren't really related. If you want to revert that change you need to reset your working directory. Just run `git reset` – Liam Dec 17 '20 at 08:39
  • 2
    you do need to commit the change before checking out the feature/hello branch. – Nemeas Dec 17 '20 at 08:52
  • Committing wouldn't make much difference. I could Git Stash, but by not committing I still show the problem in this toy version of the problem. – WishIHadThreeGuns Dec 17 '20 at 08:54
  • 1
    See also [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/q/22053757/1256452). – torek Dec 17 '20 at 09:29
  • https://stackoverflow.com/a/246298/7976758 – phd Dec 17 '20 at 10:20

1 Answers1

3

TL;DR - changes are local, until you commit them.

The file hasn't been removed from the "branch" per se, but rather, its only been removed locally.

When you do rm [filename], you are only removing the file locally. This is different from git rm [filename]. Some more details in this post.

If you check the code you pushed, the file would still be there. In fact, the file is still in the branch -- at least a far as git is concerned, since you haven't committed the deletion as a change yet. Its only when you git add [filename] that git would record the file as deleted. (In fact, even then, you can still change branches and git commit the file as deleted in a different branch!)

This is just the way git works. You can make changes locally and move between different branches, and then commit the changes to a branch of your choice. (Of course, you may encounter some conflicts along the way, but in principle this works).

In fact, its actually really useful. Say, you are in branch foo and you've just made some changes to a few files. But, what you actually wanted to do was make those changes in a new branch bar which you forgot to create & checkout. Since the changes are still local, you can simply git checkout -b bar and commit the changes in branch bar as you originally intended.

By the way, this post talks about ways to get the deleted file back, in case the rm was a mistake.

costaparas
  • 5,047
  • 11
  • 16
  • 26