赞
踩
git clone 远程仓库地址
奇淫巧技
git clone 远程仓库地址
方式一:
git init
,新建 .gitignore 文件git add .
git remote add <remote> 远程仓库地址
,默认为 origingit pull <remote> <branch> --allow-unrelated-histories
git fetch <remote> <branch>
git merge --allow-unrelated-histories
git commit -m 本次提交的说明信息
git push -u <remote> <branch>
方式二:
git init
,新建 .gitignore 文件git add .
git remote add <remote> 远程仓库地址
,默认为 origin**git checkout --track <remote>/<branch>**
git pull --allow-unrelated-histories
git fetch
git merge --allow-unrelated-histories
git commit -m 本次提交的说明信息
git push
git pull 其实是 git fetch (获取远端代码)和 git merge(合并分支) 的集合写法。
为什么直接使用 git pull 不行,而要使用 git pull origin master 呢?
因为远程仓库可能有很多分支,git 需要知道本地当前分支的上游分支是哪个。
如果没有指定上游分支,则 git fetch
就需要明确写出是从远程仓库的哪个分支获取代码。
查看上游分支:
git status
git branch -vv
$ git status
On branch master
Your branch is up to date with 'gitee/master'.
指定上游分支。我们可以直接指定,也可以在 git push 中指定,或者新建分支设置跟踪关系:
git branch --set-upstream-to=<origin>/<branch> [<本地分支>]
fatal: The current branch new has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
git checkout -b 本地分支 --track <remote>/<branch>
**git checkout --track <remote>/<branch>**
如果想要删除分支的upstream信息,可以使用命令:
git branch --unset-upstream
(删除当前分支的upstream信息)git pull 为什么需要 --allow-unrelated-histories?
第一次 git pull,其中的 git fetch 是没有问题的,但是 git merge 就会报错。
错误提示:fatal: refusing to merge unrelated histories
拒绝合并不相干的历史。
错误原因:
简单来说就是:过去git merge允许将两个没有共同基础的分支进行合并,这导致了一个后果:新创建的项目可能被一个毫无戒备的维护者合并很多没有必要的历史,到一个已经存在的项目中,目前这个命令已经被纠正,但是我们依然可以通过
--allow-unrelated-histories
选项来逃逸这个限制,来合并两个独立的项目;
什么是有共同基础的分支?
远程仓库和本地仓库之间没有共同的基础,都是独自的一条链。在 git 看来,这就是两个完全独立的项目。现在两个独立的项目想要 git merge
合并,就进行了一些默认限制。
push 的完整语法:git push 远程仓库别名 本地分支名:远端分支名
git push origin master
git push origin hhh
,但远端没有 hhh 分支,那会在远端创建同名的 hhh 分支为什么有时候可以简写成 git push,有时候简写会报错?
因为在git的全局配置中,有一个push.default属性,其决定了git push操作的默认行为。在Git 2.0之前,这个属性的默认被设为’matching’,2.0之后则被更改为了’simple’。
push.default的可选值:nothing , current , upstream , simple , matching
因为 simple 这个同名的要求,所以 github 主分支改为 main,本地分支默认依然为 master 的情况下,明明已经设置了上游分支,git push 还是会报错。
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use
git push gitee HEAD:test
To push to the branch of the same name on the remote, use
git push gitee HEAD
To choose either option permanently, see push.default in 'git help config'.
为了之后方便的使用 git push 来完成推送,现在要么改掉 push 的默认行为为 upstream,或者在设置上游分支的时候保持本地分支和上游分支同名。
git config --global push.default upstream
git branch --track <remote>/<branch>
建议使用新建分支的方式。
更多介绍:
.gitignore
文件Git 忽略文件可以不用自己写,GitHub 上有现成的可以根据需求自己组合过滤规则。
// 常用的
node_moudel
/dist
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。