Search code examples
gitgit-branchrm

Will git rm -f <file> remove the file from another branch


I work in many branches and have pulled a file from one branch (branchA) to the current one (branchB).

I used git checkout file.name

I would like to remove file.name from branchB without modifying branchA. Will git rm -f file.name remove the file from just branchB or will it remove it from branchA as well?


Solution

  • Strictly speaking, running

    git rm -f file.name
    

    does not affect any branch, yet. This command merely

    • removes the file from your working tree, and
    • stages the removal of file.name (in other words, it writes, in the index, that file.name should no longer be tracked.)

    Then, when you create a commit, the tree object of the current branch's new tip will no longer reference file.name.

    And because staging and committing only affect the current branch, the other branches (if any) will not be affected by this removal. If you later check another branch (to which file.name was committed), file.name will be checked out to your working tree.