学习笔记|Git
本文最后更新于:9 个月前
很久以前在公众号上开了一个坑,三分钟一起窥探Git门缝(上),迟迟没有写下,最近刚好有时间系统的过一遍 git,熟悉熟悉其各类应用场景,算是填坑完毕⛳️。
目录
1.user信息管理
2.暂存区管理
3.版本库管理
4.commit 管理
(1)更改最近一次提交的版本信息
(2)更改老旧 commit 的 message
(3)多个连续的commit合并成一个
(4)多个间隔的commit合并成一个
7.查看log
8.分支管理
9.比较版本差异
10..git文件夹内容
11.对象之间的关系
12.分离头指针
13.处理紧急任务
14.不需要git管理的文件
15.备份与同步
16团队协作的场景
(1)不同人同时修改不同区域
(2)不同人同时修改同一区域
(3)更改文件名对他人影响
(4)不使用 git push -f
(5)不对公共 commit
做变基
17.工作流
(1)主干开发
(2)Git Flow
(3)Github Flow
(4)GitLab Flow(带生产分支)
(5)GitLab Flow(带环境分支)
(6)GitLab Flow(带发布分支)
18.分支集成策略
(1)Create a merge commit
(2)Squash and merge
(3)Rebase and merge
1.user信息管理
###配置user信息
git config --global user.name 'your_name'
git config --global user.email 'your_email@domain.com'
--global :全球
--local :本地,单个项目中使用
###显示user信息
git config --list --global
###编辑配置文件
git config --global --edit
2.暂存区管理
git add readme.md #添加到暂存区
git add -u #将更改过的、已经在git中管理的文件,全都添加到暂存区
--------------------------------------------
git reset HEAD #将暂存区恢复成和HEAD一致(即清空暂存区),不改变工作区
git reset HEAD -- 1.txt 2.txt #取消暂存区部分文件的更改,不改变工作区
--------------------------------------------
git checkout -- 2.txt #将工作区文件恢复和暂存区一致
英文:
use "git reset HEAD <file>..." to unstage
#unstage:不发生 stage:使发生
(use "git checkout -- <file>..." to discard changes in working directory)
#discard:丢弃、放弃 directory:目录
3.版本库管理
git status #查看状态
--------------------------------------------
git commit -m'版本信息' #提交到git中
git commit -am'版本信息' #对于修改的工作区的文件直接提交到版本库里
--------------------------------------------
git reset --hard aaaa6666 #撤销版本库,撤销暂存区,撤销工作区,更改到aaaa6666版本
git reset --hard HEAD #将暂存区和工作区恢复和HEAD一致
--------------------------------------------
git mv file_name new_file_name #更改文件名
git rm file_name #删除版本库中文件
###更改完需要commit一次
4.commit 管理
(1)更改最近一次提交的版本信息
git commit --amend
(2)更改老旧 commit 的 message
更改
message
,必将更改此次commit
的hash
值;更改此次commit
的hash
值,就要基于其父亲commit
来改变,这叫rebase
。根据下方的提示,将
pick
改成r
git rebase -i aaaa6666 # aaaa6666为所修改commit的 父亲commit 的hash值 ###得到以下交互信息: pick 17eeb4a change index ###将要修改的massage,pick改为r pick 3c052e5 move readme.md to readmine + Add readme ###不需修改的massage,保持 pick pick 0abe5ee rm rqeq1 ###不需修改的massage,保持 pick # Rebase e7d6113..0abe5ee onto aaaa6666 (3 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted.
随后跳出的页面就是修改 massage。
change index ### 修改成自己想要的 massage
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: ...
#
# interactive rebase in progress; onto aaaa6666
# Last commands done (1 commands done):
# reword e7d6113 ...
# Next command to do (0 remaining command):
# You are currently editing a commit while rebasing branch 'master' on 'aaaa6666'.
#
# Changes to be committed:
# modified: ...
(3)多个连续的commit合并成一个
meld:合并;previous:以前的
根据下方的提示,将 pick
改成 s
git rebase -i aaaa6666 #aaaa6666为合并的四个commit的 父亲commit 的hash值
###1.要合并前三条commit
pick d93a5a2 Add images+index #----必须保留一条pick
s e7d6113 add inin #----pick改为s
s 17eeb4a change index #----pick改为s
pick 3c052e5 move readme.md to readmine + Add readme
pick 634bfa3 rm rqeq1+2
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# You are editing the todo file of an ongoing interactive rebase.
-- INSERT --
###2.添加合并的massage
# This is a combination of 3 commits. #----在此处添加massage
# This is the 1st commit message:
Add images+index
# This is the commit message #2:
add inin
# This is the commit message #3:
change index
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sat Mar 7 20:32:23 2020 +0800
#
# interactive rebase in progress; onto cef862a
# Last commands done (3 commands done):
# squash e7d6113 add inin
# squash 17eeb4a change index
# Next commands to do (2 remaining commands):
# pick 3c052e5 move readme.md to readmine + Add readme
# pick 634bfa3 rm rqeq1+2
# You are currently rebasing branch 'master' on 'cef862a'.
#
-- INSERT --
(4)多个间隔的commit合并成一个
- 找到最老旧的
commit
的parents
- 手动在最上面添加
pick parents_hash
- 将要合并的
commit
剪切到pick parents_hash
下面,并将pick
改成s
- 更改
massage
7.查看log
git log --oneline #简介的查看log(当前分支)
git log --oneline -n2 #简介的查看log,最近的2条log(当前分支)
git log --all #查看所有分支的log日志
git log --all --graph #查看分支父子关系
8.分支管理
git branch -v #查看本地有多少分支
--------------------------------------------
git checkout master #切换分支
git checkout -b tmp2 413c5d9b76 #基于413c5d9b76创建新的分支tmp2
git checkout -b tmp2 tmp #基于分支tmp创建新分支tmp2
git checkout 413c5d9b76 #分离头指针(detached HEAD),基于413c5d9b76创建一个没有分支的工作区
--------------------------------------------
git branch -d branch_name #删除分支
--------------------------------------------
git merge master dev #将 master 分支和 dev 分支进行合并
9.比较版本差异
git diff a0a5992 35f7cdf #比较两个commit之间的差异
--------------------------------------------
git diff HEAD HEAD^1 #比较当前分支的commit 和 父亲的commit 之间的差异
git diff HEAD HEAD^
git diff HEAD HEAD~1
--------------------------------------------
git diff HEAD HEAD^^ ##比较当前分支的commit 和 父亲的父亲的commit 之间的差异
git diff HEAD HEAD~2
--------------------------------------------
git diff --cached #比较当前版本库 和 暂存区的差异
git diff #比较工作区 和 暂存区的差异
git diff --file_name #比较工作区和暂存区中,关于file_name的差异
--------------------------------------------
git diff tmp master #比较两个分支的差异
git diff tmp master -- index.html #比较两个分支中index.html的差异
10.git文件夹内容
cat HEAD #当前所在分支,是个引用,指向了refs/heads中当前所在分支
cat config #配置文件
cd refs/tags #标签
cd refs/heads #版本库(哈希值)
11.对象之间的关系
- 每次提交都会生成一个
commit
对象【相当于一个快照】 - 一个
commit
对应一棵树,【相当于一个文件夹】 - 只要内容一样,即使文件名不一样,也是同一个文件【
hash
值标记】
git cat-file -t aee370 #查看该哈希值的对象类型【commit、blob、tree、tag】
git cat-file -p aee370 #查看该哈希值的对象内容
12.分离头指针
git checkout 413c5d9b76 #分离头指针(detached HEAD),基于413c5d9b76创建一个没有分支的工作区
###创建一个没有分支的工作区,注意:当切换到其他分支时,此工作区将被当作垃圾丢弃,此处会产生以下提醒:
➜ use_git git:(a0a5992) git checkout master
Warning: you are leaving 2 commits behind, not connected to
any of your branches:
a0a5992 2.txt
35f7cdf 1.txt
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> a0a5992
Switched to branch 'master'
###此时若要基于此工作区创建新的分支,使用命令可创建新的分支:
git branch <new-branch-name> a0a5992
13.处理紧急任务
在处理
task01
时,需要紧急完成task02
,此时需要工作区和暂存区为clean
。解决方案: 将
工作区
和暂存区
的已修改的文件
,放入堆栈中(工作区的新建的文件无法放入,需要先git add
)。
git stash #将工作区和暂存区的任务放到堆栈中
git stash pop #将文件从堆栈中弹出来,并删除堆栈信息
git stash apply #将文件从堆栈中弹出来,不删除堆栈信息
git stash list #查看堆栈信息
14.不需要git管理的文件
- 不需要git管理的文件,写入
.gitignore
文件中。 .gitignore
文件中doc
表示名叫doc的文件
和名叫doc的文件夹
都不管理.gitignore
文件中doc/
表示管理名叫doc的文件
,但不管理名叫doc的文件夹下的内容
15.备份与同步

