创建仓库
在Github/Coding/码云/GitLab等新建项目
提交代码
- 在项目根目录输入
git init
初始化 - 添加所有文件
git add .
- 添加更新说明
git commit -m "First commit"
- 设置提交地址
git remote add origin 第一步中项目的提交地址
- 提交
git push origin master
,第一次提交时,若采用https的方式,可能会提示输入账号密码;若采用ssh的形式,可能需要配置ssh
可能出现的错误
如果之后在AndroidStudio提交代码时,弹出错误:
Can’t update: no tracked branch**
No tracked branch configured for branch master.
To make your branch track a remote branch call, for example,
git branch –set-upstream master origin/master
Push rejected
Push to origin/master was rejected
原因是没有指定分支,解决方法就是按提示
git branch --set-upstream master origin/master
此时可能又出现一个提示:
The –set-upstream flag is deprecated and will be removed. Consider using –track or –set-upstream-to
Branch master set up to track remote branch master from origin.
我们按着提示重新设置后就可以提交了
git branch --set-upstream-to origin/master
如果出现了 failed to push some refs to 问题:
error: failed to push some refs to ‘git@github.com:hansionit/H-Downloader.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
主要原因是github中的README.md文件不在本地代码目录中
先通过如下命令执行代码合并
git pull --rebase origin master
如果无法pull,报错 fatal: refusing to merge unrelated histories,就改用以下命令
git pull origin master --allow-unrelated-histories
然后可以看到本地代码库中多了README.md文件,再次push
git push origin master
提示:fatal: remote origin already exists
一般在执行第4步时出现,主要原因是已经添加了远程仓库了,如果需要更换,需要先移除远程仓库
git remote rm origin
然后再重新从第4步开始
git remote add origin 提交地址
提示:
warning: LF will be replaced by CRLF in 某文件
The file will have its original line endings in your working directory.
一般是在执行第2步时出现,但不处理也不会影响提交,主要原因是:
CRLF 代表CR(Carriage-Return)、LF(Line-Feed) 回车换行
回车(CR, ASCII 13, \r) 、换行(LF, ASCII 10, \n) 这两个ACSII字符不会在屏幕有任何输出,但在Windows中广泛使用来标识一行的结束,而在Linux/UNIX系统中只有换行符。 也就是说在windows中的换行符为 CRLF, 而在linux下的换行符为:LF
使用git来生成工程后,文件中的换行符为LF, 当执行git add .时,系统提示:LF 将被转换成 CRLF解决方法:
删除刚刚生成的.git文件
rm -rf .git
配置core.autocrlf为false
git config --global core.autocrlf false
然后重新重第1步开始