git分布式版本控制系统(二)

avatar
作者
筋斗云
阅读量:1

目前世界上最先进的分布式版本控制系统

官方网址:https://git-scm.com

学习目标:

1 了解 git 前世今生
2 掌握 git 基础概念、基础操作
3 各种 git 问题处理
4 互联网常用 gitflow(工作流程规范)
5 git 代码提交规范
6 git 分支管理及命名规范

git 操作

目前执行git操作有两种风格,命令行格式 || 图形化操作,文档中以命令行来讲解

3.1 配置用户名/邮箱,git初始化

Shell

// 全局配置 设置提交用户名称 提交邮箱 此处的名称可以自己定义 $ git config --global user.name "wangjing" // 邮箱需要为给予权限的邮箱 此处的邮箱是一个例子 $ git config --global user.email wang.jing@okshu.com  // 当前工作目录的配置 $ pwd /Users/wangjing/work/wtswangjing // 配置当前工作目录的用户名 为初始化git 所以报错 $ git config user.name "wtswangjing" fatal: not in a git directory // git初始化  初始化便是 建立本地仓库 $ git init Initialized empty Git repository in /Users/wangjing/work/wtswangjing/.git/ // 配置当前工作目录的用户名+邮箱  // 上述这两个不同的邮箱可以理解  $ git config user.name "wtswangjing" $ git config user.email 8829668+wtswangjing@user.noreply.gitee.com  // 在当前目录下 全局和当前工作目录 就是两个独立的用户名+邮箱,可以同时投身于不相关的项目 $ git config user.name wtswangjing $ git config --global user.name wangjing 
3.2 创建远程仓库

这是两个代码托管平台,可自行注册
https://gitee.com
https://github.com/

创建好的个人仓库
在这里插入图片描述

3.3 克隆仓库

克隆远程仓库至本地仓库,克隆地址在3.2创建好的远程仓库页面获取,ssh\https 地址均可
Shell

// git clone 通过https获取代码 wtswangjing:wtswangjing wangjing$ git clone https://gitee.com/wtswangjing/public.git Cloning into 'public'... remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (4/4), done. remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (4/4), done. wtswangjing:wtswangjing wangjing$ cd public/ // 代码与创建代码仓库内容一致,包含两个文件 wtswangjing:public wangjing$ ls README.en.md    README.md // 查看远程仓库指向地址链接 与我们创建的地址链接一致 wtswangjing:public wangjing$ git remote -v origin  https://gitee.com/wtswangjing/public.git (fetch) origin  https://gitee.com/wtswangjing/public.git (push) // 查看当前所在分支 wtswangjing:public wangjing$ git branch * master 
3.4 编辑文件 + 提交文件 + 推到远程仓库

Shell

// 查看当前工具区文件变更列表 wtswangjing:public wangjing$ 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 checkout -- <file>..." to discard changes in working directory)          modified:   README.en.md no changes added to commit (use "git add" and/or "git commit -a") // 对比变更文件的具体细节 wtswangjing:public wangjing$ git diff README.en.md diff --git a/README.en.md b/README.en.md index f222ece..e305574 100644 --- a/README.en.md +++ b/README.en.md @@ -1,4 +1,4 @@ -# wtswangjing +# wtswangjing 20230517 16:12:23 // 将工作区文件 添加至 暂存区 wtswangjing:public wangjing$ git add README.en.md wtswangjing:public wangjing$ git status On branch master Your branch is up to date with 'origin/master'.  Changes to be committed:   (use "git reset HEAD <file>..." to unstage)          modified:   README.en.md // 将暂存区文件添加至本地仓库 wtswangjing:public wangjing$ git commit -m "feat:修改注释" [master 66fa9b9] feat:修改注释  1 file changed, 1 insertion(+), 1 deletion(-)  // 将本地仓库上传至远程仓库 wtswangjing:public wangjing$ git push // 需要填写gitee网站注册时的账号和密码,校验当前用户是否有修改代码的权限 Username for 'https://gitee.com': 13810141428 Password for 'https://18234139597@gitee.com':  Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 10 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 352 bytes | 352.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/wtswangjing/public.git // 66fa9b9 是最近提交的commit hash,关于commit hash的详细意义可以https://code84.com/836725.html查看    e2c9453..66fa9b9  master -> master // 查看当前分支提交记录以及 如果是由A分支衍生到当前分支,A分支中的log也有 wtswangjing:public wangjing$ git log commit 66fa9b92b1d0b6491fcd999e22dc27b6a23753cc (HEAD -> master, origin/master, origin/HEAD) Author: wangjing <wang.jing@okshu.com> Date:   Wed May 17 16:13:53 2023 +0800      feat:修改注释  commit e2c94535a1bb6d294c0f78a0dd44665440dd65cf Author: wtswangjing <8829668+wtswangjing@user.noreply.gitee.com> Date:   Wed May 17 06:37:38 2023 +0000      Initial commit 

成功提交,查看远程代码是否更新

