Git最佳实践(init、config、status、add、commit、diff、push) 1.0v

Git最佳实践


工程设计领域中使用版本控制来管理工程蓝图的设计过程,在IT开发过程中借鉴了版本控制思想管理代码的版本迭代。

Git是一个开源的分布式版本控制系统,最初由Linus Torvalds创作,于2005年以GPL发布。最初目的是为了更好的管理Linux内核开发而设计。

在熟练使用之后,官方文档的阅读可以加深对Git的理解。


1.Git的安装

以Windows版本为例,下载好安装包后进行安装,安装路径要求没有中文和空格。然后一直下一步,默认配置就好。

2.Git的结构

在这里插入图片描述
围绕上述图进行展开,大体可以分为三个区:

  • 工作区(Working Files):写代码修改的文件
  • 暂存区(Stage):临时存储,可以撤销修改
  • 本地库:不可以撤销,保存了所有已提交的历史版本,可以进行版本切换

要有这三个区的概念,对后面的Git 命令操作理解会有帮助。

3. Git的版本控制操作

版本控制
1.版本控制最主要的功能就是追踪文件的变更
2.版本控制包括:检入检出控制、分支和合并、历史记录。

3.1 Git本地库的初始化、参数配置、状态查询(init、config、status)
  • git init :添加 .git/ 目录,提供本项目的Git上下文环境
    脚本流程:git init > ls -lA > cd .git/ > ll
admin@DESKTOP-HRU77PG MINGW32 /e/project/Test
$ git init #git初始化,创建.git目录
Initialized empty Git repository in E:/project/Test/.git/

admin@DESKTOP-HRU77PG MINGW32 /e/project/Test (master)
$ ls -lA #.git/目录为隐藏文件,存储本地库的配置和文件的历史版本
total 4
drwxr-xr-x 1 admin 197121 0 816 07:47 .git/

admin@DESKTOP-HRU77PG MINGW32 /e/project/Test (master)
$ cd .git/ #切换.git/目录,查看目录信息

admin@DESKTOP-HRU77PG MINGW32 /e/project/Test/.git (GIT_DIR!)
$ ll
total 7
-rw-r--r-- 1 admin 197121 130 816 07:47 config
-rw-r--r-- 1 admin 197121  73 816 07:47 description
-rw-r--r-- 1 admin 197121  23 816 07:47 HEAD
drwxr-xr-x 1 admin 197121   0 816 07:47 hooks/
drwxr-xr-x 1 admin 197121   0 816 07:47 info/
drwxr-xr-x 1 admin 197121   0 816 07:47 objects/
drwxr-xr-x 1 admin 197121   0 816 07:47 refs/
  • git config :参数配置,第一步就是设置签名
    脚本流程:git config user.name test > git config user.email test@git.com > git config user.name > git config user.email > cat .git/config
$ git config user.name test #设置用户名
$ git config user.email test@git.com #设置用户邮箱
# 上述设置只是签名信息,非远程Git仓库账号

$ git config user.name #查询用户名
$ git config user.email #查询用户邮箱

$ cat .git/config #第二种查询方式,配置的参数在git init命令中添加的.git/目录路
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[user]
        name = test
        email = test@git.com
  • git status:查询工作区、暂存区状态,后面项目操作时,时时查看。
    脚本流程:git status
$ git status #查询工作区、暂存区状态
On branch master	#在master分支

No commits yet	#没有已提交commit

#没有可以commit的文件
nothing to commit (create/copy files and use "git add" to track)	
3.2 Git暂存区的文件添加、文件删除(add、rm --cached)
  • git add :将文件添加到暂存区
  • git rm --cached:将暂存区的文件删除
    脚本流程:vi info.txt > git add info.txt > git rm --cached info.txt
$ ls -lA #查询当前目录文件
total 4
drwxr-xr-x 1 admin 197121 0 817 06:23 .git/

$ vi info.txt #工作区创建info.txt 文件 使用vi命令

