diff --git a/modules/git/repo.go b/modules/git/repo.go index 579accf92e..046d639c42 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -186,6 +186,7 @@ func Clone(ctx context.Context, from, to string, opts CloneRepoOptions) error { // PushOptions options when push to remote type PushOptions struct { Remote string + LocalBranch string Branch string Force bool ForceWithLease string @@ -207,7 +208,13 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error { } remoteBranchArgs := []string{opts.Remote} if len(opts.Branch) > 0 { - remoteBranchArgs = append(remoteBranchArgs, opts.Branch) + var refspec string + if opts.LocalBranch != "" { + refspec = fmt.Sprintf("%s:%s", opts.LocalBranch, opts.Branch) + } else { + refspec = opts.Branch + } + remoteBranchArgs = append(remoteBranchArgs, refspec) } cmd.AddDashesAndList(remoteBranchArgs...) diff --git a/services/repository/init.go b/services/repository/init.go index 8b9081b37e..733528673c 100644 --- a/services/repository/init.go +++ b/services/repository/init.go @@ -74,6 +74,7 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi } if err := gitrepo.PushFromLocal(ctx, tmpPath, repo, git.PushOptions{ + LocalBranch: "HEAD", Branch: defaultBranch, Env: repo_module.InternalPushingEnvironment(u, repo), }); err != nil {