背景
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 "[email protected]"
创建一个新仓库
git clone ssh://[email protected]:30401/myysophia/git-flight-rules.git
cd git-flight-rules
git switch -c main
touch README.md
gitadd 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://[email protected]:30401/myysophia/git-flight-rules.git
gitadd.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://[email protected]:30401/myysophia/git-flight-rules.git
git push -u origin --allgit 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 <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同样生效
版权归原作者 MyySophia 所有, 如有侵权,请联系我们删除。