$ ls -lA #查询当前目录文件,已经添加成功
total 5
drwxr-xr-x 1 admin 197121  0 817 06:23 .git/
-rw-r--r-- 1 admin 197121 29 816 19:26 info.txt

$ git status #查询工作区、暂存区状态
On branch master #在master分支

No commits yet #没有已提交commit

#有一个可以提交文件,提示用git add <file>来添加文件
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        info.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git add info.txt #添加文件到 暂存区
warning: LF will be replaced by CRLF in info.txt. #文本文件的换行符 LF>CRLF
The file will have its original line endings in your working directory. #就是将工作区的内容添加到暂存区

$ git status #查询工作区、暂存区状态
On branch master #在master分支

No commits yet #没有已提交commit

Changes to be committed: #添加了一个新的文件,可以使用git rm --cached <file>进行回滚
  (use "git rm --cached <file>..." to unstage)

        new file:   info.txt

$ git rm --cached info.txt #对暂存区 git add info.txt 进行回滚
rm 'info.txt'

$ git status #查询工作区、暂存区状态
On branch master #在master分支

No commits yet #没有已提交commit

Untracked files: #我们又回到了未git add之前的状态了
  (use "git add <file>..." to include in what will be committed)

        info.txt

nothing added to commit but untracked files present (use "git add" to track)
3.3 Git本地库的文件添加(commit)
  • git commit :将文件添加到本地库,不可撤销(暂存区的意义就是可以撤销)
    脚本流程:vi info.txt > git add info.txt > git commit info.txt -m “first commit”
$ vi info.txt #工作区继续创建info.txt文件用于测试

$ git add info.txt #将文件添加到暂存区

$ git commit info.txt -m "first commit" #将文件添加到本地库,本地库已记录了本次提交,-m 后面是本次提交的注释信息
[master (root-commit) 3f55dcf] first commit #在master分支上的提交
 1 file changed, 1 insertion(+)
 create mode 100644 info.txt #新建info.txt文件

$ git status #查询工作区、暂存区状态
On branch master #在master分支
nothing to commit, working tree clean #没有可以提交的文件,工作树状态干净
3.4 Git本地库的文件添加的历史版本查询(log)
  • git log :查询commit的日志信息
    脚本流程:git log
$ vi info-2.txt #工作区再添加info-2.txt文件用于版本切换

$ git add info-2.txt #暂存区添加info-2.txt文件

$ git commit info-2.txt -m "second commit" #本地库添加info-2.txt文件

$ git log #查询本地库的commit历史版本信息和当前所在版本
commit 9b4bb11d097d2d6d9bf696ae03e963fb77a84183 (HEAD -> master) #当前所在版本
Author: test <test@git.com>	#签名的意义在此,区别提交的用户信息
Date:   Tue Aug 17 07:25:29 2021 +0800 #提交信息

    second commit #提交的注释信息

commit 3f55dcf471189772048823578ca46f9fe4d098b3 #历史版本
Author: test <test@git.com>
Date:   Tue Aug 17 06:58:27 2021 +0800

    first commit
3.5 Git本地库的文件添加的历史版本切换(reflog、reset)
  • git reflog :查询本地库commits的版本
  • git reset :切换本地库commits的版本,建议使用 --hard 模式(暂存区、工作区的版本和本地库版本一起切换),工作区与暂存区都添加提交到本地库,清空工作区和暂存区的添加修改内容,保证本地库版本硬切换过程中不会丢失修改的内容。
    脚本流程:git reflog > git reset --hard 索引值
$ git reflog #查询本地库历史版本查询,HEAD指针信息
9b4bb11 (HEAD -> master) HEAD@{0}: commit: second commit #索引值、当前指针、提交注释
3f55dcf HEAD@{1}: commit (initial): first commit

$ ll -lA #查询项目文件,存在两个文件,分别为first commit、second commit所提交
total 6
drwxr-xr-x 1 admin 197121  0 817 07:49 .git/
-rw-r--r-- 1 admin 197121 28 817 06:56 info.txt
-rw-r--r-- 1 admin 197121 30 817 07:49 info-2.txt

