Git介绍及测试
一、git介绍1.概念Git 是分布式版本控制和源代码管理系统重点使用和管理代码的速度。 Git 最初是由Linus Torvalds设计开发的用于管理Linux内核开发。Git 是根据GNU通用公共许可证版本2的条款分发的自由/免费软件。//更多请阅读https://www.yiibai.com/git2.术语Workspace工作区Index/Stage暂存区也叫索引Repository仓库区或本地仓库也存储库Remote远程仓库3.关键词工作区:通过git init创建的代码库的所有文件但是不包括.git文件(版本库)暂存区:通过git add ./*/Xxx/Xxxx添加的修改,都是进入到暂存区了,肉眼不可见 通过 git status 可以看到修改的状态。**版本库**工作区中的.git的文件这个属于版本库提交的版本都到了这个文件里面不要随便修改这个文件夹的东西二、Git使用测试1.clone远程仓库到本地git clone命令将远程仓库克隆到本地[rootk8s-master git]# git clone https://gitee.com/ops-mx/test.gitCloning intotest... Usernameforhttps://gitee.com:805xxxxxxqq.com // 这里输入的账号密码是gitee的账密 Passwordforhttps://805xxxxxxqq.comgitee.com:remote: Enumerating objects:9, done. remote: Counting objects:100%(9/9), done. remote: Compressing objects:100%(9/9), done. remote: Total9(delta0), reused0(delta0), pack-reused0Unpacking objects:100%(9/9), done.查看克隆信息会发现在本地生成了一个仓库名称的目录[rootk8s-master git]# lstest添加远程仓库信息这一步是为了push做准备[rootk8s-master test]# git remote add test gitgitee.com:ops-mx/test.git[rootk8s-master test]# git remote showorigintest查看git配置信息注意当前在test仓库目录下git config拿的信息其实是.git目录下的config内容[rootk8s-master test]# git config --listuser.nameops-mxuser.email805xxxxxxqq.compush.defaultsimplecore.repositoryformatversion0core.filemodetruecore.barefalsecore.logallrefupdatestrueremote.origin.urlhttps://gitee.com/ops-mx/test.gitremote.origin.fetchrefs/heads/*:refs/remotes/origin/*branch.master.remoteoriginbranch.master.mergerefs/heads/masterremote.test.urlgitgitee.com:ops-mx/test.gitremote.test.fetchrefs/heads/*:refs/remotes/test/*branch.xxx.remotetestbranch.xxx.mergerefs/heads/xxx[rootk8s-master test]# pwd/data/git/test[rootk8s-master test]# ls -a....git .gitee .gitignore gohomework LICENSE README.en.md README.md[rootk8s-master test]# cd .git[rootk8s-master .git]# lsbranches COMMIT_EDITMSG config description HEAD hooks index info logs objects ORIG_HEAD packed-refs refs[rootk8s-master .git]# cat config[core]repositoryformatversion0filemodetruebarefalselogallrefupdatestrue[remoteorigin]urlhttps://gitee.com/ops-mx/test.git fetchrefs/heads/*:refs/remotes/origin/*[branchmaster]remoteorigin mergerefs/heads/master[remotetest]urlgitgitee.com:ops-mx/test.git fetchrefs/heads/*:refs/remotes/test/*[branchxxx]remotetestmergerefs/heads/xxx如果有多个项目仓库时只需要切换目录就能查看各项目的信息因为git拿的是项目目录下的.git信息[rootk8s-master git]# pwd/data/git[rootk8s-master git]# ls // 我这里有两个项目仓库一个是test一个是golang12golang12test[rootk8s-master git]# cd golang12/[rootk8s-master golang12]# lscourse_doc go.mod go.work homework README.md[rootk8s-master golang12]# git log // golang12仓库下的信息commit a5b7cd8818d42d812d24e2bd1e03ab44e3fc0022 Author: dbdbMacBook-Pro.localDate: Wed Jun2810:55:3220230800addhomework week7 commit cd66a7819cd3eef4ea2464bbf42d2e1b8b7d668c Merge: e3f744d b81e51d Author: xiaowei1244******qq.comDate: Tue Jun2717:32:3420230800 Merge branchmasterof https://gitee.com/magedu/golang12 commit e3f744d615950f719372f42655571bd38985f2d5 Author: xiaowei1244******qq.comDate: Tue Jun2717:31:4620230800 Update main.go[rootk8s-master golang12]# cd ../test/ // 切换目录就能查看test仓库的信息了[rootk8s-master test]# git logcommit ffc29038a73c2f03f932d86c6ac35cf82f5b35ee Author: ops-mx805******qq.comDate: Tue Jun2016:35:0420230800addweek2 homework commit 11d4aecdd6b3f2814f951cad65369d747b9de576 Author: ops-mx805******qq.comDate: Tue Jun2015:48:2120230800addduplicate count codeformx commit 5f2ff87b366c18e405af276204f54496ab823611 Author: 马某人805******qq.comDate: Tue Jun2006:01:1020230000 Initial commit[rootk8s-master test]# git branch // test仓库下的分支信息* master2.创建分支并选中创建分支[rootk8s-master week4]# git checkout -b testM gohomework/week4/main.go Switched to a new branchtest[rootk8s-master week4]# git branch // * 号代表已切换至test分支master *testxxx在本地创建分支并追踪远程仓库分支gitcheckout-bterraform-dev-0.0.1 origin/terraform-dev-0.0.13.修改内容并提交修改[rootk8s-master week4]# vim main.go[rootk8s-master week4]# pwd/data/git/test/gohomework/week4先添加至暂存区[rootk8s-master week4]# git add . // add 添加修改至暂存区[rootk8s-master week4]# git status // status查看当前状态# On branch test# Changes to be committed:# (use git reset HEAD file... to unstage)## modified: main.go#提交变更到本地仓库[rootk8s-master week4]# git commit -m add some content[test 9d4634b]addsome content1filechanged,1insertion()查看本地仓库提交信息[rootk8s-master week4]# git logcommit 9d4634b29af6584d8480eb426f76327106a32b51 Author: ops-mx805xxxxxxqq.comDate: Wed Jun2816:58:2320230800addsome content推送至远程仓库[rootk8s-master week4]# git remote show // 查看远程仓库信息origintest[rootk8s-master week4]# git push test test // 第一个test是仓库命名 第二个test是分支名称Counting objects:9, done. Delta compression using up to16threads. Compressing objects:100%(4/4), done. Writing objects:100%(5/5),424bytes|0bytes/s, done. Total5(delta2), reused0(delta0)remote: Powered by GITEE.COM[GNK-6.4]remote: Create a pull requestforteston Gitee by visiting: remote: https://gitee.com/ops-mx/test/pull/new/ops-mx:test...ops-mx:master To gitgitee.com:ops-mx/test.git *[new branch]test-test远程仓库查看推送情况推送前无test分支推送后有test分支4.分支合并master上的代码[rootk8s-master week4]# git checkout masterAlready onmaster[rootk8s-master week4]# git branch* mastertestxxx[rootk8s-master week4]# cat main.go // master分支 main.go里面没有添加的内容funcmain(){fmt.Println(~~~~~~这是第1题答案~~~~~~)topic1()fmt.Println(~~~~~~这是第2题答案~~~~~~)topic2()fmt.Println(~~~~~~这是第3题答案~~~~~~)topic3()}test分支的代码注意这里我只是切换了分支然后代码就会不一样[rootk8s-master test]# git checkout testSwitched to branchtest[rootk8s-master week4]# cat main.go // test分支有添加的内容funcmain(){fmt.Println(~~~~~~这是第四周作业~~~~~~)// 这是新添加的内容 fmt.Println(~~~~~~这是第1题答案~~~~~~)topic1()fmt.Println(~~~~~~这是第2题答案~~~~~~)topic2()fmt.Println(~~~~~~这是第3题答案~~~~~~)topic3()}将test分支合并到master[rootk8s-master week4]# git checkout master // 将test分支合并到master先选定master分支再合并testSwitched to branchmaster[rootk8s-master week4]# git merge test // 合并test分支Updating 11d4aec..9d4634b Fast-forward gohomework/week2/main.go|62 gohomework/week4/main.go|12files changed,63insertions()create mode100644gohomework/week2/main.go[rootk8s-master week4]# cat main.gofuncmain(){fmt.Println(~~~~~~这是第四周作业~~~~~~)fmt.Println(~~~~~~这是第1题答案~~~~~~)topic1()fmt.Println(~~~~~~这是第2题答案~~~~~~)topic2()fmt.Println(~~~~~~这是第3题答案~~~~~~)topic3()}[rootk8s-master week4]# git branch // 能看到 这里是master分支修改的内容已经有了但目前还只是在本地仓库需要将合并后的master上传到远程仓库* mastertestxxx上传到远程仓库[rootk8s-master week4]# git push test masterTotal0(delta0), reused0(delta0)remote: Powered by GITEE.COM[GNK-6.4]To gitgitee.com:ops-mx/test.git 11d4aec..9d4634b master -master图中可以看到远程仓库的master分支已经有修改过的内容了5.撤销addgit add 完之后发现文件目录放错了[rootk8s-master gohomework]# git add .[rootk8s-master gohomework]# git status# On branch master# Changes to be committed:# (use git reset HEAD file... to unstage)## new file: main.go# new file: week4/test#[rootk8s-master gohomework]# ls // main.go文件应该放在week7目录下main.go week2 week4 week7[rootk8s-master gohomework]# git branch* mastertestxxx用git reset撤回 git add[rootk8s-master gohomework]# git reset HEAD main.go[rootk8s-master gohomework]# git status# On branch master# Changes to be committed:# (use git reset HEAD file... to unstage)## new file: week4/test## Untracked files:# (use git add file... to include in what will be committed)## main.go[rootk8s-master gohomework]# git reset HEAD week4/test[rootk8s-master gohomework]# git status# On branch master# Untracked files:# (use git add file... to include in what will be committed)## main.go# week4/testnothing added to commit but untracked files present(usegit addto track)移动文件[rootk8s-master gohomework]# mv main.go week7/[rootk8s-master gohomework]# ls week7/main.go重新add并提交推送至远程仓库[rootk8s-master gohomework]# git add week7/main.go[rootk8s-master gohomework]# git status# On branch master# Changes to be committed:# (use git reset HEAD file... to unstage)## new file: week7/main.go## Untracked files:# (use git add file... to include in what will be committed)## week4/test[rootk8s-master gohomework]# git commit -m commit homework for seventh week[master c05a218]commit homeworkforseventh week1filechanged,46insertions()create mode100644gohomework/week7/main.go[rootk8s-master gohomework]# git remote showorigintest[rootk8s-master gohomework]# git push test masterCounting objects:7, done. Delta compression using up to16threads. Compressing objects:100%(4/4), done. Writing objects:100%(5/5),816bytes|0bytes/s, done. Total5(delta1), reused0(delta0)remote: Powered by GITEE.COM[GNK-6.4]To gitgitee.com:ops-mx/test.git 9d4634b..c05a218 master -master[rootk8s-master gohomework]# git logcommit c05a2187b6bf0b4bea6edb25285a3c1ba5473226 Author: ops-mx805xxxxxxqq.comDate: Wed Jul512:02:5320230800 commit homeworkforseventh week commit 9d4634b29af6584d8480eb426f76327106a32b51 Author: ops-mx805xxxxxxqq.comDate: Wed Jun2816:58:2320230800addsome content6.合并多个commit第一次提交了代码之后发现没有加注释于是加了注释又提交了第二次想把这两次的提交合并为一次提交[rootk8s-master week7]# git logcommit 7b7290a205d5e02c48bd9fcfe95607a43075284a Author: ops-mx805xxxxxxqq.comDate: Wed Jul512:08:2520230800addannotations // 第二次添加注释 commit c05a2187b6bf0b4bea6edb25285a3c1ba5473226 Author: ops-mx805xxxxxxqq.comDate: Wed Jul512:02:5320230800 commit homeworkforseventh week // 第一次提交代码使用git rebase命令合并[rootk8s-master week7]# git rebase -i HEAD~2 // HEAD~2是选择前两个版本pick c05a218 commit homeworkforseventh week // 第一次提交代码 pick 7b7290aaddannotations // 第二次提交添加注释# Rebase 9d4634b..7b7290a onto 9d4634b## Commands:# p, pick use commit# r, reword use commit, but edit the commit message# e, edit use commit, but stop for amending# s, squash use commit, but meld into previous commit# f, fixup like squash, but discard this commits log message# x, exec run command (the rest of the line) using shell## 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.## Note that empty commits are commented out将第二次提交的pick修改为ss是squash的意思 退出编辑此时会执行合并并再次弹出vim编辑界面# This is a combination of 2 commits.# The first commits message is:commit homeworkforseventh week# This is the 2nd commit message:addannotations# Please enter the commit message for your changes. Lines starting# with # will be ignored, and an empty message aborts the commit.# HEAD detached at c05a218# You are currently editing a commit while rebasing branch master on 9d4634b.## Changes to be committed:# (use git reset HEAD^1 file... to unstage)## new file: gohomework/week7/main.go## Untracked files:# (use git add file... to include in what will be committed)## gohomework/week4/test进入编辑模式删除2nd commit那部分内容这时就会保留一个commit并生成一个新的commit ID保存退出# This is a combination of 2 commits.# The first commits message is:commit homeworkforseventh week // 对比上面的内容 发现commit变为一个如果想修改message的话直接编辑这行内容即可# Please enter the commit message for your changes. Lines starting# with # will be ignored, and an empty message aborts the commit.# HEAD detached at c05a218# You are currently editing a commit while rebasing branch master on 9d4634b.## Changes to be committed:# (use git reset HEAD^1 file... to unstage)## new file: gohomework/week7/main.go保存退出后返回rebase成功[rootk8s-master week7]# git rebase -i HEAD~2[detached HEAD 407a835]commit homeworkforseventh week1filechanged,51insertions()create mode100644gohomework/week7/main.go Successfully rebased and updated refs/heads/master. // 返回成功git log 发现第二次提交的内容没有了[rootk8s-master week7]# git log //没合并之前是有一段 add annotations的内容的这里没有了commit 407a835a4b1ee58783d4fce964e0ac270ef65ce1 Author: ops-mx805xxxxxxqq.comDate: Wed Jul512:02:5320230800 commit homeworkforseventh week commit 9d4634b29af6584d8480eb426f76327106a32b51 Author: ops-mx805xxxxxxqq.comDate: Wed Jun2816:58:2320230800addsome content最后一步就是将本地仓库推送至远程仓库用push -f 强制执行这步有风险有可能会丢失其他人写的代码一定要确认好[rootk8s-master week7]# git push test master // 直接push是有问题的To gitgitee.com:ops-mx/test.git![rejected]master -master(non-fast-forward)error: failed to push some refs togitgitee.com:ops-mx/test.githint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes(e.g.git pull)hint: before pushing again. hint: See theNote about fast-forwardsingit push --helpfordetails.[rootk8s-master week7]# git push test master -f // 用push -f 强制执行Counting objects:7, done. Delta compression using up to16threads. Compressing objects:100%(4/4), done. Writing objects:100%(5/5),939bytes|0bytes/s, done. Total5(delta1), reused0(delta0)remote: Powered by GITEE.COM[GNK-6.4]To gitgitee.com:ops-mx/test.git 7b7290a...407a835 master -master(forced update)跨版本合并假如我前两次提交完后有同事又提交了一次代码ID是ffc2903这个那我想合并 c05a218 和 7b7290a 应该怎么办[rootk8s-master test]# git rebase -i HEAD~3 // 先变基到第三个版本pick c05a218 commit homeworkforseventh week pick 7b7290aaddannotations pick ffc2903 week7 homework# Rebase 9d4634b..407a835 onto 9d4634b## Commands:# p, pick use commit# r, reword use commit, but edit the commit message# e, edit use commit, but stop for amending# s, squash use commit, but meld into previous commit# f, fixup like squash, but discard this commits log message# x, exec run command (the rest of the line) using shell## 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.## Note that empty commits are commented out修改第二次提交pick 改为s保存退出此时会执行合并并再次弹出vim编辑界面pick c05a218 commit homeworkforseventh week s 7b7290aaddannotations // 将第二次提交pick 改为 s pick ffc2903 week7 homework# Rebase 9d4634b..407a835 onto 9d4634b## Commands:# p, pick use commit# r, reword use commit, but edit the commit message# e, edit use commit, but stop for amending# s, squash use commit, but meld into previous commit# f, fixup like squash, but discard this commits log message# x, exec run command (the rest of the line) using shell## 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.## Note that empty commits are commented out修改message剩下的操作跟上边的合并一样修改完后保存退出# This is a combination of 2 commits.# The first commits message is:commit homeworkforseventh week# Please enter the commit message for your changes. Lines starting# with # will be ignored, and an empty message aborts the commit.# HEAD detached at c05a218# You are currently editing a commit while rebasing branch master on 9d4634b.## Changes to be committed:# (use git reset HEAD^1 file... to unstage)## new file: gohomework/week7/main.go查看本地仓库提交日志[rootk8s-master golang12]# git logcommit 2a8cc7ee3d9a6ff591b9cbef0ccc99bbf27cd4a8 // 同事提交的代码还在 Author: zhangg-stars191xxxxxxxqq.comDate: Wed Jul515:44:3320230800 week7 homework commit cd07aff29d80d75ae4a22a5fc7e2ae376b6c9314 // 对比这个commit ID已经发生了变化第二次提交已经没有了现在是第一次和第三次 Author: ops-mx805xxxxxxqq.comDate: Wed Jul510:58:4520230800 commit homeworkforseventh week