简体   繁体   中英

How to create local git branch with remote tracking in VS Code

When pulling from a private git server, whenever I make a branch in VS Code it doesn't track the upstream branch. The git output shows that it's running

git checkout -q -b NewBranchLocal --no-track origin/NewBranch

if I use ">Git: Checkout to..." or ">Git: Create branch from..." command.

Is there a best practive for pulling these from a git server? How can I use the git extension in VS Code to create local versions of origin branches and tie it to that upstream branch?

From what I can see in the source code for VS Code, it looks like the --no-track should only show up if the branch you're creating is being checked out, but I don't see an option where you can create a branch and not check it out. Looking in the vscode-master/extensions/git/src folder, git.ts (line 1238) says

    async branch(name: string, checkout: boolean, ref?: string): Promise<void> {
        const args = checkout ? ['checkout', '-q', '-b', name, '--no-track'] : ['branch', '-q', name];

        if (ref) {
            args.push(ref);
        }

        await this.run(args);
    }

The only reference is in repository.ts (line 979):

    async branch(name: string, _checkout: boolean, _ref?: string): Promise<void> {
        await this.run(Operation.Branch, () => this.repository.branch(name, _checkout, _ref));
    }

This is called in the commands.ts file (line 1563)

    private async _branch(repository: Repository, defaultName?: string, from = false): Promise<void> {
        const branchName = await this.promptForBranchName(defaultName);

        if (!branchName) {
            return;
        }

        let target = 'HEAD';

        if (from) {
            const picks = [new HEADItem(repository), ...createCheckoutItems(repository)];
            const placeHolder = localize('select a ref to create a new branch from', 'Select a ref to create the \'{0}\' branch from', branchName);
            const choice = await window.showQuickPick(picks, { placeHolder });

            if (!choice) {
                return;
            }

            target = choice.label;
        }

        await repository.branch(branchName, true, target);
    }

so there's no ability to not check out that branch, and no chance to have the new, local branch linked to the origin.

I couldn't find any other references in the vscode-master project, so I'm stumped as to where to look next.

According to this VS Code issue , the Git: Checkout to... command will show you a list of branches. If you pick a remote branch, it will create a local branch and set it to track the remote one. I just tested this with VS Code 1.47.3 and it created the local branch with tracking. Perhaps they fixed this after you posted the question here?

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