Skip to content

Commit d18068c

Browse files
committed
Closes #4477 avoid set upstream unless names match
1 parent 59241cf commit d18068c

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- Changes branch creation to avoid setting an upstream branch if the new branch name and remote branch name don't match ([#4477](https://github.com/gitkraken/vscode-gitlens/issues/4477))
12+
913
## [17.3.0] - 2025-07-08
1014

1115
### Added

src/commands/git/branch.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,23 @@ export class BranchGitCommand extends QuickCommand {
379379
state.reference = result;
380380
}
381381

382+
const isRemoteBranch = isBranchReference(state.reference) && state.reference.remote;
383+
const remoteBranchName = isRemoteBranch ? getReferenceNameWithoutRemote(state.reference) : undefined;
384+
382385
if (state.counter < 4 || state.name == null || state.suggestNameOnly) {
386+
let value: string | undefined = state.name;
387+
// if it's a remote branch, pre-fill the name (if it doesn't already exist)
388+
if (!state.name && isRemoteBranch && !(await state.repo.git.branches.getBranch(remoteBranchName))) {
389+
value = remoteBranchName;
390+
}
391+
383392
const result = yield* inputBranchNameStep(state, context, {
384393
title: `${context.title} from ${getReferenceLabel(state.reference, {
385394
capitalize: true,
386395
icon: false,
387396
label: state.reference.refType !== 'branch',
388397
})}`,
389-
value:
390-
state.name ?? // if it's a remote branch, pre-fill the name
391-
(isBranchReference(state.reference) && state.reference.remote
392-
? getReferenceNameWithoutRemote(state.reference)
393-
: undefined),
398+
value: value,
394399
});
395400
if (result === StepResultBreak) continue;
396401

@@ -430,7 +435,11 @@ export class BranchGitCommand extends QuickCommand {
430435
await state.repo.switch(state.reference.ref, { createBranch: state.name });
431436
} else {
432437
try {
433-
await state.repo.git.branches.createBranch?.(state.name, state.reference.ref);
438+
await state.repo.git.branches.createBranch?.(
439+
state.name,
440+
state.reference.ref,
441+
isRemoteBranch && state.name !== remoteBranchName ? { noTracking: true } : undefined,
442+
);
434443
} catch (ex) {
435444
Logger.error(ex, context.title);
436445
// TODO likely need some better error handling here

src/env/node/git/sub-providers/branches.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,10 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
441441
}
442442

443443
@log()
444-
async createBranch(repoPath: string, name: string, sha: string): Promise<void> {
444+
async createBranch(repoPath: string, name: string, sha: string, options?: { noTracking?: boolean }): Promise<void> {
445445
try {
446-
await this.git.branch(repoPath, name, sha);
446+
const args = options?.noTracking ? ['--no-track'] : [];
447+
await this.git.branch(repoPath, name, sha, ...args);
447448
} catch (ex) {
448449
if (ex instanceof BranchError) {
449450
throw ex.update({ branch: name, action: 'create' });

src/git/gitProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export interface GitBranchesSubProvider {
249249
cancellation?: CancellationToken,
250250
): Promise<string | undefined>;
251251

252-
createBranch?(repoPath: string, name: string, sha: string): Promise<void>;
252+
createBranch?(repoPath: string, name: string, sha: string, options?: { noTracking?: boolean }): Promise<void>;
253253
deleteLocalBranch?(repoPath: string, name: string, options?: { force?: boolean }): Promise<void>;
254254
deleteRemoteBranch?(repoPath: string, name: string, remote: string): Promise<void>;
255255
/**

0 commit comments

Comments
 (0)