简体   繁体   中英

Using git, can I checkout a branch in Visual Studio without deleting the old branch?

I'm new to git, and we're just now switching from TFS 2010 to git. We created a new master branch and 3 subsidiary branches off of the master on our remote server. I installed the VS plugin and cloned the remote repository, which downloaded all of the master files into the local repository.

Now, when I make a new local branch off of one of the other remote branches, it deletes all of the local files for the master branch and replaces them with the files for the newly checked out branch. When I checkout master again, it again deletes the new branch locally and replaces it with the master branch.

Is there a way to retain the local files so that switching between branches happens quickly and doesn't have to re-download every file all the time?

Unlike in TFVC, where only the branches that you want are downloaded, Git downloads the whole repository to your local disk. From this repository it creates the working folder with the files at the version/branch you checked out.

When you switch from one branch to another, git replaces only the files that are different from its local cache. No files are transferred when switching.

This has the impact though that, unlike in TFVC, it's not possible to have two branches open at the same time in the same repository (TFVC workspace), instead you'd need to clone the repository again (you can make a local copy of the whole repo) and check out a different branch in your second repository. The same was true in TFVC when you wanted the same branch a two different versions. In order to do that you'd have to create a second workspace.

The underlying reason for this all is that, in git, a branch is nothing more than a pointer to a version. and switching between branches is the same as switching between versions. And because Git has a local copy of all the changes ever made in the repo, switching is fast and efficient (up to a certain size repo of course).

通常git已经在本地拥有所有分支,因此如果签出了分支,则没有其他下载。

When you clone a repository, all the history with all the branches and all the files are downloaded and put, with some other data, in a '.git' folder.

When you check out a branch, the workspace is updated with the local files (no download!)

Normally, a check out is quite quick. If that's not the case, you have a problem with the files that you put in it.

Unlike TFVC, you must not commit binary files or big files. My advice is that you should clean your history ( surely using 'BFG repo cleaner')

If that's not really possible, perhaps you should use 'git-lfs' (supported by Tfs and other git servers).

But, in short term, perhaps you could have a look at 'git worktree' if you want to check out 2 branches in 2 different directories like what you did with TFVC...

But it will not help you get the real power of git :-(

Branches in git are not deleted, if you do checkout another one. Like stated in the above comments, the documentation mentioned in working/staging/commit do explain this pretty nicely.

Git is distributed, and therefor all Branches are stored on each machine, where you have fetched/pulled it. The whole history of the repository is stored on your machine.

Obviously there is a way to delete Branches (local as well as remote), but this is not part of the current question.

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