$ git reset --hard 3f55dcf #切换到 first commit 的索引值
HEAD is now at 3f55dcf first commit

$ ll -lA #查询项目文件,只有info.txt文件,当前项目状态在first commit
total 5
drwxr-xr-x 1 admin 197121  0 817 07:49 .git/
-rw-r--r-- 1 admin 197121 28 817 06:56 info.txt

$ git reset --hard 9b4bb11 #切换到 second commit 的索引值
HEAD is now at 9b4bb11 second commit

$ ll -lA #文件又回来了,当前项目状态在second commit
total 6
drwxr-xr-x 1 admin 197121  0 817 07:49 .git/
-rw-r--r-- 1 admin 197121 28 817 06:56 info.txt
-rw-r--r-- 1 admin 197121 30 817 07:49 info-2.txt
3.6 Git本地库的文件与历史版本比较(diff)
  • git diff :不同版本的内容比较
    脚本流程:vi info.txt > git diff info.txt > git reflog > git diff head 索引值 info.txt
$ vi info.txt #修改文件内容

$ 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:   info.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git diff info.txt #不带参数,比较工作区与暂存区
warning: LF will be replaced by CRLF in info.txt.
The file will have its original line endings in your working directory.
diff --git a/info.txt b/info.txt
index a4b80bd..ea4f1a7 100644
--- a/info.txt
+++ b/info.txt
@@ -1 +1,2 @@
-2021-08-17 06:55 create time
\ No newline at end of file
+2021-08-17 06:55 create time
+2021-08-09 19:55 update time

$ git reflog #查询本地库的commit版本
b9d0204 (HEAD -> master) HEAD@{0}: commit: three commit
9b4bb11 HEAD@{1}: reset: moving to 9b4bb11
3f55dcf HEAD@{2}: reset: moving to 3f55dcf
9b4bb11 HEAD@{3}: commit: second commit
3f55dcf HEAD@{4}: commit (initial): first commit

$ git diff head 9b4bb11 info.txt #比较的是工作区与本地库

4. Git的分支管理操作

分支管理
在这里插入图片描述

1.在Git的版本管理操作中 git status 命令输出的信息显示当前是在名为master分支上
2.在Git的版本管理操作中 git reflog 命令输出的HEAD信息就是在master分支上的版本历史
3.分支可以理解为项目代码的一个副本,可以使我们并行开发,相互不影响,最后各分支副本合并,完成开发任务

4.1 Git本地库的分支查询、创建、切换(branch、checkout)
  • git branch :查询本地的所有分支和当前所在分支
    脚本流程:git branch -v > git branch hot_fix > git checkout hot_fix > git branch -v
$ git branch #查询本地的所有分支
* master
$ git branch -v #查询本地的所有分支(包含当前各分支的当前head值),和当前所在分支(分支前带 * )
* master b9d0204 three commit

$ git branch hot_fix #以本地master分支为基础创建名为hot_fix的分支

$ git branch -v #查询本地的所有分支(包含当前各分支的当前head值)
  hot_fix b9d0204 three commit
* master  b9d0204 three commit

$ git checkout hot_fix #当前分支切换为hot_fix 分支
Switched to branch 'hot_fix'
M       info.txt

$ git branch -v #查询本地的所有分支(包含当前各分支的当前head值),已切换到 hot_fix
* hot_fix b9d0204 three commit
  master  b9d0204 three commit
4.2 Git本地库的分支合并(merge)
  • git merge :将其他分支内容合并到当前分支
    脚本流程:git branch -v > git commit info.txt > git checkout master > git branch -v > git merge hot_fix > git branch -v
$ git branch -v #当前分支在hot_fix,俩个分支的版本一致
* hot_fix b9d0204 three commit
  master  b9d0204 three commit

$ vi info.txt #对文件进行修改

$ git add info.txt #将修改的内容提交到暂存区

$ git commit info.txt -m 'hot_fix update' #将暂存区修改的内容提交到本地库

