github的一些提交操作
1. 本地创建好了项目,提交到github
在本地项目处使用下面的命令
git init
git remote add origin https://github.com/xxx
git pull origin master
git add .
git push -u origin master
2. 先在github上创建好了项目
git clone https://github.com/xxx
3. 遇到的问题
3.1 第一次提交,没有拉取仓库内的文件
您可能使用了下面的命令:
git init
git add .
git commit -m "first commit"
git remote add origin https://xxx.xxx.xxx
git push -u origin master
那么在运行到最后一行代码的时候,会提示您需要先进行拉取一下,仓库上有你没有的文件
To https://github.com/ruanjiayu/RabbitMQ.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ruanjiayu/RabbitMQ.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.
那么我输入了下面的指令
$ git pull origin master
提示我两个分支没有任何的交际,不能在本地进行合并。
From https://github.com/ruanjiayu/RabbitMQ
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
git pull -f origin master --allow-unrelated-histories
这样就可以把无关的分支合并了
注意上述出现问题的原因是,你的github上有文件(比如:README.md)但是本地仓库没有导致的。另外这样的提交方式会导致你的ignore文件对一开始文件不起效果,因为本地初始化后,你使用了git add . 导致所有文件加入了暂存区
3.2 git commit提交修改好的文件时候,提示Changes not staged for commit
- 使用
git status
来查看本地修改好的文件是否已将放入暂存区 - 如果发现没有加入暂存区,那么你可以使用
git add .
或者git add fileName
来加入暂存区 - 最后你可以使用
git commit -m "xxxxx"
来提交暂存区的数据到本地库,当然你可以直接可以使用git commit -am "xxxx""
来进行直接的提交
3.3 已经加入暂存区的文件如何撤回,保留工作区内的文件
- 使用
git rm --cached fileName
可以撤回相对应的文件。 - 使用
git rm -r --cached .
可以撤回所有的文件
3.4 删除工作区内的文件,并且进行提交上传。注意:要删除的文件是没有修改过的,就是说和当前版本库文件的内容相同
- 使用
git rm fileName
,可以删除工作区内的文件,并且加入到暂存
3.5 强制删除工作区和暂存区内对应的文件,并将删除好后的状态提交到暂存区
- 使用
git rm -f fileName
,可以删除工作区内的文件,并且加入到暂存区