# 添加远程仓库(git_name 相当于 地址的别名)
git remote add git_name file://path/.git
git remote add git_name git@github.com:AimTao/use_git.git
# 查看远程仓库
git remote -v
# 删除远程仓库
git remote remove git_name
--------------------------------------------
# 将本地同步到远程仓库
git push git_name --all
git push git_name branch_name
--------------------------------------------
# 将远程仓库克隆到本地
git clone --bare file://path/.git new_git_name
git clone git@github.com:AimTao/use_git.git #默认名为远程仓库名
git clone git@github.com:AimTao/use_git.git new_git_name #自定义名称
--------------------------------------------
# 获取(fetch)远端的改动,不合并(merge)
git fetch git_name branch_name
# 获取(fetch)并合并(merge)远端的改动
git pull git_name branch_name
remote:遥远的 fetch:取回 merge:合并
16团队协作的场景
(1)不同人同时修改不同区域
# 1.先把远端的仓库fetch下来
git fetch git_name
# 2.再将本地和远端仓库合并
git merge remote_branch_name
# 3.合并后push到远端仓库
git push git_name branch_name
--------------------------------------------
# 其中 1&2 可以用pull命令代替
# 将远端的仓库pull下来(fetch+merge)
git pull git_name
(2)不同人同时修改同一区域
手动修改该文件,并删掉merge的提示信息
(3)更改文件名对他人影响
1号队员更改文件名并push,2号队员修改内容发现push被reject。
2号队员直接pull即可。
(4)不使用 git push -f
(5)不对公共 commit
做变基
只能对 本地commit
做变基,一旦 push
到远端仓库,就不可以 pull
下来,做变基
17.工作流
(1)主干开发

(2)Git Flow
复杂,不推荐
(3)Github Flow
在特性分支开发,再集成到 master

(4)GitLab Flow(带生产分支)
在 Github Flow基础上, 用 master 做集成,用 production 来做缓冲,做上线

(5)GitLab Flow(带环境分支)
在上线之前增加环境测试

(6)GitLab Flow(带发布分支)
需要维护不同版本

18.分支集成策略
Github 的 marge 选项

(1)Create a merge commit
将特性分支 merge 到主干分支

(2)Squash and merge
将特性分支的 commit 合成一条 commit 并提交到主干分支,(保留特性分支)

(3)Rebase and merge
将特性分支的 commit 直接复制到主干分支

本博客所有文章均个人原创,除特别声明外均采用 CC BY-SA 4.0协议,转载请注明出处!