github上传代码

0.开头

在github上注册账号并简历仓库后,上传本地数据前需要配置SSH,SSH用于进行身份认证(git是分布式的代码管理工具,远程代码管理是基于ssh的,所以使用远程的git需要ssh的配置)。

关于windows:
如果你使用的是windows系统,建议使用 GitHub for Windows ,安装这个软件也会附带Git Bash工具,并且比通过 git 命令进行版本管理方便的多。

1.git配置

windows用户需要下载安装Git Bash,linux通过终端安装git。安装完后对git进行配置(配置过的用户可以通过git --lis命令查看配置):

$ git config --global user.name "xxx"
$ git config --global user.email "xxx@abc.com"

2.SSH生成

在Git Bash或者终端中,输入以下代码,用你的github邮箱替换其中的邮箱地址:

ssh-keygen -t rsa -b 4096 -C "*your_email@example.com*"
# Creates a new ssh key, using the provided email as a label
Generating public/private rsa key pair.

当界面提示:"Enter a file in which to save the key,"时,直接回车使用默认存储地址(默认地址为:~/.ssh):

Enter a file in which to save the key (/Users/*you*/.ssh/id_rsa): *[Press enter]*

然后提示输入密码和确认密码,此处可以按回车跳过。

Enter passphrase (empty for no passphrase): *[Type a passphrase]*
Enter same passphrase again: *[Type passphrase again]*

然后可以看见提示,ssh生成到此完成。

Your identification has been saved in /home/<user>/.ssh/id_rsa.
Your public key has been saved in /home/<user>/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:********** <youemail>@123.com
The key's randomart image is:
******

ssh生成完成后会在~/.ssh文件夹看到两个文件(默认存储路径时):

默认ssh路径下

3.添加SSH key到ssh-agent

确定ssh-agent是否可用:

  • 如果你使用Git Bash或者终端,键入如下命令打开ssh-agent:
  # start the ssh-agent in the background
  $ eval "$(ssh-agent -s)"
  Agent pid 59566
  • 如果你使用其他的终端,例如Git for Windows,请键入如下命令打开ssh-agent:
# start the ssh-agent in the background
$ eval $(ssh-agent -s)
Agent pid 59566

将SSH key添加到ssh-agent:

$ ssh-add ~/.ssh/id_rsa

4.在你的gitbug账户中添加一个新的SSH key

拷贝SSH key:

  • 通过xclip拷贝,在终端中执行以下命令:
$ sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)

$ xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
  • 直接拷贝。通过编辑器打开id_rsa.pub,或者通过cat等命令直接查看id_rsa.pub文件,并且选中内容拷贝。

在你的github主页选择Settings->SSH and GPG keys->New SSH key or Add SSH key
在title中输入这个ssh key的名称,用于自己辨识。在key中粘贴刚刚复制的数据。
然后点击Add SSH key,进行github密码验证后即可添加成功。

5.git常用操作

  • git init //初始化,在此文件夹中建立空的git库
  • git clone url //同步远程的库
  • git add . //添加所有修改过的文件(仅添加,未上传,或者说未保存到本地库)
  • git commit -m "说明" //真正的上传文件(保存到本地库)
  • git remote add origin url //远程上传到指定地址
  • git push -u origin master //上传到远程库
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容