But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name> (with the colon), or the remote branch will be deleted!
So that a subsequent git pull will know what to do, you might instead want to use:
# ... work ...
git commit
# ... work ...
git commit
git push origin HEAD:refs/heads/your_branch
或者跟踪分支以避免 git Push 的参数
git checkout --track -b your_branch origin/your_branch
# ... work ...
git commit
# ... work ...
git commit
git push
First, you must create your branch locally
git checkout -b your_branch
After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it
git push -u origin your_branch
Teammates can reach your branch, by doing:
git fetch
git checkout origin/your_branch
You can continue working in the branch and pushing whenever you want without passing arguments to git push (argumentless git push will push the master to remote master, your_branch local to remote your_branch, etc...)
git push
Teammates can push to your branch by doing commits and then push explicitly
# ... work ...
git commit
# ... work ...
git commit
git push origin HEAD:refs/heads/your_branch
Or tracking the branch to avoid the arguments to git push
git checkout --track -b your_branch origin/your_branch
# ... work ...
git commit
# ... work ...
git commit
git push
push.default = current - 推送当前分支以更新分支
接收端同名。适用于中央和非中央工作流程。
对我来说,这很好地简化了我的日常 Git 工作流程。配置设置负责“通常”的用例,即您在本地添加分支并希望远程创建它。此外,我只需执行 git co remote_branch_name 即可轻松地从远程创建本地分支(而不是使用 --set-upstream-to 标志)。
我知道这个问题和接受的答案相当旧,但行为已经改变,因此现在存在配置选项以使您的工作流程更简单。
要添加到全局 Git 配置,请在命令行上运行:
$ git config --global push.default current
Simple Git 2.0+ solution:
As of Git 2.0, the behavior has become simpler:
You can configure git with push.default = current to make life easier:
I added this so now I can just push a new branch upstream with
$ git push -u
-u will track remote branch of the same name. Now with this configuration, you will auto-guess the remote reference to git push. From git.config documentation:
push.default
Defines the action git push should take if no refspec is explicitly given.
push.default = current - push the current branch to update a branch with the
same name on the receiving end. Works in both central and non-central workflows.
For me, this is a good simplification of my day-to-day Git workflow. The configuration setting takes care of the 'usual' use case where you add a branch locally and want to create it remotely. Also, I can just as easily create local branches from remotes by just doing git co remote_branch_name (as opposed to using --set-upstream-to flag).
I know this question and the accepted answers are rather old, but the behavior has changed so that now configuration options exist to make your workflow simpler.
To add to your global Git configuration, run this on the command line:
这是将当前分支推送到 origin 上同名分支并跟踪它的快捷方式,这样您就不需要指定 origin HEAD 将来。
Create a new branch locally based on the current branch:
git checkout -b newbranch
Commit any changes as you normally would. Then, push it upstream:
git push -u origin HEAD
This is a shortcut to push the current branch to a branch of the same name on origin and track it so that you don't need to specify origin HEAD in the future.
最简单的解决方案... Drumm Roll... git version 2.10.1 (Apple Git-78)
1) git checkout -b localBranchNameThatDoesNotExistInRemote
2) Do your changes, and do a git commit
3) git push origin localBranchNameThatDoesNotExistInRemote --force
Easiest Solution... Drumm Roll... git version 2.10.1 (Apple Git-78)
1) git checkout -b localBranchNameThatDoesNotExistInRemote
2) Do your changes, and do a git commit
3) git push origin localBranchNameThatDoesNotExistInRemote --force
N.B. - The branch you just created in your local environment, and the remote non-existing branch where you are trying to push, must have the same name.
remote_name : git sets the name by default to be "origin" when it creates the repository. This can however be changed to a different arbitrary name.
local_branch_name : is the name of the local branch to be pushed.
remote_branch_name : is the name of the remote branch that we want to be created on remote.
If we remove the local and remote branch names, it will have the format
git push -u remote_name branch_name
This will push the local branch to remote and with the same name as the local branch branch_name. The local branch will be tracking the remote branch as well.
# create new branch and checkout that branch
git checkout -b myNewBranch
# now push branch to remote
git push origin myNewBranch
# set up the new branch to track remote branch from origin
git branch --set-upstream-to=origin/myNewBranch myNewBranch
I know this question is well answered, but just wanted to list the steps I take to create a new branch "myNewBranch" and push to remote ("origin" in my case) and set up tracking. Consider this the "TL;DR" version :)
# create new branch and checkout that branch
git checkout -b myNewBranch
# now push branch to remote
git push origin myNewBranch
# set up the new branch to track remote branch from origin
git branch --set-upstream-to=origin/myNewBranch myNewBranch
Creates a new branch, it also checks out that branch / makes it your current branch. If, for some reason, all you want to do is snap off a branch but not make it your current branch, then you would use the following command:
git branch {branchName}
In the first command, "checkout" makes said branch your current branch, and the "-b" means: this branch doesn't exist yet, so make it for me.
1: Open SourceTree, click on Repository -> Checkout
2: Click on Create New Branch
3: Select the branch where you want to get code for new branch
4: Give your branch name
5: Push the branch (by click on Push-button)
How to do through Source Tree
1: Open SourceTree, click on Repository -> Checkout
2: Click on Create New Branch
3: Select the branch where you want to get code for new branch
4: Give your branch name
5: Push the branch (by click on Push-button)
git push -u <remote-name> <branch-name> doesn't work if the newly created branch isn't spawned from the same repo, i.e. if you haven't created the new branch using git checkout -b new_branch, then this will not work.
For eg, I had cloned two different repositories locally and I had to copy repo2/branch1 to repo1/ and then push it too.
This link helped me push my local branch (cloned from another repo) to my remote repo:
Starting from Git 2.37.0, you do not need "--set-upstream origin" anymore. Just git push. You can achieve this with the push.autoSetupRemote option enabled
To create a new branch in remote, you can use the following command:
git checkout -b <new-branch-name>
For example, to create a new branch called "feature_branch", you would use the following command:
git checkout -b feature_branch
This will create a new branch locally, but it will not push it to the remote repository. To push the new branch to the remote repository, you can use the following command:
git push origin feature_branch
For example, to push the "feature_branch" branch to the remote repository, you would use the following command:
git push origin feature_branch
This will push the new branch to the remote repository, and it will be available to other users.
Here are some additional things to keep in mind about creating new branches in remote:
You can use the git branch -l command to list all of the branches in your local repository.
You can use the git checkout command to switch to a different branch.
You can use the git merge command to merge a branch into the current branch.
You can use the git reset --hard command to reset the current branch to the state of the specified branch.
Go the "Git Repository Exploring" view and expand the git project to which you want to create a branch. Under Branches -> Local .. select the branch for which you want to create the branch ( In my case I selected master .. you can select another branch if you wish) .. then right click and click on Create Branch option .. and select the checkout this project option and then click the finish button.
Now from the project explorer select the project .. right click then Team -> Push Branch.
A new remote branch will be created. You can give the name of the branch to your colleagues so that they can pull it.
Then to start up a new local + remote branch, I write:
gitb feature/mynewbranch
This creates the branch and does the first push, not just to setup tracking (so that later git pull and git push work without extra arguments), but actually confirming that the target repo doesn't already have such branch in it.
发布评论
评论(26)
首先,创建一个新的本地分支并检查它:
当您将其推送到远程服务器时,会自动创建远程分支:
通常是origin
,这是 git 为您克隆的远程服务器提供的名称。然后,您的同事可以简单地拉动该分支。但请注意,形式上,格式为:
但是当您省略一个时,它假定两个分支名称相同。话虽如此,作为警告的话,不要犯仅指定
:
(使用冒号)的严重错误,或者远程分支将被删除!为了让后续的 git pull 知道要做什么,您可能想使用:
如下所述,
--set-upstream
选项设置上游分支:First, create a new local branch and check it out:
The remote branch is automatically created when you push it to the remote server:
<remote-name>
is typicallyorigin
, which is the name which git gives to the remote you cloned from. Your colleagues may then simply pull that branch.Note however that formally, the format is:
But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only
:<remote-branch-name>
(with the colon), or the remote branch will be deleted!So that a subsequent
git pull
will know what to do, you might instead want to use:As described below, the
--set-upstream
option sets up an upstream branch:首先,您必须在本地创建分支
之后,您可以在您的分支本地工作,当您准备好共享分支时,推送它。下一个命令将分支推送到远程存储库源并跟踪它
队友可以通过执行以下操作到达您的分支:
您可以继续在分支中工作并在需要时随时推送,而无需将参数传递给 git push (无参数 git Push 会将 master 推送到远程 master、本地 your_branch 到远程 your_branch 等...)
队友可以通过执行提交然后显式推送来推送到您的分支,
或者跟踪分支以避免 git Push 的参数
First, you must create your branch locally
After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it
Teammates can reach your branch, by doing:
You can continue working in the branch and pushing whenever you want without passing arguments to git push (argumentless git push will push the master to remote master, your_branch local to remote your_branch, etc...)
Teammates can push to your branch by doing commits and then push explicitly
Or tracking the branch to avoid the arguments to git push
简单的 Git 2.0+ 解决方案:
从 Git 2.0 开始,行为变得更加简单:
您可以使用
push.default = current
配置 git 来使生活更轻松:我添加了这个,所以现在我可以使用
-u
将新分支推送到上游,将跟踪同名的远程分支。现在通过此配置,您将自动猜测对 git Push 的远程引用。来自 git.config 文档:
对我来说,这很好地简化了我的日常 Git 工作流程。配置设置负责“通常”的用例,即您在本地添加分支并希望远程创建它。此外,我只需执行 git co remote_branch_name 即可轻松地从远程创建本地分支(而不是使用
--set-upstream-to
标志)。我知道这个问题和接受的答案相当旧,但行为已经改变,因此现在存在配置选项以使您的工作流程更简单。
要添加到全局 Git 配置,请在命令行上运行:
Simple Git 2.0+ solution:
As of Git 2.0, the behavior has become simpler:
You can configure git with
push.default = current
to make life easier:I added this so now I can just push a new branch upstream with
-u
will track remote branch of the same name. Now with this configuration, you will auto-guess the remote reference to git push. From git.config documentation:For me, this is a good simplification of my day-to-day Git workflow. The configuration setting takes care of the 'usual' use case where you add a branch locally and want to create it remotely. Also, I can just as easily create local branches from remotes by just doing
git co remote_branch_name
(as opposed to using--set-upstream-to
flag).I know this question and the accepted answers are rather old, but the behavior has changed so that now configuration options exist to make your workflow simpler.
To add to your global Git configuration, run this on the command line:
正如前面的答案中所述,
足以推动本地分支机构。
您的同事可以使用以下命令拉取所有远程分支(包括新分支):
然后,要在分支上进行更改,通常的流程:
As stated in the previous answers,
is enough for pushing a local branch.
Your colleagues, can pull all remote branches (including new ones) with this command:
Then, to make changes on the branch, the usual flow:
基于当前分支在本地创建新分支:
像平常一样提交任何更改。然后,将其推送到上游:
这是将当前分支推送到
origin
上同名分支并跟踪它的快捷方式,这样您就不需要指定origin HEAD 将来。
Create a new branch locally based on the current branch:
Commit any changes as you normally would. Then, push it upstream:
This is a shortcut to push the current branch to a branch of the same name on
origin
and track it so that you don't need to specifyorigin HEAD
in the future.如果您想从当前分支创建分支,或者
想要从远程分支创建分支,则可以尝试
如果完成更改,则可以添加文件。
然后在本地进行提交
当你想推送到远程仓库时
全部一起将是
或
如果您想从远程分支创建本地分支
bug_fixes
,请说development
您可以通过以下方式将分支推送到远程仓库:
任何时候您想从任何其他分支更新您的分支分支说
master
,If you want to create a branch from the current branch
you want a branch from a remote branch, you can try
If you are done with changes you can add the file.
Then do a commit locally
When you want to push to remote repo
All together will be
or
If you want to create a local branch
bug_fixes
from a remote branch, saydevelopment
You can push to the branch to remote repo by
Anytime you want to update your branch from any other branch say
master
,[快速解答]
您可以分两步完成:
1.使用
checkout
创建本地分支:2.使用
>push
命令自动创建分支并将代码发送到远程存储库:[Quick Answer]
You can do it in 2 steps:
1. Use the
checkout
for create the local branch:2. Use the
push
command to autocreate the branch and send the code to the remote repository:如果你实际上只想创建远程分支而没有本地分支,你可以这样做:
它将你的 HEAD 中的任何内容推送到远程上不存在的分支 foo 。
If you wanna actually just create remote branch without having the local one, you can do it like this:
It pushes whatever is your HEAD to branch foo that did not exist on the remote.
最简单的解决方案... Drumm Roll... git version 2.10.1 (Apple Git-78)
注意 - 您刚刚在本地环境中创建的分支,以及您所在的远程不存在的分支尝试推送时,必须具有相同的名称。
Easiest Solution... Drumm Roll... git version 2.10.1 (Apple Git-78)
N.B. - The branch you just created in your local environment, and the remote non-existing branch where you are trying to push, must have the same name.
首先,您在本地创建分支:
然后远程创建分支:
注意:这适用于最新版本的 git:
干杯!
First you create the branch locally:
And then to create the branch remotely:
Note: This works on the latests versions of git:
Cheers!
你可以简单地,
git checkout -b YOUR-NEW-BRANCH-NAME
git add .
git push origin YOUR-NEW-BRANCH-NAME
你可以在相关 git repo 下看到您的分支以及代码
干杯! :)
you can simply,
git checkout -b YOUR-NEW-BRANCH-NAME
git add .
git push origin YOUR-NEW-BRANCH-NAME
you can see your branch with the code under the relevant git repo
Cheers !! :)
在 github 上推送分支:
当您想在分支中提交某些内容时,请确保在您的分支中。
您可以查看使用以下命令创建的所有分支:
它将显示:
为您的分支添加新的远程:
将更改从您的提交推送到您的分支:
当官方存储库中的原始分支已更新时更新您的分支:
然后您需要申请合并更改,如果您的分支源自开发,您需要执行以下操作:
删除本地文件系统上的分支:
强制删除文件系统上的本地分支:
删除 github 上的分支:
此处所有信息
其他现有项目
Push the branch on github :
When you want to commit something in your branch, be sure to be in your branch.
You can see all branches created by using :
Which will show :
Add a new remote for your branch :
Push changes from your commit into your branch :
Update your branch when the original branch from official repository has been updated :
Then you need to apply to merge changes, if your branch is derivated from develop you need to do :
Delete a branch on your local filesystem :
To force the deletion of local branch on your filesystem :
Delete the branch on github :
Here All Information
Other Existing project
从现有分支创建本地分支(可以是 master/develop/any-other-branch)。
将其推送到远程
这里,
如果我们删除本地和远程分支名称,它将具有以下格式
这会将本地分支推送到远程,并与本地分支branch_name同名。本地分支也将跟踪远程分支。
Creating a local branch from an existing branch (can be master/ develop/ any-other-branch).
Push this to remote
Here,
If we remove the local and remote branch names, it will have the format
This will push the local branch to remote and with the same name as the local branch branch_name. The local branch will be tracking the remote branch as well.
我知道这个问题得到了很好的回答,但只是想列出我创建新分支“myNewBranch”并推送到远程(在我的情况下为“origin”)并设置跟踪所采取的步骤。将此视为“TL;DR”版本:)
I know this question is well answered, but just wanted to list the steps I take to create a new branch "myNewBranch" and push to remote ("origin" in my case) and set up tracking. Consider this the "TL;DR" version :)
只是想添加 while:
创建一个新分支,它还会检查该分支/使其成为您当前的分支。如果出于某种原因,您想要做的只是折断一个分支,但不将其设为当前分支,那么您将使用以下命令:
在第一个命令中,“checkout”使该分支成为您的当前分支,而“ -b”的意思是:这个分支还不存在,所以为我做一个。
Just wanted to add that while:
Creates a new branch, it also checks out that branch / makes it your current branch. If, for some reason, all you want to do is snap off a branch but not make it your current branch, then you would use the following command:
In the first command, "checkout" makes said branch your current branch, and the "-b" means: this branch doesn't exist yet, so make it for me.
现在使用 git,当您位于正确的分支时,您只需键入
git push --set-upstream origin>;
并 git 为您创建原始分支。
Now with git, you can just type, when you are in the correct branch
git push --set-upstream origin <remote-branch-name
>and git create for you the origin branch.
如何通过源码树来做
How to do through Source Tree
git push -u <远程名称>如果新创建的分支不是从同一个存储库生成的,即如果您尚未使用 git checkout -b new_branch 创建新分支,则 不起作用>,那么这将不起作用。
例如,我在本地克隆了两个不同的存储库,我必须将 repo2/branch1 复制到 repo1/,然后也推送它。
此链接帮助了我将我的本地分支(从另一个存储库克隆)推送到我的远程存储库:
git push -u <remote-name> <branch-name>
doesn't work if the newly created branch isn't spawned from the same repo, i.e. if you haven't created the new branch usinggit checkout -b new_branch
, then this will not work.For eg, I had cloned two different repositories locally and I had to copy repo2/branch1 to repo1/ and then push it too.
This link helped me push my local branch (cloned from another repo) to my remote repo:
从 Git 2.37.0 开始,您不再需要“--set-upstream origin”。只需
git推送
。您可以通过启用push.autoSetupRemote
选项来实现此目的git config --global --add --bool push.autoSetupRemote true
Starting from Git 2.37.0, you do not need "--set-upstream origin" anymore. Just
git push
. You can achieve this with thepush.autoSetupRemote
option enabledgit config --global --add --bool push.autoSetupRemote true
要在远程创建新分支,可以使用以下命令:
git checkout -b
例如,要创建一个名为“feature_branch”的新分支,您可以使用以下命令:
git checkout -b feature_branch
这将在本地创建一个新分支,但不会将其推送到远程存储库。要将新分支推送到远程存储库,可以使用以下命令:
git push origin feature_branch
例如,要将“feature_branch”分支推送到远程存储库,可以使用以下命令:
git push origin feature_branch
这会将新分支推送到远程存储库,并且其他用户可以使用它。
以下是有关在远程创建新分支时需要记住的一些其他事项:
您可以使用 gitbranch -l 命令列出本地存储库中的所有分支。
您可以使用 git checkout 命令切换到不同的分支。
您可以使用 git merge 命令将分支合并到当前分支。
可以使用 git reset --hard 命令将当前分支重置为指定分支的状态。
To create a new branch in remote, you can use the following command:
git checkout -b <new-branch-name>
For example, to create a new branch called "feature_branch", you would use the following command:
git checkout -b feature_branch
This will create a new branch locally, but it will not push it to the remote repository. To push the new branch to the remote repository, you can use the following command:
git push origin feature_branch
For example, to push the "feature_branch" branch to the remote repository, you would use the following command:
git push origin feature_branch
This will push the new branch to the remote repository, and it will be available to other users.
Here are some additional things to keep in mind about creating new branches in remote:
You can use the git branch -l command to list all of the branches in your local repository.
You can use the git checkout command to switch to a different branch.
You can use the git merge command to merge a branch into the current branch.
You can use the git reset --hard command to reset the current branch to the state of the specified branch.
以下是在 Eclipse 中通过 Egit 执行此操作的方法。
进入“Git Repository Exploring”视图并展开您想要创建分支的 git 项目。在分支下 ->本地..选择要为其创建分支的分支(在我的例子中,我选择了master..如果您愿意,您可以选择另一个分支)..然后右键单击并单击“创建分支”选项..并选择结帐此项目选项,然后单击完成按钮。
现在从项目资源管理器中选择项目..右键单击然后团队 ->推送分支。
将创建一个新的远程分支。您可以将分支的名称提供给您的同事,以便他们可以拉取它。
Here is how you do it in eclipse through Egit.
Go the "Git Repository Exploring" view and expand the git project to which you want to create a branch. Under Branches -> Local .. select the branch for which you want to create the branch ( In my case I selected master .. you can select another branch if you wish) .. then right click and click on Create Branch option .. and select the checkout this project option and then click the finish button.
Now from the project explorer select the project .. right click then Team -> Push Branch.
A new remote branch will be created. You can give the name of the branch to your colleagues so that they can pull it.
我使用了两种方法来创建分支
如果您使用 TortoiseGit 请按照以下步骤操作:-
1.使用 TortoiseGit 创建分支
2.推送分支
3.切换到新分支
如果您使用命令提示符,请按照以下步骤操作:-
1.使用命令提示符创建分支
2.推送分支
3.切换到新分支
它已经切换到 new_branch_name 否则你可以使用
I have used two ways to create branch
If you are using TortoiseGit follow these steps:-
1.Create Branch using TortoiseGit
2.Push the branch
3.Switch to new branch
If you are using command prompt follow these steps:-
1.Create branch using command prompt
2.Push the branch
3.Switch to new branch
it will already switched to new_branch_name otherwise you can use
我用这个,它非常方便:
你甚至不需要 git status;也许,我只是想确保一切顺利......
您可以使用一个命令同时拥有本地和远程分支。
I use this and it is pretty handy:
You don't even need git status; maybe, I just want to make sure everything is going well...
You can have BOTH the LOCAL and REMOTE branch with a single command.
我通过将其添加到我的 bash
~/.profile
中解决了这个问题:function gitb() { git checkout -b $1 && git push --set-upstream origin $1;然后
,为了启动一个新的本地+远程分支,我编写:
gitb feature/mynewbranch
这将创建分支并且进行第一次推送,而不仅仅是设置跟踪(以便稍后的 git pull 和 git push 无需额外参数即可工作),但实际上确认目标存储库中尚不存在此类分支。
I've solved this by adding this into my bash
~/.profile
:function gitb() { git checkout -b $1 && git push --set-upstream origin $1; }
Then to start up a new local + remote branch, I write:
gitb feature/mynewbranch
This creates the branch and does the first push, not just to setup tracking (so that later
git pull
andgit push
work without extra arguments), but actually confirming that the target repo doesn't already have such branch in it.这是一个例子,我只有两个首先是本地的分支:origin 和 mobile-test。
直到我在命令行中使用它来实际在远程分支中显示我更新的文件之前,对我来说没有任何作用。
Heres an example I only have two branches that were local first: origin and mobile-test.
Nothing for me worked until I used this in command line to actually show my updated files in a remote branch.
如果您已使用
--single-branch
克隆当前分支,请使用它从当前分支创建一个新分支:If you have used
--single-branch
to clone the current branch, use this to create a new branch from the current: