git hub简单来说就是一个免费的开源代码管理平台。详细介绍请参考官网
git hub有一个缺点就是在国内访问有点慢,有时候访问不了,需要多试几次,实在不行就得开vpn代理了。
- 为了避免本地代码丢失,可以把代码放在git hub上管理,同时也可以在其他电脑上下载和查看代码。
- 长时间多次修改代码后,有可能把某个功能改坏了,因此需要回退代码,使用git管理非常方便查看修改记录和回退到某个节点。
帐号注册
Screen Shot 2022-01-09 at 12.53.46.png
进入官网后,点击sign up,输入email和密码。
Screen Shot 2022-01-09 at 12.55.59.png
git hub的这个界面非常有科幻感。按照提示完成注册即可,用刚注册的帐号登录。
建立一个代码工程
在个人主页点击new
Screen Shot 2022-01-09 at 13.05.30.png
弹出如下页面,开始创建一个代码工程,输入工程名称
Screen Shot 2022-01-09 at 13.06.06.png
按照提示完成工程创建,空工程建议选中 add a READEME file,里面可以写一下这个工程的介绍。
添加public key
git hub提供4种方式下载代码:https,ssh,CLI,zip
- 其中zip只能下载代码,不能提交代码,只适合下载下来看看源码,无法查看提交记录。
- https方式理论上可以下载代码和提交代码,但实际操作时,github 2021年12月以后无法通过用户名密码提交代码,因此这个方法也无法提交代码了。
- CLI是给企业管理用的,不知道怎么用,因此这里只介绍使用ssh方式。
本地电脑生成public key
# ssh-keygen -t rsa -C "xxx@xxx.com"
其中后面的字符串是个人邮箱,一般用来管理代码,每次提交的时候会子提交记录里面显示这个邮箱,方面联系到代码作者。当然,也可以填一个假的。
把本地的public key添加到git hub工程里面
- 本地的public key可以根据刚才命令行提示信息找到,一般在用户根目录的.ssh/目录下的id_rsa.pub
root@592a3d1e06e8:/usr1/hello_word_jun# ssh-keygen -t rsa -C "xxx@xxx.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:mzcOuOnoHLdT7yG+t3qHnkneHZ3f0TH3/lqIj8sFzk8 xxx@xxx.com
The key's randomart image is:
+---[RSA 3072]----+
| |
| |
| |
| |
| S . o.|
| ..o o o oB|
| . o.=.=.+ Eo=|
| . +.= B=B.B ++|
| .+.=.+BOo=.=.*|
+----[SHA256]-----+
如上提示信息:Your public key has been saved in /root/.ssh/id_rsa.pub
-
使用vi打开/root/.ssh/id_rsa.pub,拷贝里面的全部内容,复制到git hub。点击工程->点击 settings->点击Deploy keys,打开如下界面:把刚才复制的内容添加到key这个输入框,title里面随便填一个标题即可。
Screen Shot 2022-01-09 at 13.24.17.png - 勾选最下面的复选框 Allow write access,点击Add key即可完成public key添加。
使用git下载工程
-
打开工程code页面,点击右边的code按钮,在弹出的小页面里面,点击ssh,再点击复制按钮
Screen Shot 2022-01-09 at 13.30.27.png - 在本地一个空目录下输入git clone xxx命令,其中xxx是刚才复制的ssh地址,如下提示表示克隆成功。
root@592a3d1e06e8:/usr1# git clone git@github.com:junbenjun/shell_Jun.git
Cloning into 'shell_Jun'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
root@592a3d1e06e8:/usr1# ll
total 12
drwxr-xr-x 1 root root 4096 Jan 9 13:32 ./
drwxr-xr-x 1 root root 4096 Jan 2 12:18 ../
drwxr-xr-x 3 root root 4096 Jan 9 13:32 shell_Jun/
root@592a3d1e06e8:/usr1# cd shell_Jun/
root@592a3d1e06e8:/usr1/shell_Jun# git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
root@592a3d1e06e8:/usr1/shell_Jun#
- 修改代码,提交到本地git仓
root@592a3d1e06e8:/usr1/shell_Jun# ll
total 16
drwxr-xr-x 3 root root 4096 Jan 9 13:32 ./
drwxr-xr-x 1 root root 4096 Jan 9 13:32 ../
drwxr-xr-x 8 root root 4096 Jan 9 13:32 .git/
-rw-r--r-- 1 root root 11 Jan 9 13:32 README.md
root@592a3d1e06e8:/usr1/shell_Jun# vi README.md
root@592a3d1e06e8:/usr1/shell_Jun# git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
root@592a3d1e06e8:/usr1/shell_Jun# git add
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?
root@592a3d1e06e8:/usr1/shell_Jun# git add .
root@592a3d1e06e8:/usr1/shell_Jun# git commit
[main 867618e] modify reademe
1 file changed, 5 insertions(+), 1 deletion(-)
root@592a3d1e06e8:/usr1/shell_Jun#
- push代码到github
root@592a3d1e06e8:/usr1/shell_Jun# git push
ERROR: Permission to junbenjun/shell_Jun.git denied to deploy key
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
root@592a3d1e06e8:/usr1/shell/shell_Jun# git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 270 bytes | 135.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 2.git:junbenjun/shell_Jun.git
81f5b89..10987e5 main -> main
root@592a3d1e06e8:/usr1/shell/shell_Jun#
其他电脑下载提交代码
- 第一次下载代码操作一样,参考"使用git下载工程"
- 已经clone过就直接用git fetch,git merge就可以更新代码了
git 常用命令
- 克隆(下载代码)
root@592a3d1e06e8:/usr1# git clone git@github.com:junbenjun/shell_Jun.git
- 查看当前git仓库状态
root@592a3d1e06e8:/usr1/shell_Jun# git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
root@592a3d1e06e8:/usr1/shell_Jun#
- 更新代码-pull
root@592a3d1e06e8:/usr1/shell_Jun# git pull
Already up to date.
root@592a3d1e06e8:/usr1/shell_Jun#
- 更新代码-fetch+merge(推荐使用)
fetch:拉去远端代码到本地git缓存
merge:本地git缓存区合并到工作区
root@592a3d1e06e8:/usr1/shell_Jun# git fetch
root@592a3d1e06e8:/usr1/shell_Jun# git merge
Already up to date.
root@592a3d1e06e8:/usr1/shell_Jun#
- 提交代码(add,commit,push)
add:把本地文件添加到git工作区
commit:把git工作区提交到git缓存区
push:把本地git缓存区提交到远端仓库
root@592a3d1e06e8:/usr1/shell_Jun# git add .
root@592a3d1e06e8:/usr1/shell_Jun# git commit
[main 867618e] modify reademe
1 file changed, 5 insertions(+), 1 deletion(-)
root@592a3d1e06e8:/usr1/shell_Jun# git push
ERROR: Permission to junbenjun/shell_Jun.git denied to deploy key
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.