Search code examples
gitgithubgit-fetch

What happens actually when I do git fetch and git checkout origin/branch from a local branch where new files are there?


Consider I have a local branch A and two remote branches A and B.
The local branch A contains some files which are not in remote B.
then, I do as follows:

git fetch
git checkout origin/B

then, those files from local A remains there, I removed those files and commit with my required changes in some files which were there in origin/B:

git add .
git commit -m "fixes made"

then, head got detached with showing the commit hash

So, I have two questions here

  1. Why those files from my local branch A copied/remained to/in origin/B?

  2. Why head got detached?


Solution

  • Why those files from my local branch A copied/remained to/in origin/B?

    Because they aren't files from branch A. They're files in your working directory that aren't managed by git. You didn't add them to any commit in branch A or anywhere else. And git doesn't do anything to files that it hasn't been told to manage. Work isn't "on a branch" until you commit it.

    Why head got detached?

    Because you did git checkout origin/B, which leaves HEAD detached. You probably meant git checkout -t origin/B, which creates a new branch B with an upstream of origin/B and points HEAD there.