如何将新的本地分支推送到远程 Git 存储库并对其进行跟踪?

发布于 2024-08-31 23:17:20 字数 211 浏览 11 评论 0 原文

如何:

  1. 从另一个分支创建本地分支(通过 gitbranchgit checkout -b)。

  2. 推送本地分支 到远程存储库(即发布),但使其 可跟踪,以便 git pullgit Push 可以工作。

How do I:

  1. Create a local branch from another branch (via git branch or git checkout -b).

  2. Push the local branch
    to the remote repository (i.e. publish), but make it
    trackable so that git pull and git push will work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

野却迷人 2024-09-07 23:17:20

在 Git 1.7.0 及更高版本中,您可以签出新分支:

git checkout -b <branch>

编辑文件、添加和提交。然后使用-u--set-的缩写)进行推送upper) 选项:

git push -u origin <branch>

Git 将在推送过程中设置跟踪信息。

In Git 1.7.0 and later, you can checkout a new branch:

git checkout -b <branch>

Edit files, add and commit. Then push with the -u (short for --set-upstream) option:

git push -u origin <branch>

Git will set up the tracking information during the push.

梓梦 2024-09-07 23:17:20

如果您不与其他人共享您的存储库,这对于将您的所有分支推送到远程非常有用,并为您正确--set-upstream跟踪:(

git push --all -u

不完全是OP 要求什么,但这句话非常受欢迎)

如果您与其他人共享您的存储库,这并不是一个很好的形式,因为您将用所有狡猾的实验分支堵塞存储库。

If you are not sharing your repo with others, this is useful to push all your branches to the remote, and --set-upstream tracking correctly for you:

git push --all -u

(Not exactly what the OP was asking for, but this one-liner is pretty popular)

If you are sharing your repo with others this isn't really good form as you will clog up the repo with all your dodgy experimental branches.

池木 2024-09-07 23:17:20

在引入 git push -u 之前,没有 git Push 选项来获取您想要的内容。您必须添加新的配置语句。

如果您使用以下方式创建新分支:

$ git checkout -b branchB
$ git push origin branchB:branchB

您可以使用 git config 命令来避免直接编辑 .git/config 文件:

$ git config branch.branchB.remote origin
$ git config branch.branchB.merge refs/heads/branchB

或者您可以手动编辑 .git/config 文件。 git/config 文件将跟踪信息添加到此分支:

[branch "branchB"]
    remote = origin
    merge = refs/heads/branchB

Prior to the introduction of git push -u, there was no git push option to obtain what you desire. You had to add new configuration statements.

If you create a new branch using:

$ git checkout -b branchB
$ git push origin branchB:branchB

You can use the git config command to avoid editing directly the .git/config file:

$ git config branch.branchB.remote origin
$ git config branch.branchB.merge refs/heads/branchB

Or you can edit manually the .git/config file to add tracking information to this branch:

[branch "branchB"]
    remote = origin
    merge = refs/heads/branchB
终止放荡 2024-09-07 23:17:20

简而言之,要创建一个新的本地分支,请执行以下操作:

git branch <branch-name>

要将其推送到远程存储库,请执行以下操作:

git push -u origin <branch-name>

Simply put, to create a new local branch, do:

git branch <branch-name>

To push it to the remote repository, do:

git push -u origin <branch-name>
贪恋 2024-09-07 23:17:20

此处已给出的解决方案略有不同:

  1. 基于其他(远程或本地)分支创建本地分支:

    git checkout -b 分支名称
    
  2. 将本地分支推送到远程存储库(发布),但使其可跟踪,以便git pullgit push 将立即生效

    git push -u origin HEAD
    

    使用 HEAD 是一种“将当前分支推送到远程相同名称的便捷方法”。来源:https://git-scm.com/docs/git-push
    在 Git 术语中,HEAD(大写)是对当前分支(树)顶部的引用。

    -u 选项只是 --set-upstream 的缩写。这将为当前分支添加上游跟踪参考。您可以通过查看 .git/config 文件来验证这一点:

    在此处输入图像描述

