使用git提交代码到github

本文详细介绍了首次使用Git所需完成的基本配置步骤,包括用户信息配置、SSH密钥生成与添加至GitHub等,并提供了本地代码上传至GitHub的具体操作流程及常见错误解决办法。

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

首次使用git要做的工作:

  1. 配置用户信息
    git config –global user.name “nc”
    git config –global user.email “nicahead@gmail.com”
  2. 配置SHH
    配置shh key是让本地git项目与远程的github建立联系
    先检查是否已经有SSH Key,打开Git Bash,输入 cd ~/.ssh
    如果没有.ssh这个目录,则生成一个新的SSH,输入 ssh-keygen -t rsa -C "your e-mail"
    接下来一顿回车,可能中间会输入yes
    出现这种类型的图案,则配置成功
    这里写图片描述
  3. 添加 SSH Key 到 GitHub
    打开Git Bash,然后输入 cd ~/.ssh
    进入到.shh文件夹中再输入ls,查看是否有id_rsa.pub文件
    输入cat命令,打开id_rsa.pub文件 cat id_rsa.pub
    将该ssh字符串复制下来,进入Github官网,点击+旁边的头像,再按settings进入设置 ,点击New SSH key创建新的 SSH Key
  4. 验证配置是否成功
    打开Git Bash,然后输入 ssh -T git@github.com , 显示success信息

一次完整的本地代码上传到github的操作:

  1. github上新建一个仓库 new repository
  2. 在本地项目的文件夹下执行 git init 命令 ,初始化版本库
  3. 执行git add 文件名,添加文件到版本库,可以执行git add .将文件夹下的所有文件添到版本库
  4. 执行git commit -m “标示”,创建这次版本的标示
    出现提示: On branch master nothing to commit, working directory clean
    解决方法: rm -rf .git/ 然后从第二步开始重新操作
  5. 和仓库建立连接 git remote add origin git@github.com:yourName/yourname.github.io.git
    出现提示: fatal: remote origin already exists.
    解决方法: git remote rm origin
  6. 开始上传 git push -u origin master
    出现提示: fatal: unable to access 'https://github.com/yourname/yourname.github.io.git/': The requested URL returned error: 403
    解决方法: 通过ssh的方式与仓库建立连接 git remote rm origin
    出现提示: $ git push -u origin master
    To git@github.com:nicahead/nicahead.github.io.git
    ! [rejected] master -> master (fetch first)
    error: failed to push some refs to 'git@github.com:nicahead/nicahead.github.io.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 --rebase origin master 然后 git push -u origin master
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值