Git是一款开源的分布式版本控制系统,而GItHub是依托Git的代码托管平台
准备工作
一、注册账号
进入GitHub官网首页,GitHub官网网址:https://github.com/,其中需要设置
账号:liuzy1
密码:************
邮箱:*************@163.com
二、设置SSH Key
由于本地GIt仓库与GitHub网站仓库之间的传输通过SSH加密的,这时需要设置SSH Keys。
1.创建SSH Key
首先在用户主目录下查看手否有.ssh目录,
cd ~/.ssh
ls
查看是否存在id_rsa,id_rsa.pub两个文件。
如果没有,则运行
ssh-keygen -t rsa -C "*********@163.com"
运行此命令后,一直回车,会生成/home/grq/…ssh文件夹,里面有id_rsa,id_rsa.pub。它们是SSH Key的秘钥对。其中id_rsa是私钥,不能泄露,id_rsa.pub是公钥,可以告诉其他人
整个过程如下:
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~$ cd ~/.ssh
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ls
known_hosts
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-keygen -t rsa -C "*********@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/lzy/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/lzy/.ssh/id_rsa.
Your public key has been saved in /home/lzy/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5d9cB2PudrUbExxqTxpA0l0KglM0iUL/I4Um2Gx9NtI changan_03053399@163.com
The key's randomart image is:
+---[RSA 2048]----+
| .. =*oo. .. |
| +.o+o.=.... |
| . =.*.E.. .+. |
| . o *o. .ooo.|
| .So. +.++|
| . ...+=.=|
| ..==.|
| . .+|
| . |
+----[SHA256]-----+
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh -v git@github.com
OpenSSH_7.2p2 Ubuntu-4ubuntu2.7, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to github.com [52.74.223.119] port 22.
debug1: Connection established.
debug1: identity file /home/lzy/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/lzy/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.7
debug1: Remote protocol version 2.0, remote software version babeld-9d924d26
debug1: no match: babeld-9d924d26
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: rsa-sha2-512
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /home/lzy/.ssh/known_hosts:1
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/lzy/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/lzy/.ssh/id_dsa
debug1: Trying private key: /home/lzy/.ssh/id_ecdsa
debug1: Trying private key: /home/lzy/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-agen -s
No command 'ssh-agen' found, did you mean:
Command 'ssh-agent' from package 'openssh-client' (main)
ssh-agen: command not found
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-zG7300szo7zJ/agent.5421; export SSH_AUTH_SOCK;
SSH_AGENT_PID=5422; export SSH_AGENT_PID;
echo Agent pid 5422;
lzy@lzy-Lenovo-ideapad-Y700-17ISK:~/.ssh$ ssh-add ~/.ssh/id_rsa
Identity added: /home/lzy/.ssh/id_rsa (/home/lzy/.ssh/id_rsa)
2.在Github端设置,SSH Key
登录GitHub,点击右上角头像,Settings -> Personal settings -> SSH and GPG keys。在SSH Keys标签右方点击New SSH Key。
弹出两个文本框。其中的Title,可以随意命名。笔者此处随便命名为grq-Ubuntu。
另一个Key文本框,需要输入刚刚生成的id_rsa.pub文件中的内容。粘贴后点击Add SSH Key,即可生成SSH Key。
三、上传项目
1.准备工作
ubuntu安装Git:sudo apt-get install git
查看Git版本信息:git version
配置Git 用户信息
lzy@lzy-Lenovo-IdeaPad-S500:~$ git config --global user.name"liuzy1"
lzy@lzy-Lenovo-IdeaPad-S500:~$ git config --global user.email"*********@163.com"
进入到我们需要上传项目所在文件夹,打开终端,使用git的初始化命令
git init
这个命令可以把当前目录变成git可以管理的仓库。
现在我们就可以进行git操作将项目上传到GitHub了。
2.添加需要上传的文件
上传之前,我们需要添加上传的文件。这里使用到的指令是:
git add file
其中,file是我们想要添加的文件。这里笔者想要将整个文件夹内容都添加进去,所以此处笔者输入的指令如下:
git add ./
3.检查当前git 状态
我们可以使用git指令,查看当前git的状态。指令如下:
git status
该指令可以查看当前的分支以及添加文件的情况。
4.commit 推送
将缓存区的修改提交到本地仓库。指令如下:
git commit -m "Update Readme Files(Version of Chinese & English)"
-m后的内容是笔者添加的描述。由于笔者这次是更新该仓库,更新内容是中文与英文的Readme文件
若出现错误:
** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'XXX@YYY.(none)')
这时候的解决办法是,在进行git add ./操作的路径中,实际上已经生成了一个隐藏的.git文件夹。在该路径下输入指令cd ./.git便进入.git文件夹,使用gedit或vim打开文件config,在文件末尾加入内容:
[user]
email=*********@163.com
name=liuzy1
填的GitHub账号中的Email和用户名。填好后如图所示:
这时候再进行commit指令,应该就可以成功了。完成后就可以等待提交了。
5.添加文件到远程库
构建仓库,得到该仓库的SSH Key 向其上传内容。进入新建的仓库,点击Clone or download,在弹出的Clone with SSH框中点击小按钮Copy to clipboard。如下图所示:
1.添加 一个远程仓库,命名为origin,这样随后才可以添加文件到远程库中。指令如下:
git remote add origin git@github.com:liuzy1/god.git
如果此时出现错误:fatal: remote origin already exists. 这时候说明远程仓库已经存在。这时候需要先删除origin仓库,然后再重新添加该远程仓库。指令如下:
git remote rm origin
git remote add origin git@github.com:liuzy1/god.git
2.添加文件到远程库
git remote set-url origin git@github.com:liuzy1/god.git
3.push指令进行上传。
if 如果该仓库是第一次进行push,则指令如下:
git push origin master
当执行到push时,就会报错,报错代码如下:
$ git push origin master
To git@github.com:liuzy1/sensor_fusion.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:liuzy1/sensor_fusion.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.
解决方法:
出现这个问题是因为github中的README.md文件不在本地代码目录中,可以通过如下命令进行代码合并
git pull --rebase origin master
然后再
git push origin master
else 之前传输过,此处为更新
git push
case2 :
有时候运行了git push origin master遇到这样的错误:
To git@github.com:liuzy1/shenlancollege_laser_slam.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:liuzy1/shenlancollege_laser_slam.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决方法:
push前先将远程repository修改pull下来
$ git pull origin master
$ git push -u origin master
最后在repository后面添加readme.md也是可以的。
push结束后,在GitHub端的对应仓库上刷新一下,内容就改变了。说明项目内容已经上传成功了~