A slight variation of the solutions already given here:

  1. Create a local branch based on some other (remote or local) branch:

    git checkout -b branchname
    
  2. Push the local branch to the remote repository (publish), but make it trackable so git pull and git push will work immediately

    git push -u origin HEAD
    

    Using HEAD is a "handy way to push the current branch to the same name on the remote". Source: https://git-scm.com/docs/git-push
    In Git terms, HEAD (in uppercase) is a reference to the top of the current branch (tree).

    The -u option is just short for --set-upstream. This will add an upstream tracking reference for the current branch. you can verify this by looking in your .git/config file:

    Enter image description here

云雾 2024-09-07 23:17:20

我只是简单地

git push -u origin localBranch:remoteBranchToBeCreated

重复一个已经克隆的项目。

Git 在我在 localBranch 中所做的提交下创建了一个名为 remoteBranchToBeCreated 的新分支。

编辑:这会将您当前的本地分支(可能名为localBranch)上游更改为origin/remoteBranchToBeCreated。要解决此问题,只需键入:

git branch --set-upstream-to=origin/localBranch

git branch -u origin/localBranch

因此您当前的本地分支现在会跟踪 origin/localBranch

I simply do

git push -u origin localBranch:remoteBranchToBeCreated

over an already cloned project.

Git creates a new branch named remoteBranchToBeCreated under my commits I did in localBranch.

Edit: this changes your current local branch's (possibly named localBranch) upstream to origin/remoteBranchToBeCreated. To fix that, simply type:

git branch --set-upstream-to=origin/localBranch

or

git branch -u origin/localBranch

So your current local branch now tracks origin/localBranch back.

薯片软お妹 2024-09-07 23:17:20

git push --set-upstream origin <您的分支名称>

OR

git push -u origin <您的分支名称>

git push --set-upstream origin <your branch name>

OR

git push -u origin <your branch name>

故人爱我别走 2024-09-07 23:17:20

edit 已过时,只需使用 git push -u origin $BRANCHNAME


使用 gitpublish-branch 来自 William 的各种 Git 工具

好吧,没有 Ruby,所以 - 忽略安全措施! - 获取脚本的最后三行并创建一个bash脚本,git-publish-branch

#!/bin/bash
REMOTE=$1 # Rewrite this to make it optional...
BRANCH=$2
# Uncomment the following line to create BRANCH locally first
#git checkout -b ${BRANCH}
git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
git config branch.${BRANCH}.remote ${REMOTE} &&
git config branch.${BRANCH}.merge refs/heads/${BRANCH}

然后运行git-publish-branch REMOTENAME BRANCHNAME,其中REMOTENAME通常是origin (您可以修改脚本以将原点作为默认值,等等...)

edit Outdated, just use git push -u origin $BRANCHNAME


Use git publish-branch from William's miscellaneous Git tools.

OK, no Ruby, so - ignoring the safeguards! - take the last three lines of the script and create a bash script, git-publish-branch:

#!/bin/bash
REMOTE=$1 # Rewrite this to make it optional...
BRANCH=$2
# Uncomment the following line to create BRANCH locally first
#git checkout -b ${BRANCH}
git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
git config branch.${BRANCH}.remote ${REMOTE} &&
git config branch.${BRANCH}.merge refs/heads/${BRANCH}

Then run git-publish-branch REMOTENAME BRANCHNAME, where REMOTENAME is usually origin (you may modify the script to take origin as default, etc...)

寄离 2024-09-07 23:17:20

将本地更改推送到新功能分支的完整 Git 工作流程如下所示

拉取所有远程分支

git pull --all

立即列出所有分支

git branch -a  

签出或创建分支(将 替换为您的分支名称):

git checkout -b <feature branch>

