git command

本文详细介绍如何从本地上传项目到GitHub,包括初始化仓库、添加文件、提交更改、关联远程仓库及推送代码的步骤。同时,也讲解了如何克隆远程仓库到本地、更新远程仓库到本地以及解决推送冲突的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 上传本地项目到GitHub
mkdir demo
cd demo
echo "# demo" >> README.md
git init //把这个目录变成Git可以管理的仓库
git add README.md //文件添加到仓库
git add . //不但可以跟单一文件,还可以跟通配符,更可以跟目录。一个点就把当前目录下所有未追踪的文件全部add了(空目录不会被添加)
git status //查看当前工作区的状态(需提交的变更)
git commit -m "first commit" //把文件提交到仓库
git remote add origin git@github.com:hxf0663/demo.git //关联远程仓库
git push -u origin master //将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库,第一次用,后面只用下面的即可)
git push origin master //将本地主分支推到远程主分支
  1. 克隆远程仓库到本地
git clone git@github.com:hxf0663/demo.git
  1. 更新远程仓库到本地(并覆盖本地)
git pull origin master
  1. git清空缓存
git rm -r --cached .
  1. .git文件太大
    直接重建
rm -rf .git
git init
git add -A
git commit
  1. git提交到远程仓库冲突
error: failed to push some refs to 'https://github.com/GDDXZ/RobotDenso.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 push -f

可以提交,会将remote上第一个人的改动冲掉,比较暴力,不太好。

  • 正常解决
    先 git fetch origin 然后git merge origin/master, 和本地分支合并, 之后再push。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值