git总结
git上传代码的方式
将本地代码上传到服务器托管的方式有两种:
- 将本地代码与服务器上的分支做关联,再push本地代码
- 将服务器上的repository clone到本地的空文件夹下,再将代码拷贝到该文件夹下,再push本地代码
以下我以码云为服务器来操作
方式1
1.创建服务器上的repository
2.初始化本地代码仓库
3.查看本地仓库的状态,是否有未提交或修改的文件(这里我添加了一个txt文件)
4.添加所有未加如到暂存区的文件,将其加入git暂存区
6.提交本地代码
5.本地仓库关联远程仓库(master分支)
6.将本地代码提交到远程仓库
可以看到我第一句命令出现了错误,因为在push代码的时候除了git url以外还要加上所要添加的git分支路径,这里默认master。
以上就将本地代码提交到服务器了,可以看到我在码云上的项目已经更新了
方式2
基本操作其实跟方式一差不多,不过少了关联远程服务器的步骤,这里就不再赘述
常见git错误代码分析
- To push the current branch and set the remote as upstream, use git push –set-upstream git_test master
在push命令的时候没有说明提交到哪个分支,跟上面的步骤6其实错误一致
-
fatal: refusing to merge unrelated histories
本地仓库和远程仓库没有关联,需要先关联之后再做push或者pull等交互代码的操作,解决方法(以pull命令为例):
$ git pull origin master --allow-unrelated-histories
-
You asked to pull from the remote ‘origin’, but did not specify: a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line.
解决办法:找到:.git/config 修改如下
1 [branch “master”]
2 remote = origin
3 merge = refs/heads/master -
Please enter a commit message to explain why this merge/(or some other action) is necessary.
commit的时候需要写提交描述,也就是你提交了什么东西,git gui会跳转到一个无法退出的界面,除非你把gui关了,下面是解决方案:
1.按键盘字母 i 进入insert模式
2.输入描述字符串
3.按Esc键
4.输入”:wq”,注意是冒号+wq,按回车键即可
git学习相关资料和网站
1.git中文教程