git push -u参数是什么意思?
创始人
2024-05-27 05:50:57
0

背景

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

官方解释

-u--set-upstreamFor 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..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分支到远程仓库

还有另外一种情况,如下
我有两个远程仓库,一个内网一个外网的。
这是提交的时候就需要很明确的知道你需要把本地的哪个分支推送到远程仓库的哪个分支。
在这里插入图片描述

下面这两个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  ').
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 = merge = [remote ""]url = fetch = See git-config(1) for details.

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

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...