Git---将本地的仓库代码提交到github上

1. 本地创建好了项目,提交到github

在本地项目处使用下面的命令

  1. git init
  2. git remote add origin https://github.com/xxx
  3. git pull origin master
  4. git add .
  5. git push -u origin master

2. 先在github上创建好了项目

git clone https://github.com/xxx

3. 遇到的问题

3.1 第一次提交,没有拉取仓库内的文件

您可能使用了下面的命令:

  1. git init
  2. git add .
  3. git commit -m "first commit"
  4. git remote add origin https://xxx.xxx.xxx
  5. 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,可以删除工作区内的文件,并且加入到暂存区
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值