前言
最近工作上使用到了git,现在记录一下简单使用。包含代码提交、版本回退、远程仓库、可视化工具(使用Sourcetree、vscode、Git Gui提交代码)等。
使用一个例子演示以上操作。
参考:
廖雪峰git教程
菜鸟git教程
Git互动式教学(互动教学,指令可视化)
一、git是什么?
git是什么:一种分布式版本控制系统
git能干嘛:对代码版本进行管理、多人协作
为什么要用git:因为可以对版本进行管理,所以每一次改动,谁改动的,改动了哪里一目了然,多人写代码,各写各的如果改动有冲突可以解决冲突
二、使用步骤
1.下载安装git
去Git官网(Git Pro中文版)下载git,安装。
打开Git Bash,能查到版本号说明安装好了
$ git --version
git version 2.36.1.windows.1
2.简单配置
配置用户信息(写自己的信息,别抄我的)
$ git config --global user.name "Mike Li"
$ git config --global user.email "1726582664@qq.com"
查看是否设置好
$ git config user.name
Mike Li
$ git config user.email
1726582664@qq.com
3.建立本地仓库
新建文件夹然后右击选择Git Bash Here

初始化本地仓库

检查是否初始化完成,ls是看不到 .git 文件的

4.新建文件
新建文件,并查看是否新建完成

到文件夹里看看初始化和新建的文件

5.代码提交

新建了文件但是这个文件现在属于未跟踪的,也就是说还没有放到本地仓库里
打开文本文件,随便输入点东西

再查看仍是未跟踪状态

将文件add一下,文件就被放到暂存区了,但是等待commit

提交并在双引号里写上这次提交的message,现在文件就被commit 到本地的仓库了

看看是不是有提交

在文本里加点内容然后再提交


现在已经有两个提交了
6.版本回退
如果发现最新的修改其实没必要,还是恢复到第一个版本吧


打开文本,还真恢复了
7.远程仓库Github
登录Github,新建仓库

给仓库起个名字,然后创建

有下面的画面就是创建好了

创建公钥
$ ssh-keygen -t rsa -C "1726582664@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/用户/.ssh/id_rsa):
Created directory '/c/Users/\345\260\244\345\262\201\350\277\236/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/用户/.ssh/id_rsa
Your public key has been saved in /c/Users/用户/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:Sqrdt+TFzuz27HqOXSVHpbxUO/I7l0Dn5apPDZWEU8o 1726582664@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| ooo|
| .+oo+|
| E==+|
| ..==o|
| . S .+o+|
| o . . .Bo|
| . . . o ++o|
| o . o.=.+.+ o|
| . . ..o+B*B.. |
+----[SHA256]-----+
看看有没有创建好

复制公钥准备添加公钥
添加公钥
进入设置

点击添加

Title随便写,公钥粘贴进去就行了

添加之后


本地仓库与远程库联系起来

开始推送
$ git push -u origin master
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 223 bytes | 7.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:Yousuilian/git_study.git
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
刷新一下仓库

代码就推送完成了
8.可视化工具之 Sourcetree
Sourcetree
下载然后添加本地仓库

可以看到现在在master分支上
有提交信息
提交的版本号
谁提交的
什么时候提交的
提交的message
现在不使用指令,而是使用GUI进行提交
文本里添加一行并保存

可视化软件里立马就有反应

现在将文件暂存到暂存区等效于:
$ git add readme.txt

提交到暂存区之后

看看是不是存进去了
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
现在仍然是未提交,点击提交等效于git commit -m “xxxxxxx”

点击之后需要写一下message

写完之后点击提交

