背景
git push的時(shí)候,有時(shí)候會用-u參數(shù),有時(shí)候不適用。這是為什么呢?
官方解釋
-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.
實(shí)戰(zhàn)
當(dāng)在gitlab上初始化一個(gè)項(xiàng)目的時(shí)候,通常會給你一些git提升
例如
Git 全局設(shè)置
git config --global user.name "Administrator"
git config --global user.email "ninesun@126.com"
創(chuàng)建一個(gè)新倉庫
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
推送現(xiàn)有文件夾
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
推送現(xiàn)有的 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 參數(shù)相當(dāng)于是讓你本地的倉庫和遠(yuǎn)程倉庫進(jìn)行了關(guān)聯(lián)。
git push -u origin --all
這代表是將本地已存在的git項(xiàng)目的所有分支推送到遠(yuǎn)程倉庫名為origin的倉庫。
git push -u origin main 只推送main分支到遠(yuǎn)程倉庫
還有另外一種情況,如下
我有兩個(gè)遠(yuǎn)程倉庫,一個(gè)內(nèi)網(wǎng)一個(gè)外網(wǎng)的。
這是提交的時(shí)候就需要很明確的知道你需要把本地的哪個(gè)分支推送到遠(yuǎn)程倉庫的哪個(gè)分支。
下面這兩個(gè)push操作你應(yīng)該就知道是什么意思了吧
git push -u main main
git push -u origin master
有時(shí)候你git push不帶u參數(shù)后,git pull (不帶參數(shù))會報(bào)錯(cuò)文章來源:http://www.zghlxwxcb.cn/news/detail-430145.html
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.
這是因?yàn)槟銢]有把你本地的分支和遠(yuǎn)程沒有關(guān)聯(lián)。要么使用git push -u 遠(yuǎn)程倉庫名 本地分支名。 要么git pull的時(shí)候使用-u同樣生效文章來源地址http://www.zghlxwxcb.cn/news/detail-430145.html
到了這里,關(guān)于git push -u參數(shù)是什么意思?的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!