git push -u参数是什么意思?

简介: git push的时候,有时候会用-u参数,有时候不适用。这是为什么呢?

背景

git push的时候,有时候会用-u参数,有时候不适用。这是为什么呢?

官方解释

-u
--set-upstream
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).

branch..merge

Defines, together with branch..remote, the upstream branch for the given branch. It tells git fetch/git pull which branch to merge and can also affect git push (see push.default). When in branch , it tells git fetch the default refspec to be marked for merging in FETCH_HEAD. The value is handled like the remote part of a refspec, and must match a ref which is fetched from the remote given by “branch..remote”. The merge information is used by git pull (which at first calls git fetch) to lookup the default branch for merging. Without this option, git pull defaults to merge the first refspec fetched. Specify multiple values to get an octopus merge. If you wish to setup git pull so that it merges into from another branch in the local repository, you can point branch..merge to the desired branch, and use the special setting . (a period) for branch..remote.

实战

当在gitlab上初始化一个项目的时候,通常会给你一些git提升

例如

Git 全局设置
git config --global user.name "Administrator"
git config --global user.email "ninesun@126.com"
创建一个新仓库
git clone ssh://git@k8s-22.host.com:30401/myysophia/git-flight-rules.git
cd git-flight-rules
git switch -c main
touch README.md
git add README.md
git commit -m "add README"
git push -u origin main
推送现有文件夹
cd existing_folder
git init --initial-branch=main
git remote add origin ssh://git@k8s-22.host.com:30401/myysophia/git-flight-rules.git
git add .
git commit -m "Initial commit"
git push -u origin main
推送现有的 Git 仓库
cd existing_repo
git remote rename origin old-origin
git remote add origin ssh://git@k8s-22.host.com:30401/myysophia/git-flight-rules.git
git push -u origin --all
git push -u origin --tags

-u 参数相当于是让你本地的仓库和远程仓库进行了关联。


git push -u origin --all


这代表是将本地已存在的git项目的所有分支推送到远程仓库名为origin的仓库。


git push -u origin main 只推送main分支到远程仓库


还有另外一种情况,如下

我有两个远程仓库,一个内网一个外网的。

这是提交的时候就需要很明确的知道你需要把本地的哪个分支推送到远程仓库的哪个分支。

d9574b1ef5f34f308ac051d7db615f09.png下面这两个push操作你应该就知道是什么意思了吧

git push -u main main


git push -u origin master


有时候你git push不带u参数后,git pull (不带参数)会报错

You asked me to pull without telling me which branch you
want to merge with, and 'branch.test.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.
If you often merge with the same branch, you may want to
use something like the following in your configuration file:
    [branch "test"]
    remote = <nickname>
    merge = <remote-ref>
    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>
See git-config(1) for details.

这是因为你没有把你本地的分支和远程没有关联。要么使用git push -u 远程仓库名 本地分支名。 要么git pull的时候使用-u同样生效

目录
相关文章
|
8天前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
29 16
|
9天前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
37 18
|
3月前
|
网络安全 开发工具 git
解决fatal:remote error:You can’t push to git://github.com/username/*.g
通过上述诊断与修复步骤,绝大多数的推送错误都能得到有效解决,确保您的Git工作流顺畅无阻。
299 1
|
5月前
|
开发工具 git
GIT:如何合并已commit的信息并进行push操作
通过上述步骤,您可以有效地合并已提交的信息,并保持项目的提交历史整洁。记得在执行这些操作之前备份当前工作状态,以防万一。这样的做法不仅有助于项目维护,也能提升团队协作的效率。
218 4
|
5月前
|
开发工具 git
GIT:如何合并已commit的信息并进行push操作
通过上述步骤,您可以有效地合并已提交的信息,并保持项目的提交历史整洁。记得在执行这些操作之前备份当前工作状态,以防万一。这样的做法不仅有助于项目维护,也能提升团队协作的效率。
315 3
|
6月前
|
开发工具 git 开发者
|
8月前
|
Shell 网络安全 开发工具
git实现服务器自动push拉取代码--webhooks
git实现服务器自动push拉取代码--webhooks
646 1
|
8月前
|
网络安全 开发工具 数据安全/隐私保护
git pull/push每次都需要输入密码问题
git pull/push每次都需要输入密码问题
492 0
|
8月前
|
Linux 开发工具 git
解决 Linux git push 贡献者不同(没有出现绿点)的问题
解决 Linux git push 贡献者不同(没有出现绿点)的问题
|
8月前
|
开发工具 git
【已解决】error: failed to push some refs to ‘git@github.com:BATdalao/Github-green.git‘
【已解决】error: failed to push some refs to ‘git@github.com:BATdalao/Github-green.git‘