显示当前分支。必须在其前面显示 *

git branch      

添加您的本地更改(此处是故意的)

git add .

现在提交您的更改:

git commit -m "Refactored/ Added Feature XYZ"

重要:从 master 获取更新:

git pull origin feature-branch

现在推送您的本地更改:

git push origin feature-branch

Complete Git work flow for pushing local changes to anew feature branch looks like this

Pull all remote branches

git pull --all

List all branches now

git branch -a  

Checkout or create branch(replace <feature branch> with your branch name):

git checkout -b <feature branch>

shows current branch. Must show with * In front of it

git branch      

Add your local changes (. is on purpose here)

git add .

Now commit your changes:

git commit -m "Refactored/ Added Feature XYZ"

Important: Take update from master:

git pull origin feature-branch

Now push your local changes:

git push origin feature-branch
月朦胧 2024-09-07 23:17:20

我想您已经克隆了一个项目,例如:

git clone http://github.com/myproject.git
  1. 然后在本地副本中创建一个新分支并检查它:

    git checkout -b ;
    
  2. 假设您在服务器上创建了“git bare --init”并创建了 myapp.git,您应该:

    git 远程添加源 ssh://example.com/var/git/myapp.git
    git推送原点大师
    
  3. 之后,用户应该能够

    git 克隆 http://example.com/var/git/myapp.git
    

注意:我假设您的服务器已启动并运行。如果不是,那就行不通。 此处有一个很好的操作方法。

添加

添加远程分支:

git push origin master:new_feature_name

检查一切是否正常(获取源并列出远程分支):

git fetch origin
git branch -r

创建本地分支并跟踪远程分支:

git checkout -tb new_feature_name origin/new_feature_name

更新所有内容:

git pull

I suppose that you have already cloned a project like:

git clone http://github.com/myproject.git
  1. Then in your local copy, create a new branch and check it out:

    git checkout -b <newbranch>
    
  2. Supposing that you made a "git bare --init" on your server and created the myapp.git, you should:

    git remote add origin ssh://example.com/var/git/myapp.git
    git push origin master
    
  3. After that, users should be able to

    git clone http://example.com/var/git/myapp.git
    

NOTE: I'm assuming that you have your server up and running. If it isn't, it won't work. A good how-to is here.

ADDED

Add a remote branch:

git push origin master:new_feature_name

Check if everything is good (fetch origin and list remote branches):

git fetch origin
git branch -r

Create a local branch and track the remote branch:

git checkout -tb new_feature_name origin/new_feature_name

Update everything:

git pull
离笑几人歌 2024-09-07 23:17:20

通过从现有分支分支来创建新分支

git checkout -b <new_branch>

,然后使用将此新分支推送到存储库

git push -u origin <new_branch>

这将创建并将所有本地提交推送到新创建的远程分支 origin/

To create a new branch by branching off from an existing branch

git checkout -b <new_branch>

and then push this new branch to repository using

git push -u origin <new_branch>

This creates and pushes all local commits to a newly created remote branch origin/<new_branch>

穿透光 2024-09-07 23:17:20

对于 1.7 之前的 GitLab 版本,请使用:

git checkout -b name_branch

(name_branch, ex: master)

要将其推送到远程存储库,请执行:

git push -u origin name_new_branch

(name_new_branch, example: feature)

For GitLab version prior to 1.7, use:

git checkout -b name_branch

(name_branch, ex: master)

To push it to the remote repository, do:

git push -u origin name_new_branch

(name_new_branch, example: feature)

傾城如夢未必闌珊 2024-09-07 23:17:20

我创建了一个别名,以便每当我创建新分支时,它都会相应地推送和跟踪远程分支。我将以下块放入 .bash_profile 文件中:

# Create a new branch, push to origin and track that remote branch
publishBranch() {
  git checkout -b $1
  git push -u origin $1
}
alias gcb=publishBranch