$ git branch -v #当前分支在hot_fix,俩个分支的版本不一致,master与hot_fix比较已落后
* hot_fix 84cffd0 hot_fix update
  master  b9d0204 three commit

$ git checkout master #切换到要被合并分支master
Switched to branch 'master'

$ git branch -v #再次查询当前分支,确认是否再master上
  hot_fix 84cffd0 hot_fix update
* master  b9d0204 three commit

$ git merge hot_fix #将hot_fix分支合并到当前master分支
Updating b9d0204..84cffd0
Fast-forward
 info.txt | 3 +++
 1 file changed, 3 insertions(+)

$ git branch -v #再次查询,master分支与hot_fix版本保存一致,将hot_fix的新增内容添加到master分支中,合并成功
  hot_fix 84cffd0 hot_fix update
* master  84cffd0 hot_fix update
4.3 Git本地库的分支合并冲突解决(merge)

模拟不同分支(master、hot_fix)修改文件(info.txt)的同一处代码,造成在合并时冲突

$ git branch -v #当前分支master,两个分支的版本一致
  hot_fix 84cffd0 hot_fix update
* master  84cffd0 hot_fix update

$ vi info.txt #在master分支修改info.txt文件
$ git add info.txt #添加到暂存区
$ git commit -m "master update" #添加到本地库

$ git checkout hot_fix #切换到hot_fix分支修改info.txt文件
$ git add info.txt #添加到暂存区
$ git commit -m "hot_fix update" #添加到本地库


$ git checkout master #切换回master分支
$ git merge hot_fix #将hot_fix的修改拉取到master上,合并两个分支
Auto-merging info.txt
CONFLICT (content): Merge conflict in info.txt
Automatic merge failed; fix conflicts and then commit the result.
自动合并 info.txt
CONFLICT(内容):在 info.txt 中合并冲突
自动合并失败; 修复冲突,然后提交结果。

$ cat info.txt #发现有冲突的标识特殊字符>>>>>>>、=======
2021-08-17 06:55 create time
2021-08-09 19:55 update time
2021-08-18 20:07 update time

2021-08-18 21:00 update time

<<<<<<< HEAD
master update
=======
hot_fix update
>>>>>>> hot_fix

$ vi info.txt #修改冲突内容
$ cat info.txt #修改之后
2021-08-17 06:55 create time
2021-08-09 19:55 update time
2021-08-18 20:07 update time

2021-08-18 21:00 update time

master update
hot_fix update

$ git add info.txt #解决冲突之后的文件添加到暂存区

$ git status #查询工作区、暂存区状态
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

        modified:   info.txt

$ git commit -m "解决冲突" #解决合并冲突的提交不能带文件名

5. Git的远程仓库操作

5.1 Git本地库的分支与远程库分支推送、克隆(remote、push、clone)

远程仓库
1.日常开发过程中,本地库的不同分支合并并不多,常常是本地库分支与远程库分支的推送、拉取、合并
2.创建一个GitHub账号作为远程仓库,管理我们的项目。GitHub注册地址 GitHub登录地址
3.创建一个项目,GitHub项目创建地址,设置项目名称,其他默认。复制项目地址https://github.com/…
在这里插入图片描述

  • git remote :添加远程仓库地址别名
  • git push :将本地库的当前分支推送到远程仓库分支
  • git clone :将远程仓库拉取到本地并创建本地库
    脚本流程:git remote -v > git remote add origin https://github.com/… > git remote -v > git branch -v > git push origin master > git clone https://github.com/…
$ git remote -v #查询保存的远程仓库地址,结果无,添加一个

$ git remote add origin https://github.com/... #添加一个名为origin的远程仓库地址,地址为https://github.com/...

$ git remote -v #查询保存的远程仓库地址,就是添加那一个
origin  https://github.com/... (fetch)
origin  https://github.com/... (push)

$ git push origin master #向远程origin的master分支推送,输入远程仓库登录账号与密码