现在已经是新的版本了
看看是不是有提交
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git log
commit 36391b83f229b80a12d949c2be5f1e3e1b9f14d8 (HEAD -> master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 15:43:30 2022 +0800
add sourcetree in line 2
commit e8d69ed738c3cb0c24b3f669389b1ec6d1d87751 (origin/master)
Author: LiMing <1726582664@qq.com>
Date: Sat Jul 2 18:58:16 2022 +0800
add first line
现在进行推送到远程仓库
点击推送


确认一下信息然后进行推送
看看是不是推送上去了
$ git log
commit 36391b83f229b80a12d949c2be5f1e3e1b9f14d8 (HEAD -> master, origin/master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 15:43:30 2022 +0800
add sourcetree in line 2
commit e8d69ed738c3cb0c24b3f669389b1ec6d1d87751
Author: LiMing <1726582664@qq.com>
Date: Sat Jul 2 18:58:16 2022 +0800
add first line
可以看到HEAD指向的是master和远程的origin分支
刷新一下Github网页发现确实被推送了

9.可视化工具之 VScode
VScode里面安装插件

在源代码管理里面有按钮,点击打开可视化窗口


和Sourcetree里面一样
文本内容再加一行然后保存
可以看到Git Graph有变化,左边源代码管理窗口也有变化

查看现在的状态
$ git status
On branch master
Your branch is up to date with 'origin/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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
点击暂存更改,等效于git add readme.txt

此时暂存的更改里面已经有了

看看是不是暂存了
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
点击提交

填写message后回车提交

commit里显示有一个提交


刷新一下

看看是不是提交了
$ git log
commit fb032167a29e3d014bca460cb3d21d091ace84a2 (HEAD -> master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 16:11:37 2022 +0800
add use VScode in line 3
commit 36391b83f229b80a12d949c2be5f1e3e1b9f14d8 (origin/master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 15:43:30 2022 +0800
add sourcetree in line 2
commit e8d69ed738c3cb0c24b3f669389b1ec6d1d87751
Author: LiMing <1726582664@qq.com>
Date: Sat Jul 2 18:58:16 2022 +0800
add first line
准备推送到远程库

看看是不是推送了
$ git log
commit fb032167a29e3d014bca460cb3d21d091ace84a2 (HEAD -> master, origin/master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 16:11:37 2022 +0800
add use VScode in line 3
commit 36391b83f229b80a12d949c2be5f1e3e1b9f14d8
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 15:43:30 2022 +0800
add sourcetree in line 2
commit e8d69ed738c3cb0c24b3f669389b1ec6d1d87751
Author: LiMing <1726582664@qq.com>
Date: Sat Jul 2 18:58:16 2022 +0800
add first line
现在HEAD指向master和远程库的origin分支
刷新Github网页

10.可视化工具之 Git Gui
在本地仓库右击选择Git Gui Here

文本再加一行

点击Rescan进行扫描,可以扫描出被修改的文件还没有放进暂存区

点击Stage Change

$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
此时文件被加入暂存区(Stage)
点击文件
写点message
点击commit


看看有没有提交
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git log
commit cf1243216a0c90f58f4522e44ec7e8770220365d (HEAD -> master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 16:29:36 2022 +0800
add use Git Gui in line 4
commit fb032167a29e3d014bca460cb3d21d091ace84a2 (origin/master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 16:11:37 2022 +0800
add use VScode in line 3
commit 36391b83f229b80a12d949c2be5f1e3e1b9f14d8
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 15:43:30 2022 +0800
add sourcetree in line 2
commit e8d69ed738c3cb0c24b3f669389b1ec6d1d87751
Author: LiMing <1726582664@qq.com>
Date: Sat Jul 2 18:58:16 2022 +0800
add first line
点击Push

确认信息之后点Push


推送成功会有新窗口弹出提示
$ git log
commit cf1243216a0c90f58f4522e44ec7e8770220365d (HEAD -> master, origin/master)
Author: Mike Li <1726582664@qq.com>
Date: Sun Jul 3 16:29:36 2022 +0800
add use Git Gui in line 4
现在HEAD指向最新的版本
刷新Github发现的确推送了

总结
以上就是git的简单使用,介绍了最基本的提交和推送代码到远程库,但是还有很多其他内容如分支管理、冲突解决、标签管理都没有提及,待读者自行探索,推荐点击互动教学的链接,比较适合入门,因为每一条指令都很客观、很形象。
本篇只作为一个入门,走一遍基本流程,快速了解代码提交的过程。
另外本篇没有详细介绍每一条指令,只是在使用的时候直接就用了,在使用中体会指令的功能
路漫漫其修远兮,吾将上下而求索。
9175

被折叠的 条评论
为什么被折叠?



