简体   繁体   中英

why can see right files after git checkout -b $local_branch based on git checkout remotes/origin/remote_branch

What I did is

  1. git fetch to get all remote branch meta data,

  2. git checkout remotes/origin/$remote_branch

  3. git checkout -b $local_branch

When I brow files in Intellij, all files on remote branch show correctly.
Which did the magic to pull the remote files?

Which did the magic to pull the remote files?

Your second checkout did create a local branch based on your current HEAD, which was origin/$remote_branch.

But the right command would be to use the new command git switch ( presented here )

git switch -c $local_branch origin/$remote_branch

You're starting from a bad assumption: git fetch does not merely get metadata , it gets commits . The commits contain both data—snapshots of files—and metadata: information about who made the commit, when, and so on. Having obtained any new commits required, your git fetch then goes on to update your remote-tracking names , which are the origin/* names. Their full names are refs/remotes/origin/ name , where each name corresponds to a branch name as seen in the other Git, over at origin .

When you run git checkout remotes/origin/ branch , your Git puts you in detached HEAD mode with that one specific commit checked out. So this step updated the files you can see in your working tree. The git checkout -b name step created a new name that identifies this same commit—the same commit that refs/remotes/origin/ branch identifies—and no files need updating.

It's the commits that matter. The names, whether they are remote-tracking names like origin/somebranch or (local) branch names like somebranch , merely help you (and Git) find the commits. The commits contain the files, and are the history. The git fetch step obtains, from some other Git repository, any new commits that they have, that you don't, that you need; from then on, everything you do here is local.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM