申请GitHub账号
直接登录GitHub: Let’s build from here · GitHub网站申请GitHub的账号, 用于学习git
git基本操作知识
git是免费开源的版本控制软件, 对于版本控制而言在敏捷开发的流程中是必不可缺的一部分, 版本控制就是对开发者的代码进行集中化、迭代化管理, 使得每个开发阶段的代码得以保存记录, 方便版本的回退、多人协作的问题. git是现代DevOps理论中实现代码管理的最优工具之一.
-
安装
## 安装git(Linux、Windows、MacOS均支持git) [root@git ~]# yum -y install git ## 配置git个人信息 [root@git ~]# git config --global user.name lsy [root@git ~]# git config --global user.email lsy@163.com [root@git ~]# cat $HOME/.gitconfig [user] name = lsy [email] name = lsy@163.com
-
创建仓库及提交文件
[root@git ~]# mkdir ServerAPI [root@git ~]# cd ServerAPI/ [root@git ServerAPI]# git init 初始化空的 Git 版本库于 /root/ServerAPI/.git/ [root@git ServerAPI]# echo "#ServerAPI\nAuthor: lsy" >README.md [root@git ServerAPI]# git add README.md [root@git ServerAPI]# git commit -m "Add README.md" [master(根提交) 5df2242] Add README.md 1 file changed, 1 insertion(+) create mode 100644 README.md
-
版本回退
--------------------------------------version 01-------------------------------------- [root@git ServerAPI]# vim ServerOnline.sh #!/usr/bin/env bash # # author: lsy # email: lsy@163.com # date: 2023/12/08 # usage: Detect host survivability. netip='192.168.161' for hostip in $(seq 2 254) do { ping -c1 ${netip}.${hostip} &>/dev/null if [ $? -eq 0 ];then echo '{${netip}.${hostip}:online}' >>OnlineIP.json else echo '{${netip}.${hostip}:offline}' >>OfflineIP.json fi }& done wait echo 'Complete OK.' [root@git ServerAPI]# git add ServerOnline.sh [root@git ServerAPI]# git commit -m 'Add ServerOnline.sh shell scripts' [master 2c5e2a2] Add ServerOnline.sh shell scripts 1 file changed, 22 insertions(+) create mode 100644 ServerOnline.sh --------------------------------------version 02-------------------------------------- [root@git ServerAPI]# vim ServerOnline.sh #!/usr/bin/env bash # # author: lsy # email: lsy@163.com # date: 2023/12/08 # usage: Detect host survivability. # Protocol: GPL netip='192.168.161' for hostip in $(seq 2 254) do { ping -c1 ${netip}.${hostip} &>/dev/null if [ $? -eq 0 ];then echo '{${netip}.${hostip}:online}' >>OnlineIP.json else echo '{${netip}.${hostip}:offline}' >>OfflineIP.json fi }& done wait echo 'Complete OK.' [root@git ServerAPI]# git add ServerOnline.sh [root@git ServerAPI]# git commit -m 'modify ServerOnline.sh Protocol:GPL.' [master 7fc9ec9] modify ServerOnline.sh Protocol:GPL. 1 file changed, 1 insertion(+) --------------------------------------version 03-------------------------------------- [root@git ServerAPI]# vim ServerOnline.sh #!/usr/bin/env bash # # author: lsy # email: lsy@163.com # date: 2023/12/08 # usage: Test host online rate. # Protocol: GPL netip='192.168.161' for hostip in $(seq 2 254) do { ping -c1 ${netip}.${hostip} &>/dev/null if [ $? -eq 0 ];then echo '{${netip}.${hostip}:online}' >>OnlineIP.json else echo '{${netip}.${hostip}:offline}' >>OfflineIP.json fi }& done wait echo 'Complete OK.' [root@git ServerAPI]# git add ServerOnline.sh [root@git ServerAPI]# git commit -m 'modify ServerOnline.sh usage descripts.' [master 31aeda4] modify ServerOnline.sh usage descripts. 1 file changed, 1 insertion(+), 1 deletion(-) --------------------------------------reflog_version-------------------------------------- [root@git ServerAPI]# git reflog 31aeda4 HEAD@{0}: commit: modify ServerOnline.sh usage descripts. 7fc9ec9 HEAD@{1}: commit: modify ServerOnline.sh Protocol:GPL. 2c5e2a2 HEAD@{2}: commit: Add ServerOnline.sh shell scripts 5df2242 HEAD@{3}: commit (initial): Add README.md [root@git ServerAPI]# git reset --hard 2c5e2a2 # 回到过去 HEAD 现在位于 2c5e2a2 Add ServerOnline.sh shell scripts [root@git ServerAPI]# git reflog 2c5e2a2 HEAD@{0}: reset: moving to 2c5e2a2 31aeda4 HEAD@{1}: commit: modify ServerOnline.sh usage descripts. 7fc9ec9 HEAD@{2}: commit: modify ServerOnline.sh Protocol:GPL. 2c5e2a2 HEAD@{3}: commit: Add ServerOnline.sh shell scripts 5df2242 HEAD@{4}: commit (initial): Add README.md [root@git ServerAPI]# git reset --hard 31aeda4 # 穿越未来 HEAD 现在位于 31aeda4 modify ServerOnline.sh usage descripts. [root@git ServerAPI]# git reflog 31aeda4 HEAD@{0}: reset: moving to 31aeda4 2c5e2a2 HEAD@{1}: reset: moving to 2c5e2a2 31aeda4 HEAD@{2}: commit: modify ServerOnline.sh usage descripts. 7fc9ec9 HEAD@{3}: commit: modify ServerOnline.sh Protocol:GPL. 2c5e2a2 HEAD@{4}: commit: Add ServerOnline.sh shell scripts 5df2242 HEAD@{5}: commit (initial): Add README.md
git远程仓库的克隆和提交master分支
-
创建远程仓库
-
克隆远程仓库
[root@git ~]# ls -l 总用量 271472 drwxr-xr-x 3 root root 4096 12月 16 21:25 ServerAPI drwxr-xr-x 2 root root 4096 12月 13 10:21 shellscripts drwxr-xr-x 2 root root 4096 12月 13 11:12 test [root@git ~]# git clone https://github.com/lsy/Cloud2312LearnGit.git 正克隆到 'Cloud2312LearnGit'... remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (5/5), done. remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (5/5), done. [root@git ~]# ls -l 总用量 271476 drwxr-xr-x 3 root root 4096 12月 16 21:40 Cloud2312LearnGit drwxr-xr-x 3 root root 4096 12月 16 21:25 ServerAPI drwxr-xr-x 2 root root 4096 12月 13 10:21 shellscripts drwxr-xr-x 2 root root 4096 12月 13 11:12 test
[root@git ~]# cd Cloud2312LearnGit/ [root@git Cloud2312LearnGit]# ls LICENSE README.md [root@git Cloud2312LearnGit]# vim ProjectApacheLog.py # python version: 3.7.2 # author: lsy # email: lsy@163.com # date: 2023/12/08 # usage: closure def logAnalysis(path): def accessLog(frequency): if type(frequency) != dict or len(frequency) != 0: prompt = 'Please Check variable must be list type and len equal zero.' return prompt with open(path, 'r+', encoding='utf8') as logfile: for line in logfile.readlines(): if line.split()[0] not in frequency: frequency[line.split()[0]] = 1 else: frequency[line.split()[0]] += 1 return frequency return accessLog log = logAnalysis('./access_log-20181111') analysis = log({}) ipList = [element for element in analysis.items()] for i in range(len(ipList)): for j in range(i, len(ipList)): if ipList[i][1] < ipList[j][1]: ipList[i], ipList[j] = ipList[j], ipList[i] print(ipList[0:10]) [root@git Cloud2312LearnGit]# git add --all [root@git Cloud2312LearnGit]# git commit -m 'log analysis program.' [master 6f4fdea] log analysis program. 2 files changed, 50 insertions(+) create mode 100644 ProjectApacheLog.py [root@git Cloud2312LearnGit]# git push -u origin master Username for 'https://github.com': <输入用户名> Password for 'https://lsy@github.com': <输入密码> Counting objects: 5, done. Delta compression using up to 2 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 1.07 KiB | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To https://github.com/lsy/Cloud2312LearnGit.git 346e127..6f4fdea master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master
git远程仓库的克隆和提交分支管理
-
创建分支以及合并分支
[root@git Cloud2312LearnGit]# ls LICENSE ProjectApacheLog.py README.md WebPVorUV.py [root@git Cloud2312LearnGit]# git checkout -b developer 切换到一个新分支 'developer' [root@git Cloud2312LearnGit]# git branch * developer master [root@git Cloud2312LearnGit]# ls LICENSE ProjectApacheLog.py README.md WebPVorUV.py [root@git Cloud2312LearnGit]# vim JsonOpera.py # author: lsy # email: lsy@163.com # date: 2023/12/08 # usage: json file opera. import json params = { 'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23 } with open('params.json', 'w') as fout: params_str = json.dump(params, fout) with open('params.json', 'r') as fin: original_params = json.load(fin) [root@git Cloud2312LearnGit]# git add JsonOpera.py [root@git Cloud2312LearnGit]# git commit -m 'json opera.' [developer 82edc9a] json opera. 1 file changed, 20 insertions(+) create mode 100644 JsonOpera.py [root@git Cloud2312LearnGit]# git push -u origin developer Username for 'https://github.com': lsy Password for 'https://lsy@github.com': Counting objects: 4, done. Delta compression using up to 2 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 492 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. remote: remote: Create a pull request for 'developer' on GitHub by visiting: remote: https://github.com/lsy/Cloud2312LearnGit/pull/new/developer remote: To https://github.com/lsy/Cloud2312LearnGit.git * [new branch] developer -> developer 分支 developer 设置为跟踪来自 origin 的远程分支 developer
[root@git Cloud2312LearnGit]# git checkout master 切换到分支 'master' [root@git Cloud2312LearnGit]# git merge developer 更新 6f4fdea..82edc9a Fast-forward JsonOpera.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 JsonOpera.py [root@git Cloud2312LearnGit]# git branch -d developer 已删除分支 developer(曾为 82edc9a)。 [root@git Cloud2312LearnGit]# git branch * master
-
代码冲突
[root@git Cloud2312LearnGit]# git checkout -b pydeveloper 换到一个新分支 'pydeveloper' [root@git Cloud2312LearnGit]# vim stringOpera.py str01 = "python is good language" # p y t h o n i s g o o d l a n g u a g e # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # print(len(str01)) # print(str01[0:3]) # print(str01[0:]) # print(str01[:-1]) # # for index in range(len(str01)): # print(str01[index]) # # for i in str01: # print(i) str01_split = str01.split() print(str01_split, type(str01_split)) str01_replace = str01.replace('o', '0') print(str01_replace) if "o" in str01: print("o exist in str01") else: print("o not exist in str01") [root@git Cloud2312LearnGit]# git add stringOpera.py [root@git Cloud2312LearnGit]# git commit -m 'add stringOpera.py' [pydeveloper dbc1867] add stringOpera.py 1 file changed, 25 insertions(+) create mode 100644 stringOpera.py [root@git Cloud2312LearnGit]# git checkout master 切换到分支 'master' [root@git Cloud2312LearnGit]# ls JsonOpera.py LICENSE ProjectApacheLog.py README.md WebPVorUV.py [root@git Cloud2312LearnGit]# vim stringOpera.py var01 = "python is good language" # p y t h o n i s g o o d l a n g u a g e # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # print(len(var01)) # print(var01[0:3]) # print(var01[0:]) # print(var01[:-1]) # # for index in range(len(var01)): # print(var01[index]) # # for i in var01: # print(i) var01_split = var01.split() print(var01_split, type(var01_split)) var01_replace = var01.replace('o', '0') print(var01_replace) if "o" in var01: print("o exist in var01") else: print("o not exist in var01") [root@git Cloud2312LearnGit]# git add stringOpera.py [root@git Cloud2312LearnGit]# git commit -m 'add stringOpera.py' [master 87dc60c] add stringOpera.py 1 file changed, 25 insertions(+) create mode 100644 stringOpera.py [root@git Cloud2312LearnGit]# git branch * master pydeveloper [root@git Cloud2312LearnGit]# git merge pydeveloper 自动合并 stringOpera.py 冲突(添加/添加):合并冲突于 stringOpera.py 自动合并失败,修正冲突然后提交修正的结果 [root@git Cloud2312LearnGit]# vim stringOpera.py [root@git Cloud2312LearnGit]# git checkout master 切换到分支 'master' [root@git Cloud2312LearnGit]# git merge pydeveloper 更新 6f4fdea..82edc9a Fast-forward stringOpera.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 stringOpera.py [root@git Cloud2312LearnGit]# git branch -d pydeveloper 已删除分支 pydeveloper(曾为 82edc9a)。 [root@git Cloud2312LearnGit]# git branch * master