如何将新的本地分支推送到远程 Git 存储库并对其进行跟踪?
如何:
-
从另一个分支创建本地分支(通过
gitbranch
或git checkout -b
)。 -
推送本地分支 到远程存储库(即发布),但使其 可跟踪,以便
git pull
和git Push
可以工作。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
在 Git 1.7.0 及更高版本中,您可以签出新分支:
编辑文件、添加和提交。然后使用
-u
(--set-的缩写)进行推送upper
) 选项:Git 将在推送过程中设置跟踪信息。
In Git 1.7.0 and later, you can checkout a new branch:
Edit files, add and commit. Then push with the
-u
(short for--set-upstream
) option:Git will set up the tracking information during the push.
如果您不与其他人共享您的存储库,这对于将您的所有分支推送到远程非常有用,并为您正确
--set-upstream
跟踪:(不完全是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:(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.
在引入
git push -u
之前,没有git Push
选项来获取您想要的内容。您必须添加新的配置语句。如果您使用以下方式创建新分支:
您可以使用 git config 命令来避免直接编辑 .git/config 文件:
或者您可以手动编辑 .git/config 文件。 git/config 文件将跟踪信息添加到此分支:
Prior to the introduction of
git push -u
, there was nogit push
option to obtain what you desire. You had to add new configuration statements.If you create a new branch using:
You can use the
git config
command to avoid editing directly the.git/config
file:Or you can edit manually the
.git/config
file to add tracking information to this branch:简而言之,要创建一个新的本地分支,请执行以下操作:
要将其推送到远程存储库,请执行以下操作:
Simply put, to create a new local branch, do:
To push it to the remote repository, do:
此处已给出的解决方案略有不同:
基于其他(远程或本地)分支创建本地分支:
将本地分支推送到远程存储库(发布),但使其可跟踪,以便
git pull 和
git push
将立即生效使用
HEAD
是一种“将当前分支推送到远程相同名称的便捷方法”。来源:https://git-scm.com/docs/git-push在 Git 术语中,HEAD(大写)是对当前分支(树)顶部的引用。
-u
选项只是--set-upstream
的缩写。这将为当前分支添加上游跟踪参考。您可以通过查看 .git/config 文件来验证这一点:A slight variation of the solutions already given here:
Create a local branch based on some other (remote or local) branch:
Push the local branch to the remote repository (publish), but make it trackable so
git pull
andgit push
will work immediatelyUsing
HEAD
is a "handy way to push the current branch to the same name on the remote". Source: https://git-scm.com/docs/git-pushIn 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:我只是简单地
重复一个已经克隆的项目。
Git 在我在
localBranch
中所做的提交下创建了一个名为remoteBranchToBeCreated
的新分支。编辑:这会将您当前的本地分支(可能名为
localBranch
)上游更改为origin/remoteBranchToBeCreated
。要解决此问题,只需键入:或
因此您当前的本地分支现在会跟踪
origin/localBranch
。I simply do
over an already cloned project.
Git creates a new branch named
remoteBranchToBeCreated
under my commits I did inlocalBranch
.Edit: this changes your current local branch's (possibly named
localBranch
) upstream toorigin/remoteBranchToBeCreated
. To fix that, simply type:or
So your current local branch now tracks
origin/localBranch
back.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>
edit 已过时,只需使用
git push -u origin $BRANCHNAME
使用
gitpublish-branch
来自 William 的各种 Git 工具。好吧,没有 Ruby,所以 - 忽略安全措施! - 获取脚本的最后三行并创建一个bash脚本,
git-publish-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
:Then run
git-publish-branch REMOTENAME BRANCHNAME
, where REMOTENAME is usually origin (you may modify the script to take origin as default, etc...)将本地更改推送到新功能分支的完整 Git 工作流程如下所示
拉取所有远程分支
立即列出所有分支
签出或创建分支(将
替换为您的分支名称):显示当前分支。必须在其前面显示 *
添加您的本地更改(此处是故意的)
现在提交您的更改:
重要:从 master 获取更新:
现在推送您的本地更改:
Complete Git work flow for pushing local changes to anew feature branch looks like this
Pull all remote branches
List all branches now
Checkout or create branch(replace
<feature branch>
with your branch name):shows current branch. Must show with * In front of it
Add your local changes (. is on purpose here)
Now commit your changes:
Important: Take update from master:
Now push your local changes:
我想您已经克隆了一个项目,例如:
然后在本地副本中创建一个新分支并检查它:
假设您在服务器上创建了“git bare --init”并创建了 myapp.git,您应该:
之后,用户应该能够
注意:我假设您的服务器已启动并运行。如果不是,那就行不通。 此处有一个很好的操作方法。
添加
添加远程分支:
检查一切是否正常(获取源并列出远程分支):
创建本地分支并跟踪远程分支:
更新所有内容:
I suppose that you have already cloned a project like:
Then in your local copy, create a new branch and check it out:
Supposing that you made a "git bare --init" on your server and created the myapp.git, you should:
After that, users should be able to
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:
Check if everything is good (fetch origin and list remote branches):
Create a local branch and track the remote branch:
Update everything:
通过从现有分支分支来创建新分支
,然后使用将此新分支推送到存储库
这将创建并将所有本地提交推送到新创建的远程分支
origin/
To create a new branch by branching off from an existing branch
and then push this new branch to repository using
This creates and pushes all local commits to a newly created remote branch
origin/<new_branch>
对于 1.7 之前的 GitLab 版本,请使用:
(name_branch, ex:
master
)要将其推送到远程存储库,请执行:
(name_new_branch, example:
feature
)For GitLab version prior to 1.7, use:
(name_branch, ex:
master
)To push it to the remote repository, do:
(name_new_branch, example:
feature
)我创建了一个别名,以便每当我创建新分支时,它都会相应地推送和跟踪远程分支。我将以下块放入
.bash_profile
文件中:用法:只需键入
gcb thuy/do-sth-kool
和thuy/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:Usage: just type
gcb thuy/do-sth-kool
withthuy/do-sth-kool
is my new branch name.您可以分两步完成:
1.使用
checkout
创建本地分支:根据需要使用您的分支。
2. 使用
push
命令自动创建分支并将代码发送到远程存储库:You can do it in 2 steeps:
1. Use the
checkout
for create the local branch:Work with your Branch as you want.
2. Use the
push
command to autocreate the branch and send the code to the remote repository:我认为这是最简单的别名,添加到您的
~/.gitconfig
您只需运行
......它就会发布分支
I think this is the simplest alias, add to your
~/.gitconfig
You just run
and... it publishes the branch
稍微基于此处的答案,我将这个过程包装为一个简单的 Bash 脚本,当然也可以用作 Git 别名。
对我来说重要的补充是,这会提示我在提交之前运行单元测试,并默认传递当前分支名称。
git_push_new_branch.sh
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
为了获得最大的灵活性,您可以使用自定义 Git 命令。例如,在
$PATH
中的某个位置以git-publish
名称创建以下 Python 脚本并使其可执行:然后
gitpublish -h
将向您显示使用信息:For greatest flexibility, you could use a custom Git command. For example, create the following Python script somewhere in your
$PATH
under the namegit-publish
and make it executable:Then
git publish -h
will show you usage information:现在可以(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如果您希望远程和本地分支具有相同的名称并且不想手动输入分支名称
In case you want the remote and local branch to have the same name and don't want to enter the branch name manually
如果您想将本地存储库推送到原始存储库,请在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>