用法:只需键入 gcb thuy/do-sth-koolthuy/do -sth-kool 是我的新分支名称。

I made an alias so that whenever I create a new branch, it will push and track the remote branch accordingly. I put following chunk into the .bash_profile file:

# Create a new branch, push to origin and track that remote branch
publishBranch() {
  git checkout -b $1
  git push -u origin $1
}
alias gcb=publishBranch

Usage: just type gcb thuy/do-sth-kool with thuy/do-sth-kool is my new branch name.

≈。彩虹 2024-09-07 23:17:20

您可以分两步完成:

1.使用checkout创建本地分支:

git checkout -b yourBranchName

根据需要使用您的分支。

2. 使用push命令自动创建分支并将代码发送到远程存储库:

git push -u origin yourBanchName

有多种方法可以做到这一点,但我认为这种方法非常简单。

You can do it in 2 steeps:

1. Use the checkout for create the local branch:

git checkout -b yourBranchName

Work with your Branch as you want.

2. Use the push command to autocreate the branch and send the code to the remote repository:

git push -u origin yourBanchName

There are mutiple ways to do this but I think that this way is really simple.

往日 2024-09-07 23:17:20

我认为这是最简单的别名,添加到您的 ~/.gitconfig

[alias]
  publish-branch = !git push -u origin $(git rev-parse --abbrev-ref HEAD)

您只需运行

git publish-branch

......它就会发布分支

I think this is the simplest alias, add to your ~/.gitconfig

[alias]
  publish-branch = !git push -u origin $(git rev-parse --abbrev-ref HEAD)

You just run

git publish-branch

and... it publishes the branch

半衬遮猫 2024-09-07 23:17:20

稍微基于此处的答案,我将这个过程包装为一个简单的 Bash 脚本,当然也可以用作 Git 别名。

对我来说重要的补充是,这会提示我在提交之前运行单元测试,并默认传递当前分支名称。

$ git_push_new_branch.sh

  Have you run your unit tests yet? If so, pass OK or a branch name, and try again

  usage: git_push_new_branch {OK|BRANCH_NAME}

  e.g.

  git_push_new_branch           -> Displays prompt reminding you to run unit tests
  git_push_new_branch OK        -> Pushes the current branch as a new branch to the origin
  git_push_new_branch MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin

git_push_new_branch.sh

function show_help()
{
  IT=$(cat <<EOF

  Have you run your unit tests yet? If so, pass OK or a branch name, and try again

  usage: git_push_new_branch {OK|BRANCH_NAME}

  e.g.

  git_push_new_branch.sh           -> Displays prompt reminding you to run unit tests
  git_push_new_branch.sh OK        -> Pushes the current branch as a new branch to the origin
  git_push_new_branch.sh MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin

  )
  echo "$IT"
  exit
}

if [ -z "$1" ]
then
  show_help
fi

CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$1" == "OK" ]
then
  BRANCH=$CURR_BRANCH
else
  BRANCH=${1:-$CURR_BRANCH}
fi

git push -u origin $BRANCH

Building slightly upon the answers here, I've wrapped this process up as a simple Bash script, which could of course be used as a Git alias as well.

The important addition to me is that this prompts me to run unit tests before committing and passes in the current branch name by default.

$ git_push_new_branch.sh

  Have you run your unit tests yet? If so, pass OK or a branch name, and try again

  usage: git_push_new_branch {OK|BRANCH_NAME}

  e.g.

  git_push_new_branch           -> Displays prompt reminding you to run unit tests
  git_push_new_branch OK        -> Pushes the current branch as a new branch to the origin
  git_push_new_branch MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin

git_push_new_branch.sh

function show_help()
{
  IT=$(cat <<EOF

  Have you run your unit tests yet? If so, pass OK or a branch name, and try again

  usage: git_push_new_branch {OK|BRANCH_NAME}

  e.g.

  git_push_new_branch.sh           -> Displays prompt reminding you to run unit tests
  git_push_new_branch.sh OK        -> Pushes the current branch as a new branch to the origin
  git_push_new_branch.sh MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin

  )
  echo "$IT"
  exit
}