$ git clone https://github.com/... #推送成功后,删除本地库,使用clone拉取远程仓库的项目
Cloning into 'Test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

$ cd Test/ #切换到拉取的项目

$ cat Info.txt #查询文件,确认克隆完成
create for test
5.2 Git本地库的分支与远程库分支拉取、合并到本地库分支(fetch、merge)
  • git fetch :拉取远程仓库的项目到本地库分支
  • git merge :不同分支项目合并
    脚本流程:git fetch origin master -v > git checkout origin/master > cat Info.txt >git checkout master > git merge origin/master >cat Info.txt

在GitHub上修改文件
在这里插入图片描述

$ git status #查询工作区、暂存区状态
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

$ cat Info.txt #查看本地的文件信息
create for test

$ git fetch origin master #拉取远程origin 的 master分支到本地
From https://github.com/itbtt/Test
 * branch            master     -> FETCH_HEAD

$ git checkout origin/master #切换到拉取的默认分支

$ cat Info.txt #查看内容,在GitHub上修改的信息已经拉取到本地
create for test
update for remote

$ git checkout master #切换会本地分支
Previous HEAD position was 6dad440... Update Info.txt
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

$ git merge origin/master #合并分支到merge上
Updating b7657a1..6dad440
Fast-forward
 Info.txt | 1 +
 1 file changed, 1 insertion(+)

$ cat Info.txt #查看内容,本地的master分支也拉取到了远程仓库的内容
create for test
update for remote
5.3 Git本地库的分支与远程库分支冲突解决(pull)
  • git pull :是fetch、merge结合版命令
  • 当本地库代码修改完成,向远程进行推送时,为了避免与其他修改的代码起冲突,一般先拉取远程仓库的代码,在本地仓库中解决冲突,再进行推送
$ git pull origin master #一定要先拉取远程仓库的代码,检查是否有冲突
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/itbtt/Test
 * branch            master     -> FETCH_HEAD
   6dad440..90bbc5e  master     -> origin/master
Auto-merging Info.txt
CONFLICT (content): Merge conflict in Info.txt
Automatic merge failed; fix conflicts and then commit the result.

#由于远程仓库修改了,并且在本地仓库也对Info.txt文件做了修改,发生了从图

$ cat Info.txt #查看代码,发现冲突的标志,修改去除冲突的标志
create for test
<<<<<<< HEAD
update for remote
update for local
=======
update for remote
update for remote merge
>>>>>>> 90bbc5efa57025dd1ec7e6879af4ea1484b5344c

$ cat Info.txt #冲突解决之后
create for test
update for remote
update for local
update for remote
update for remote merge

$ git add Info.txt #修改的内容添加到暂存区
$ git commit -m "merge local" #修改的内容添加到本地库
[master b38e49a] merge local

$ git push origin master #将本地库的代码推送至远程仓库master分支中,在弹出的框中输入账号密码
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 505 bytes | 126.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:itbtt/Test.git
   90bbc5e..b38e49a  master -> master

在GitHub上检查文件是否推送成功
在这里插入图片描述

6. Git操作总结

  • 本地代码的修改完成后,使用add添加到暂存区,此时已修改添加的可以rm --cached进行回滚。
  • 使用commit将修改的内容添加到本地库中,并需要填写此次提交的注释信息,注释信息有利于历史版本追溯。
  • 使用reflog进行历史版本追溯,使用diff查看当前版本与历史某个版本的内容差别,reset设置当前本地库的历史版本。
  • 使用Git实现本地代码的版本控制之后,理解分支的意义,brach分支查询和添加,checkout切换当前分支,使用分支实现并行开发。使用merge合并各个分支,解决冲突,形成完整项目代码。
  • 使用remote对远程仓库地址进行添加删除,使用push把本地项目推送到远程仓库,使用pull拉取远程仓库到本地,并使用merge解决冲突。

最后Git内容进一步精简就是提交暂存区保存本地库历史版本查询回滚分支开发远程仓库推送拉取,初步实现Git的日常使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值