安装Git,显示版本信息则安装成功
leo@Dalitek:~$ sudo apt-get install git
[sudo] password for leo:
Reading package lists... Done
Building dependency tree
Reading state information... Done
git is already the newest version (1:2.27.0-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
leo@Dalitek:~$ git --version
git version 2.27.0
leo@Dalitek:~$
创建版本库
新建一个文件
leo@Dalitek:~$ ls
Desktop Documents Downloads esp Music Pictures Public share Templates Videos
leo@Dalitek:~$ mkdir Git
leo@Dalitek:~$ cd Git/
leo@Dalitek:~/Git$ mkdir git_local
leo@Dalitek:~/Git$ cd git_local/
leo@Dalitek:~/Git/git_local$
初始化版本库
leo@Dalitek:~/Git/git_local$ git init
Initialized empty Git repository in /home/leo/Git/git_local/.git/
leo@Dalitek:~/Git/git_local$ ls -ah
. .. .git
leo@Dalitek:~/Git/git_local$
写一个文件,并做第一次提交
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
这是我Git上写的第一个文件,并做第一次提交
leo@Dalitek:~/Git/git_local$
添加到暂存区
leo@Dalitek:~/Git/git_local$ ls
test.txt
leo@Dalitek:~/Git/git_local$ git add test.txt
leo@Dalitek:~/Git/git_local$
做第一次提交
leo@Dalitek:~/Git/git_local$ git commit -m "1:第一次提交新版本"
*** 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 'leo@Dalitek.(none)')
leo@Dalitek:~/Git/git_local$ git commit -m "1:第一次提交新版本"
[master (root-commit) 8329eef] 1:第一次提交新版本
1 file changed, 2 insertions(+)
create mode 100644 test.txt
leo@Dalitek:~/Git/git_local$
查看提交的记录
commit 8329eef08c4bb84159bd51931b18eac76ee13398 (HEAD -> master)
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:39:09 2020 +0800
1:第一次提交新版本
版本回退
修改文件做一次新的提交:
-----------------------------------------------------------------------------------------
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
这是我Git上写的第一个文件,并做第一次提交
版本回退测试
leo@Dalitek:~/Git/git_local$ git add test.txt
leo@Dalitek:~/Git/git_local$ git commit -m "版本回退测试"
[master 8d0180d] 版本回退测试
1 file changed, 1 insertion(+)
leo@Dalitek:~/Git/git_local$ git log
commit 8d0180d86fc1b1fc226abe03ec488ca37e9c1264 (HEAD -> master)
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:45:25 2020 +0800
版本回退测试
commit 8329eef08c4bb84159bd51931b18eac76ee13398
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:39:09 2020 +0800
1:第一次提交新版本
leo@Dalitek:~/Git/git_local$
-----------------------------------------------------------------------------------------
回退到第一个版本:
-----------------------------------------------------------------------------------------
leo@Dalitek:~/Git/git_local$ git reset --hard HEAD^
HEAD is now at 8329eef 1:第一次提交新版本
leo@Dalitek:~/Git/git_local$ git log
commit 8329eef08c4bb84159bd51931b18eac76ee13398 (HEAD -> master)
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:39:09 2020 +0800
1:第一次提交新版本
leo@Dalitek:~/Git/git_local$
-----------------------------------------------------------------------------------------
回退到第二个版本:
-----------------------------------------------------------------------------------------
leo@Dalitek:~/Git/git_local$ git reset --hard 8d0180d86fc1b1fc226abe03ec488ca37e9c1264
HEAD is now at 8d0180d 版本回退测试
leo@Dalitek:~/Git/git_local$ git log
commit 8d0180d86fc1b1fc226abe03ec488ca37e9c1264 (HEAD -> master)
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:45:25 2020 +0800
版本回退测试
commit 8329eef08c4bb84159bd51931b18eac76ee13398
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:39:09 2020 +0800
1:第一次提交新版本
leo@Dalitek:~/Git/git_local$ cat test.txt
这是我Git上写的第一个文件,并做第一次提交
版本回退测试
-----------------------------------------------------------------------------------------
情景一: 明确知道我们要回退到第几个版本
打开相应的Git 命令行,在Git中,用HEAD表示当前版本,所以回退到上一个版本就只需要使用命令: git reset --hard HEAD^
情景二:已经不记得是第几个版本了
有时候,提交的东西多了,或者是一段时间后,再来继续这个项目,可能早就不记得到底是第几个版本了,这时候,我们可以使用命令:git log 来查看每次的更改记录
情景三:回退到某一个文件之后,又需要返回到最近更新的某个版本
原命令行窗口还未关闭: 直接往上查看一下要回到的那个版本的ID,然后执行命令:
$ git reset --hard 580361e6bf5ce744c0ca4a2295e97bc42f5a6c36
已经关闭:
可以借助命令: $ git reflog 来查看每一次命令的记录
leo@Dalitek:~/Git/git_local$ git reflog
8d0180d (HEAD -> master) HEAD@{0}: reset: moving to 8d0180d86fc1b1fc226abe03ec488ca37e9c1264
8329eef HEAD@{1}: reset: moving to HEAD^
8d0180d (HEAD -> master) HEAD@{2}: commit: 版本回退测试
8329eef HEAD@{3}: commit (initial): 1:第一次提交新版本
查看到之后,我们再利用 git reset 来返回到相应的版本即可,HEAD前面的一串字符为我们简写的ID
命令:$ git reset --hard 580361e
leo@Dalitek:~/Git/git_local$ git reset --hard 8d0180d8
HEAD is now at 8d0180d 版本回退测试
大功告成,这时候再次查看你的text 文件夹,里面的以及修改过的 1.txt 已经回来
工作区和暂存区
修改文件,添加到暂存区,并观察版本库状态
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ git add test.txt
leo@Dalitek:~/Git/git_local$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
leo@Dalitek:~/Git/git_local$
提交一个新的版本,观察版本库的状态:
leo@Dalitek:~/Git/git_local$ git commit -m "体会Git的工作原理"
[master d559c91] 体会Git的工作原理
1 file changed, 1 insertion(+), 3 deletions(-)
leo@Dalitek:~/Git/git_local$
管理修改
修改文件,添加到暂存区
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
leo@Dalitek:~/Git/git_local$ git add test.txt
leo@Dalitek:~/Git/git_local$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
leo@Dalitek:~/Git/git_local$
再次修改文件,不添加到暂存区,做一次提交:
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
再次修改文件,不添加暂存区,做一次提交
leo@Dalitek:~/Git/git_local$ git commit -m "再次修改文件,不添加暂存区,做一次提交"
[master 7b888c3] 再次修改文件,不添加暂存区,做一次提交
1 file changed, 1 insertion(+)
leo@Dalitek:~/Git/git_local$
查看HEAD指向版本和工作区之间的差异:
leo@Dalitek:~/Git/git_local$ git diff HEAD -- test.txt
diff --git a/test.txt b/test.txt
index e95328a..71c24d8 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
+再次修改文件,不添加暂存区,做一次提交
leo@Dalitek:~/Git/git_local$
撤销修改
撤销工作区还未添加到暂存区的修改:
——————————————————————————————————————————————————————————————————————————————————————
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
再次修改文件,不添加暂存区,做一次提交
撤销工作区还未添加到暂存区的修改
leo@Dalitek:~/Git/git_local$ git status
On branch master
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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
leo@Dalitek:~/Git/git_local$ git checkout -- test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
leo@Dalitek:~/Git/git_local$
——————————————————————————————————————————————————————————————————————————————————————
撤销工作区已经添加到暂存区的修改:
——————————————————————————————————————————————————————————————————————————————————————
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
撤销工作区已经添加到暂存区的修改
leo@Dalitek:~/Git/git_local$ git add test.txt
leo@Dalitek:~/Git/git_local$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
leo@Dalitek:~/Git/git_local$ git reset HEAD test.txt
Unstaged changes after reset:
M test.txt
leo@Dalitek:~/Git/git_local$ git status
On branch master
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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
leo@Dalitek:~/Git/git_local$ git checkout -- test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
leo@Dalitek:~/Git/git_local$
——————————————————————————————————————————————————————————————————————————————————————
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
撤销工作区添加暂存区并有新版本的修改,需要版本回退
leo@Dalitek:~/Git/git_local$ git add *
leo@Dalitek:~/Git/git_local$ git commit -m "撤销工作区添加暂存区并有新版本的修改,需要版本回退"
[master c694db0] 撤销工作区添加暂存区并有新版本的修改,需要版本回退
1 file changed, 1 insertion(+)
leo@Dalitek:~/Git/git_local$ git log
commit c694db0b7851aa1b0bf741424cf2da867f1e37f3 (HEAD -> master)
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 14:38:24 2020 +0800
撤销工作区添加暂存区并有新版本的修改,需要版本回退
commit 7b888c3158062d5d816f7264534d42ac1a137717
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 14:12:43 2020 +0800
再次修改文件,不添加暂存区,做一次提交
commit d559c91e2aa5e22a38dc2e1c6a51df75a94be57c
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 14:06:42 2020 +0800
体会Git的工作原理
commit 8d0180d86fc1b1fc226abe03ec488ca37e9c1264
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:45:25 2020 +0800
版本回退测试
commit 8329eef08c4bb84159bd51931b18eac76ee13398
Author: Leo <1405840008@qq.com>
Date: Fri Nov 6 13:39:09 2020 +0800
1:第一次提交新版本
leo@Dalitek:~/Git/git_local$ git reset --hard HEAD^
HEAD is now at 7b888c3 再次修改文件,不添加暂存区,做一次提交
leo@Dalitek:~/Git/git_local$ git status
On branch master
nothing to commit, working tree clean
leo@Dalitek:~/Git/git_local$ git reset HEAD -- test.txt
leo@Dalitek:~/Git/git_local$ git checkout -- test.txt
leo@Dalitek:~/Git/git_local$ cat test.txt
git 时一个版本控制工具
Git 是一个分布式的版本控制工具
leo@Dalitek:~/Git/git_local$
删除文件
新建一个文件,并提交一个新的版本:
————————————————————————————————————————————————————————————————————————————————————
leo@Dalitek:~/Git/git_local$ ls
test.txt
leo@Dalitek:~/Git/git_local$ vi test.txt
leo@Dalitek:~/Git/git_local$ git add *
leo@Dalitek:~/Git/git_local$ git commit -m "删除文件测试:新建一个文件,并提交一个新的版本"
[master 19f0376] 删除文件测试:新建一个文件,并提交一个新的版本
1 file changed, 1 insertion(+), 2 deletions(-)
leo@Dalitek:~/Git/git_local$ ls
test.txt
leo@Dalitek:~/Git/git_local$
——————————————————————————————————————————————————————————————————————————————————————
删除工作去的文件并删除代码库中的文件:
——————————————————————————————————————————————————————————————————————————————————————
leo@Dalitek:~/Git/git_local$ ls
test.txt
leo@Dalitek:~/Git/git_local$ rm test.txt
leo@Dalitek:~/Git/git_local$ ls
leo@Dalitek:~/Git/git_local$ git rm test.txt
rm 'test.txt'
leo@Dalitek:~/Git/git_local$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test.txt
leo@Dalitek:~/Git/git_local$ ls
leo@Dalitek:~/Git/git_local$
————————————————————————————————————————————————————————————————————————————————————————
使用ssh方式克隆远程仓库
leo@Dalitek:~/Git/git_local$ ssh-keygen -t rsa -C "1405840008@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/leo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/leo/.ssh/id_rsa
Your public key has been saved in /home/leo/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:uCelytY4vBxWJwSeB3S0iMElWxpFf/rPsPhGarPY9hQ 1405840008@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| .+=O.o |
| .X B . |
| + + = . |
| o + |
| = E |
| . B.. |
| .oo+o= |
| +==*=.= |
| .*=+*+ o |
+----[SHA256]-----+
leo@Dalitek:~/Git/git_local$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQChPloeLx7kLSFWrcQ6KmkfcymWg4qhsllGAPFfaJB4DBcjTpEu+rWDtoRTVuE8rU7SpgL7xAtR+XQkVzixfKAz9Zk/AE/hwIpgmxTX8qagLPTD4TDLpL9XRm4zyDOTT8r2GwiOoypQ+bpGDTbEYlvZsmp3jqZ9rMLxDptsujXrnGaPalgQzmmkcwKleJ0TsrnOd26jbmiZ29mwsdmZhD5EsVGNmFietOz5u4o8iLYaa6UFBgJXiVcyDH11wD8Ubk4U//ITVy69ekvcZEOg+lg0k4YoiT1DGNzX+5wt6v2I6/Sdp3G6mnGZfEDN7yVZTeKV5pVSPbptOg2vBbFa1CM6+SjIHFNSvAqpodg4/U5nm060E+P0H+eDXOaeGVD2Ol7ebNNngCttvnhJtPUSSFkcLzq+XXmWPVcn+iL6PFElkR/m2zI2roCf8JTPjHyARphJSwgqPUOoIXnvvmrLmvWxgmlIOU9sU9dGKSytXlkZe+64WSM/Rp15VUWivUb8DnM= 1405840008@qq.com
leo@Dalitek:~/Git/git_local$
添加后在终端输入:
eo@Dalitek:~/Git/git_local$ ssh -T git@gitee.com
ssh: Could not resolve hostname gitee.com: Temporary failure in name resolution
leo@Dalitek:~/Git/git_local$ ping www.baidu.com
ping: www.baidu.com: Temporary failure in name resolution
leo@Dalitek:~/Git/git_local$ sudo vi /etc/resolv.conf
leo@Dalitek:~/Git/git_local$ cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically not access this file directly, but only
# through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a
# different way, replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 8.8.8.8
options edns0 trust-ad
leo@Dalitek:~/Git/git_local$
出现这种情况,是因为没有网络,设置nameserver 8.8.8.8得让自己ubuntu连上网先再执行下面操作:
leo@Dalitek:~/Git/git_local$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (180.97.125.228)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
Hi 刘志成! You've successfully authenticated, but GITEE.COM does not provide shell access.
测试,新建文件夹,用ssh的方式克隆远程仓库,拷贝的ssh协议的链接地址,现象可以表明可以克隆和下拉
leo@Dalitek:~/Git/git_local$ mkdir test
leo@Dalitek:~/Git/git_local$ ls
test
leo@Dalitek:~/Git/git_local$ cd test/
leo@Dalitek:~/Git/git_local/test$ git clone https://gitee.com/liu__zhicheng/GitHub.git
Cloning into 'GitHub'...
remote: Enumerating objects: 23, done.
remote: Counting objects: 100% (23/23), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 23 (delta 4), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (23/23), 3.26 KiB | 151.00 KiB/s, done.
leo@Dalitek:~/Git/git_local/test$ ls
GitHub
leo@Dalitek:~/Git/git_local/test$ cd GitHub/
新建文件,添加修改到暂存区并提交一个新的版本,到本地仓库然后上传至远程仓库
leo@Dalitek:~/Git/git_local/test/GitHub$ mkdir test
leo@Dalitek:~/Git/git_local/test/GitHub$ ls
test
leo@Dalitek:~/Git/git_local/test/GitHub$ cd test/
leo@Dalitek:~/Git/git_local/test/GitHub/test$ touch 1.c 2.c 3.c
leo@Dalitek:~/Git/git_local/test/GitHub$ ls
test
leo@Dalitek:~/Git/git_local/test/GitHub$ git add *
leo@Dalitek:~/Git/git_local/test/GitHub$ git commit -m "GitHub_test"
[master 0a8ca89] GitHub_test
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test/1.c
create mode 100644 test/2.c
create mode 100644 test/3.c
leo@Dalitek:~/Git/git_local/test/GitHub$ git push origin master
Username for 'https://gitee.com': 1405840008@qq.com
Password for 'https://1405840008@qq.com@gitee.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 280 bytes | 93.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/liu__zhicheng/GitHub.git
24a0b76..0a8ca89 master -> master
leo@Dalitek:~/Git/git_local/test/GitHub$
回到码云仓库,上传成功