可以看到,我们的这个提交已经成功了,66fa9b9的 commit hash 也是完全对应的,24分钟是因为截图时忙了会儿其他工作,https://gitee.com/wtswangjing/public/commit/66fa9b92b1d0b6491fcd999e22dc27b6a23753cc 可以查看具体提交内容,与我们本地开发完全一致

3.5 git概念

在这里插入图片描述

•Directory: 想要设置成git工作目录的文件夹,在这个目录下git init初始化本地库,生成一个隐藏的.git文件夹,在这个目录下放置代码文件,即为工作区;在我们的例子中为:/Users/wangjing/work/wtswangjing/
•WorkSpace:放置需要上传git的项目代码或相关文件,这些目录和文件组成了工作区;在我们的例子中为:/Users/wangjing/work/wtswangjing/public
•.git:存放git管理详细的目录(包含如图上的版本库(包含暂存区和本地库)和其他文件),git init初始化时候自动创建的(没事不要动这些文件)在我们的例子中为:/Users/wangjing/work/wtswangjing/.git
Shell

wtswangjing:.git wangjing$ ls -l total 24 -rw-r--r--   1 wangjing  staff   23 May 17 14:44 HEAD drwxr-xr-x   2 wangjing  staff   64 May 17 14:44 branches -rw-r--r--   1 wangjing  staff  216 May 17 14:44 config -rw-r--r--   1 wangjing  staff   73 May 17 14:44 description drwxr-xr-x  13 wangjing  staff  416 May 17 14:44 hooks drwxr-xr-x   3 wangjing  staff   96 May 17 14:44 info drwxr-xr-x   4 wangjing  staff  128 May 17 14:44 objects drwxr-xr-x   4 wangjing  staff  128 May 17 14:44 refs 

•index/Stage:暂存区,工作区中的文件要是git add 或者用git add . 加入到暂存区,就保存在这里;
•Local Repo:本地仓库,一个存放在本地的版本库;HEAD是当前开发的分支(branch);
•Stash:是一个工作状态保存栈,用于保存/恢复WorkSpace临时状态;

3.6 撤消操作

场景举例:
变更两个文件,add 一个文件,commit之后,还想 add 另外一个文件
如果直接add+commit 的话 会产生两条commit 记录,可以使用上述命令追加add文件,保持1条commit 记录
Shell

wtswangjing:public wangjing$ git add README.en.md wtswangjing:public wangjing$ git commit -m "feat:readme" [master c5ad515] feat:readme  2 files changed, 1 insertion(+)  create mode 100644 readme17.md wtswangjing:public wangjing$ git log commit c5ad515d419e4f0a8c7043d2b30726ed0bc36d39 (HEAD -> master) Author: wangjing <wang.jing@okshu.com> Date:   Wed May 17 17:04:16 2023 +0800      feat:readme  commit 66fa9b92b1d0b6491fcd999e22dc27b6a23753cc (origin/master, origin/HEAD) Author: wangjing <wang.jing@okshu.com> Date:   Wed May 17 16:13:53 2023 +0800      feat:修改注释 wtswangjing:public wangjing$ git add readme17.md wtswangjing:public wangjing$ git commit --amend [master 0ff5e6f] feat:readme+1  Date: Wed May 17 17:04:16 2023 +0800  2 files changed, 2 insertions(+)  create mode 100644 readme17.md wtswangjing:public wangjing$ git log commit 0ff5e6f2a0d4efcf4f80b4c23c3642cc7dffa7eb (HEAD -> master) Author: wangjing <wang.jing@okshu.com> Date:   Wed May 17 17:04:16 2023 +0800      feat:readme+1  commit 66fa9b92b1d0b6491fcd999e22dc27b6a23753cc (origin/master, origin/HEAD) Author: wangjing <wang.jing@okshu.com> Date:   Wed May 17 16:13:53 2023 +0800      feat:修改注释 

场景举例
已经修改了一个文件,然后 git add 放入暂存区,再恢复到工作区,恢复至工作区时是带有本地的修改的,再去将文件恢复到原貌
Shell

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 checkout -- <file>..." to discard changes in working directory)          modified:   README.en.md no changes added to commit (use "git add" and/or "git commit -a") wtswangjing:public wangjing$ git add README.en.md wtswangjing:public wangjing$ git status On branch master Your branch is up to date with 'origin/master'.  Changes to be committed:   (use "git reset HEAD <file>..." to unstage)          modified:   README.en.md wtswangjing:public wangjing$ git reset head README.en.md Unstaged changes after reset: M       README.en.md wtswangjing:public wangjing$ 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 checkout -- <file>..." to discard changes in working directory)          modified:   README.en.md wtswangjing:public wangjing$ git checkout README.en.md wtswangjing:public wangjing$ git status On branch master Your branch is up to date with 'origin/master'. 
3.7 git 分支

Shell

