二、Git常用的命令
命令名称 | 作用 |
---|---|
git config --global user.name 用户名 | 设置用户签名 |
git config --global user.email 邮箱 | 设置用户签名 |
git init | 初始化本地库 |
git status | 查看本地库状态 |
git add 文件名 | 添加到暂存区 |
git commit -m "日志信息" 文件名 | 提交到本地库 |
git reflog | 查看历史记录 |
git reset --hard 版本号 | 版本穿梭 |
2.1 创建Git版本控制
Git的创始人就是Linux的创始人Linus Torvalds
,因此Git除了独特的命令,Linux的命令都可以在这里使用。
①设置用户签名
基本语法
:
git config --global user.name 用户名
git config --global user.email 邮箱
签名的作用:区分不同操作者的身份。Git 首次安装必须设置一下用户签名,否则无法提交代码
。
注意
:这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任何关系。
②初始化本地库
基本语法:
git init
只需要执行一次 这里我已经执行过了
看到.git的文件 就可以证明初始化成功
③查看本地库状态
基本语法:
git status
首次查看本地库状态,可以看出在该分支上并没有任何文件。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xsyoMgPb-1662881503516)(C:\Users\hasee\Pictures\Git\查看状态.png)]
新增文件
操作
vim hello.txt
#i 进入编辑模式
hello world
#wq保存并退出
再次查看本地库状态
(检测到未追踪的文件)
④添加暂缓区
基本语法:
git add 文件名称
查看状态
(检测到暂存区有新文件)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.txt
⑤提交本地库
将暂缓区
的文件提交到本地库
基本语法:
git commit -m "日志信息" 文件名
#没有文件需要提交
hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
下一段实操案例:
#1.修改文件
vim hello.txt
hello world 1111 #追加内容
#2.查看状态(检测到工作区有文件被修改)
git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working
directory)
modified: hello.txt
#3.将修改的文件再次添加到暂缓区
git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working
directory.
#4.查看状态(修改的文件已被提交到暂缓区
git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt
2.2 版本相关操作
基本语法:
git reflog
查看版本信息git log
查看版本详细信git reset --hard 版本号
切换版本
#实操
git reflog #查看历史版本记录
3edd753 HEAD@{1}: commit: seconed time #3edd753 唯一版本标识号
a6e4928 (HEAD -> master) HEAD@{2}: commit (initial): This is my first commit file
#2.切换版本号
git reset --hard a6e4928
HEAD is now at a6e4928 This is my first commit file #HEAD指向a6e4928,提交信息--;
#3.再次查看版本信息 已移动至a6e4928该版本
git reflog
a6e4928 (HEAD -> master) HEAD@{0}: reset: moving to a6e4928 #重点看这
3edd753 HEAD@{1}: commit: seconed time
a6e4928 (HEAD -> master) HEAD@{2}: commit (initial): This is my first commit file
#4.可以查看该版本的文件有何不同
cat hello.txt #1.0版本
Git切换版本,底层其实就是移动head指针
,简单描述: head --> master --> first
三、分支
3.1 什么是分支?
在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独 分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是 一个单独的副本。(分支底层其实也是指针的引用)
3.2 分支的好处?
同时并行推进多个功能开发,提高开发效率。 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。
3.3分支的操作
命令名称 | 作用 |
---|---|
git branch 分支名 | 创建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切换分支 |
git merge 分支名 | 把指定的分支合并到当前分支上 |
案例实操
1) 查看分支
基本命令:
git branch -v
$ git branch -v
* master 087a1a7 my third commit (*代表当前所在的分区)
2) 创建分支
基本命令:
git branch 分支名
hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ git branch demoBranch #创建分支
hasee@hasee MINGW64 /f/Git-space/git-demo (master)
$ git branch -v #查看分支
demoBranch a6e4928 This is my first commit file #刚创建的分支 并将主分支master的内容复制了一份
* master a6e4928 This is my first commit file
3) 修改分支
#在master分支下操作
vim hello.txt
#2.提交暂缓区
git add hello.txt
#3.提交本地库
git commit -m "GG" hello.txt
#4.查看分支
git branch -v
#运行结果
demoBranch a6e4928 This is my first commit file #没有改变
* master 4a8df47 GG #当前master分支已更新为最新一次提交的版本
#查看master分支的文件
cat hello.txt
3.4分支的合并
基本语法:
git merge 分支名称
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-3CbWfvk2-1714662276884)]
[外链图片转存中…(img-8GlWFe0w-1714662276884)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!