if [ -z "$1" ]
then
  show_help
fi

CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$1" == "OK" ]
then
  BRANCH=$CURR_BRANCH
else
  BRANCH=${1:-$CURR_BRANCH}
fi

git push -u origin $BRANCH
毁梦 2024-09-07 23:17:20

为了获得最大的灵活性,您可以使用自定义 Git 命令。例如,在 $PATH 中的某个位置以 git-publish 名称创建以下 Python 脚本并使其可执行:

#!/usr/bin/env python3

import argparse
import subprocess
import sys


def publish(args):
    return subprocess.run(['git', 'push', '--set-upstream', args.remote, args.branch]).returncode


def parse_args():
    parser = argparse.ArgumentParser(description='Push and set upstream for a branch')
    parser.add_argument('-r', '--remote', default='origin',
                        help="The remote name (default is 'origin')")
    parser.add_argument('-b', '--branch', help='The branch name (default is whatever HEAD is pointing to)',
                        default='HEAD')
    return parser.parse_args()


def main():
    args = parse_args()
    return publish(args)


if __name__ == '__main__':
    sys.exit(main())

然后 gitpublish -h将向您显示使用信息:

usage: git-publish [-h] [-r REMOTE] [-b BRANCH]

Push and set upstream for a branch

optional arguments:
  -h, --help            show this help message and exit
  -r REMOTE, --remote REMOTE
                        The remote name (default is 'origin')
  -b BRANCH, --branch BRANCH
                        The branch name (default is whatever HEAD is pointing to)

For greatest flexibility, you could use a custom Git command. For example, create the following Python script somewhere in your $PATH under the name git-publish and make it executable:

#!/usr/bin/env python3

import argparse
import subprocess
import sys


def publish(args):
    return subprocess.run(['git', 'push', '--set-upstream', args.remote, args.branch]).returncode


def parse_args():
    parser = argparse.ArgumentParser(description='Push and set upstream for a branch')
    parser.add_argument('-r', '--remote', default='origin',
                        help="The remote name (default is 'origin')")
    parser.add_argument('-b', '--branch', help='The branch name (default is whatever HEAD is pointing to)',
                        default='HEAD')
    return parser.parse_args()


def main():
    args = parse_args()
    return publish(args)


if __name__ == '__main__':
    sys.exit(main())

Then git publish -h will show you usage information:

usage: git-publish [-h] [-r REMOTE] [-b BRANCH]

Push and set upstream for a branch

optional arguments:
  -h, --help            show this help message and exit
  -r REMOTE, --remote REMOTE
                        The remote name (default is 'origin')
  -b BRANCH, --branch BRANCH
                        The branch name (default is whatever HEAD is pointing to)
鼻尖触碰 2024-09-07 23:17:20

现在可以(git 版本 2.37.0)设置 git config --global push.autoSetupRemote true 。另请参阅:使用 git 自动跟踪远程分支

It is now possible (git version 2.37.0) to set git config --global push.autoSetupRemote true. Also see: Automatically track remote branch with git

不甘平庸 2024-09-07 23:17:20

git push -u origin HEAD

如果您希望远程和本地分支具有相同的名称并且不想手动输入分支名称

git push -u origin HEAD

In case you want the remote and local branch to have the same name and don't want to enter the branch name manually

子栖 2024-09-07 23:17:20

如果您想将本地存储库推送到原始存储库,请在treminal中复制并粘贴下面的代码,但不要忘记更改分支名称:

git checkout -b <分支名称>

git push -u origin <分支名称>

if you want to push local repository to origin copy and paste code below in the treminal but dont forget to change name of brach:

git checkout -b <name of branch>

git push -u origin <name of branch>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文