// 创建一个名叫testing的分支 wtswangjing:public wangjing$ git branch testing // 查看本地有哪些分支 git branch -a 查看远程有哪些分支 wtswangjing:public wangjing$ git branch * master   testing // 切换到testing分支 wtswangjing:public wangjing$ git checkout testing Switched to branch 'testing'   // 创建并切换到testing1分支 wtswangjing:public wangjing$ git checkout -b testing1 Switched to a new branch 'testing1'   // -d 删除本地分支 -D删除远程分支 wtswangjing:public wangjing$ git branch -d testing Deleted branch testing (was 93a2458).  // 对分支testing1的内容做一处修改,提交到本地仓库 wtswangjing:public wangjing$ git commit -a -m "feat:add 1" [testing1 f909690] feat:add 1  1 file changed, 1 insertion(+), 1 deletion(-)    // 当前远程仓库并没有这个分支,所以推送失败,并给出意见,推送时绑定远程分支 wtswangjing:public wangjing$ git push fatal: The current branch testing1 has no upstream branch. To push the current branch and set the remote as upstream, use      git push --set-upstream origin testing1 // 绑定并推送远程分支 wtswangjing:public wangjing$     git push --set-upstream origin testing1 Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 10 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 341 bytes | 341.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-6.4] remote: Create a pull request for 'testing1' on Gitee by visiting: remote:     https://gitee.com/wtswangjing/public/pull/new/wtswangjing:testing1...wtswangjing:master To https://gitee.com/wtswangjing/public.git  * [new branch]      testing1 -> testing1 Branch 'testing1' set up to track remote branch 'testing1' from 'origin'.  // 接下来是 使用git merge 合并代码 // 推送成功,由当前分支切换至master分支 wtswangjing:public wangjing$ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. // 将testing1分支的内容,合并到master分支 wtswangjing:public wangjing$ git merge testing1 Updating 93a2458..f909690 Fast-forward  README.en.md | 2 +-  1 file changed, 1 insertion(+), 1 deletion(-) // 合并成功,推送远程分支  wtswangjing:public wangjing$ git push Total 0 (delta 0), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/wtswangjing/public.git    93a2458..f909690  master -> master   // 接下来重新创建一个分支testing2 使用 git rebase 进行分支合并   wtswangjing:public wangjing$ git checkout -b testing2 Switched to a new branch 'testing2' wtswangjing:public wangjing$ git branch   master   testing1 * testing2 wtswangjing:public wangjing$ git commit -a -m "rebase test" [testing2 87b96b5] rebase test  1 file changed, 1 insertion(+) wtswangjing:public wangjing$ git push --set-upstream origin testing2  * [new branch]      testing2 -> testing2 Branch 'testing2' set up to track remote branch 'testing2' from 'origin'. wtswangjing:public wangjing$  wtswangjing:public wangjing$ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. // 对同一行进行了改动 放入本地仓库 wtswangjing:public wangjing$ git commit -a -m "rebase master" [master ff082b7] rebase master  1 file changed, 1 insertion(+)  // 使用rebase 命令合并 wtswangjing:public wangjing$ git rebase testing2 First, rewinding head to replay your work on top of it... Applying: rebase master Using index info to reconstruct a base tree... M       README.en.md Falling back to patching base and 3-way merge... Auto-merging README.en.md // 同一行爆发冲突 CONFLICT (content): Merge conflict in README.en.md error: Failed to merge in the changes. Patch failed at 0001 rebase master hint: Use 'git am --show-current-patch' to see the failed patch  Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort".  wtswangjing:public wangjing$ git status rebase in progress; onto 87b96b5 You are currently rebasing branch 'master' on '87b96b5'.   (fix conflicts and then run "git rebase --continue")   (use "git rebase --skip" to skip this patch)   (use "git rebase --abort" to check out the original branch)  Unmerged paths:   (use "git reset HEAD <file>..." to unstage)   (use "git add <file>..." to mark resolution)          both modified:   README.en.md    no changes added to commit (use "git add" and/or "git commit -a") // 根据提示文件,去文件中 将无用代码删除,即可以解决冲突 wtswangjing:public wangjing$ git add README.en.md // 不需要commit 直接推送 推送时当前在一个虚拟分支,需要 指定远程分支 wtswangjing:public wangjing$ git push fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use      git push origin HEAD:<name-of-remote-branch>  wtswangjing:public wangjing$ git branch * (no branch, rebasing master)   master   testing1   testing2 wtswangjing:public wangjing$ git push origin master Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 10 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 339 bytes | 339.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/wtswangjing/public.git    f909690..ff082b7  master -> master wtswangjing:public wangjing$ git log commit ff082b75f2ab4fe5039a9e94aac7af585800b4af (HEAD -> master, origin/master, origin/HEAD) Author: wangjing <wang.jing@okshu.com> Date:   Wed May 17 18:03:26 2023 +0800      rebase master 

命令补充
Shell

# 比如要把topic的分支变基到master上,则先切到topic,然后执行 git rebase master # 或者 git rebase master topic # 放弃合并,回到rebase操作之前的状态 git rebase --abort # 则会将引起冲突的commits丢弃掉(慎用!!) git rebase --skip # 合并冲突,结合"git add 文件"命令一起用与修复冲突,提示开发者,一步一步地有没有解决冲突。 git rebase --continue # 将多个提交合并为一次提交 git rebase -i HEAD~2 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!