一、git介绍
1.概念
Git 是分布式版本控制和源代码管理系统,重点使用和管理代码的速度。 Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发。Git 是根据GNU通用公共许可证版本2的条款分发的自由/免费软件。//更多请阅读:https://www.yiibai.com/git
2.术语
Workspace:工作区
Index/Stage:暂存区,也叫索引
Repository:仓库区(或本地仓库),也存储库
Remote:远程仓库
3.关键词
工作区: 通过git init创建的代码库的所有文件但是不包括.git文件(版本库)
暂存区: 通过git add ./*/Xxx/Xxxx 添加的修改,都是进入到暂存区了,肉眼不可见 通过 git status 可以看到修改的状态。
**版本库:**工作区中的.git的文件,这个属于版本库,提交的版本都到了这个文件里面,不要随便修改这个文件夹的东西
二、Git使用测试
1.clone远程仓库到本地
git clone命令,将远程仓库克隆到本地
[root@k8s-master git]# git clone https://gitee.com/ops-mx/test.git
Cloning into 'test'...
Username for 'https://gitee.com': 805xxxxxx@qq.com // 这里输入的账号密码是gitee的账密
Password for 'https://805xxxxxx@qq.com@gitee.com':
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), done.
查看克隆信息,会发现在本地生成了一个仓库名称的目录
[root@k8s-master git]# ls
test
添加远程仓库信息,这一步是为了push做准备
[root@k8s-master test]# git remote add test git@gitee.com:ops-mx/test.git
[root@k8s-master test]# git remote show
origin
test
查看git配置信息,注意:当前在test仓库目录下,git config拿的信息其实是.git目录下的config内容
[root@k8s-master test]# git config --list
user.name=ops-mx
user.email=805xxxxxx@qq.com
push.default=simple
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://gitee.com/ops-mx/test.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
remote.test.url=git@gitee.com:ops-mx/test.git
remote.test.fetch=+refs/heads/*:refs/remotes/test/*
branch.xxx.remote=test
branch.xxx.merge=refs/heads/xxx
[root@k8s-master test]# pwd
/data/git/test
[root@k8s-master test]# ls -a
. .. .git .gitee .gitignore gohomework LICENSE README.en.md README.md
[root@k8s-master test]# cd .git
[root@k8s-master .git]# ls
branches COMMIT_EDITMSG config description HEAD hooks index info logs objects ORIG_HEAD packed-refs refs
[root@k8s-master .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://gitee.com/ops-mx/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "test"]
url = git@gitee.com:ops-mx/test.git
fetch = +refs/heads/*:refs/remotes/test/*
[branch "xxx"]
remote = test
merge = refs/heads/xxx
如果有多个项目仓库时,只需要切换目录就能查看各项目的信息,因为git拿的是项目目录下的.git信息
[root@k8s-master git]# pwd
/data/git
[root@k8s-master git]# ls // 我这里有两个项目仓库,一个是test,一个是golang12
golang12 test
[root@k8s-master git]# cd golang12/
[root@k8s-master golang12]# ls
course_doc go.mod go.work homework README.md
[root@k8s-master golang12]# git log // golang12仓库下的信息
commit a5b7cd8818d42d812d24e2bd1e03ab44e3fc0022
Author: db <db@MacBook-Pro.local>
Date: Wed Jun 28 10:55:32 2023 +0800
add homework week7
commit cd66a7819cd3eef4ea2464bbf42d2e1b8b7d668c
Merge: e3f744d b81e51d
Author: xiaowei <1244******@qq.com>
Date: Tue Jun 27 17:32:34 2023 +0800
Merge branch 'master' of https://gitee.com/magedu/golang12
commit e3f744d615950f719372f42655571bd38985f2d5
Author: xiaowei <1244******@qq.com>
Date: Tue Jun 27 17:31:46 2023 +0800
Update main.go
[root@k8s-master golang12]# cd ../test/ // 切换目录,就能查看test仓库的信息了
[root@k8s-master test]# git log
commit ffc29038a73c2f03f932d86c6ac35cf82f5b35ee
Author: ops-mx <805******@qq.com>
Date: Tue Jun 20 16:35:04 2023 +0800
add week2 homework
commit 11d4aecdd6b3f2814f951cad65369d747b9de576
Author: ops-mx <805******@qq.com>
Date: Tue Jun 20 15:48:21 2023 +0800
add duplicate count code for mx
commit 5f2ff87b366c18e405af276204f54496ab823611
Author: 马某人 <805******@qq.com>
Date: Tue Jun 20 06:01:10 2023 +0000
Initial commit
[root@k8s-master test]# git branch // test仓库下的分支信息
* master
2.创建分支并选中
创建分支
[root@k8s-master week4]# git checkout -b test
M gohomework/week4/main.go
Switched to a new branch 'test'
[root@k8s-master week4]# git branch // * 号代表已切换至test分支
master
* test
xxx
3.修改内容并提交
修改
[root@k8s-master week4]# vim main.go
[root@k8s-master week4]# pwd
/data/git/test/gohomework/week4
先添加至暂存区
[root@k8s-master week4]# git add . // add 添加修改至暂存区
[root@k8s-master week4]# git status // status查看当前状态
# On branch test
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: main.go
#
提交变更到本地仓库
[root@k8s-master week4]# git commit -m "add some content"
[test 9d4634b] add some content
1 file changed, 1 insertion(+)
查看本地仓库提交信息
[root@k8s-master week4]# git log
commit 9d4634b29af6584d8480eb426f76327106a32b51
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jun 28 16:58:23 2023 +0800
add some content
推送至远程仓库
[root@k8s-master week4]# git remote show // 查看远程仓库信息
origin
test
[root@k8s-master week4]# git push test test // 第一个test是仓库命名 第二个test是分支名称
Counting objects: 9, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 424 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'test' on Gitee by visiting:
remote: https://gitee.com/ops-mx/test/pull/new/ops-mx:test...ops-mx:master
To git@gitee.com:ops-mx/test.git
* [new branch] test -> test
远程仓库查看推送情况
推送前,无test分支
推送后,有test分支
4.分支合并
master上的代码
[root@k8s-master week4]# git checkout master
Already on 'master'
[root@k8s-master week4]# git branch
* master
test
xxx
[root@k8s-master week4]# cat main.go // master分支 main.go里面没有添加的内容
func main() {
fmt.Println("~~~~~~这是第1题答案~~~~~~")
topic1()
fmt.Println("~~~~~~这是第2题答案~~~~~~")
topic2()
fmt.Println("~~~~~~这是第3题答案~~~~~~")
topic3()
}
test分支的代码,注意:这里我只是切换了分支,然后代码就会不一样
[root@k8s-master test]# git checkout test
Switched to branch 'test'
[root@k8s-master week4]# cat main.go // test分支有添加的内容
func main() {
fmt.Println("~~~~~~这是第四周作业~~~~~~") // 这是新添加的内容
fmt.Println("~~~~~~这是第1题答案~~~~~~")
topic1()
fmt.Println("~~~~~~这是第2题答案~~~~~~")
topic2()
fmt.Println("~~~~~~这是第3题答案~~~~~~")
topic3()
}
将test分支合并到master,
[root@k8s-master week4]# git checkout master // 将test分支合并到master,先选定master分支,再合并test
Switched to branch 'master'
[root@k8s-master week4]# git merge test // 合并test分支
Updating 11d4aec..9d4634b
Fast-forward
gohomework/week2/main.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
gohomework/week4/main.go | 1 +
2 files changed, 63 insertions(+)
create mode 100644 gohomework/week2/main.go
[root@k8s-master week4]# cat main.go
func main() {
fmt.Println("~~~~~~这是第四周作业~~~~~~")
fmt.Println("~~~~~~这是第1题答案~~~~~~")
topic1()
fmt.Println("~~~~~~这是第2题答案~~~~~~")
topic2()
fmt.Println("~~~~~~这是第3题答案~~~~~~")
topic3()
}
[root@k8s-master week4]# git branch // 能看到 这里是master分支,修改的内容已经有了,但目前还只是在本地仓库,需要将合并后的master上传到远程仓库
* master
test
xxx
上传到远程仓库
[root@k8s-master week4]# git push test master
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:ops-mx/test.git
11d4aec..9d4634b master -> master
图中可以看到,远程仓库的master分支已经有修改过的内容了
5.撤销add
git add 完之后发现文件目录放错了
[root@k8s-master gohomework]# git add .
[root@k8s-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
#
[root@k8s-master gohomework]# ls // main.go文件应该放在week7目录下
main.go week2 week4 week7
[root@k8s-master gohomework]# git branch
* master
test
xxx
用git reset撤回 git add
[root@k8s-master gohomework]# git reset HEAD main.go
[root@k8s-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
[root@k8s-master gohomework]# git reset HEAD week4/test
[root@k8s-master gohomework]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# main.go
# week4/test
nothing added to commit but untracked files present (use "git add" to track)
移动文件
[root@k8s-master gohomework]# mv main.go week7/
[root@k8s-master gohomework]# ls week7/
main.go
重新add,并提交推送至远程仓库
[root@k8s-master gohomework]# git add week7/main.go
[root@k8s-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
[root@k8s-master gohomework]# git commit -m "commit homework for seventh week"
[master c05a218] commit homework for seventh week
1 file changed, 46 insertions(+)
create mode 100644 gohomework/week7/main.go
[root@k8s-master gohomework]# git remote show
origin
test
[root@k8s-master gohomework]# git push test master
Counting objects: 7, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 816 bytes | 0 bytes/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:ops-mx/test.git
9d4634b..c05a218 master -> master
[root@k8s-master gohomework]# git log
commit c05a2187b6bf0b4bea6edb25285a3c1ba5473226
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jul 5 12:02:53 2023 +0800
commit homework for seventh week
commit 9d4634b29af6584d8480eb426f76327106a32b51
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jun 28 16:58:23 2023 +0800
add some content
6.合并多个commit
第一次提交了代码之后发现没有加注释,于是加了注释又提交了第二次,想把这两次的提交合并为一次提交
[root@k8s-master week7]# git log
commit 7b7290a205d5e02c48bd9fcfe95607a43075284a
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jul 5 12:08:25 2023 +0800
add annotations // 第二次添加注释
commit c05a2187b6bf0b4bea6edb25285a3c1ba5473226
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jul 5 12:02:53 2023 +0800
commit homework for seventh week // 第一次提交代码
使用git rebase命令合并
[root@k8s-master week7]# git rebase -i HEAD~2 // HEAD~2是选择前两个版本
pick c05a218 commit homework for seventh week // 第一次提交代码
pick 7b7290a add annotations // 第二次提交添加注释
# 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 commit's 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,s是squash的意思 ,退出编辑,此时会执行合并,并再次弹出vim编辑界面
# This is a combination of 2 commits.
# The first commit's message is:
commit homework for seventh week
# This is the 2nd commit message:
add annotations
# 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 commit's message is:
commit homework for seventh 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成功
[root@k8s-master week7]# git rebase -i HEAD~2
[detached HEAD 407a835] commit homework for seventh week
1 file changed, 51 insertions(+)
create mode 100644 gohomework/week7/main.go
Successfully rebased and updated refs/heads/master. // 返回成功
git log 发现第二次提交的内容没有了
[root@k8s-master week7]# git log //没合并之前是有一段 add annotations的内容的,这里没有了
commit 407a835a4b1ee58783d4fce964e0ac270ef65ce1
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jul 5 12:02:53 2023 +0800
commit homework for seventh week
commit 9d4634b29af6584d8480eb426f76327106a32b51
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jun 28 16:58:23 2023 +0800
add some content
最后一步就是将本地仓库推送至远程仓库,用push -f 强制执行,这步有风险,有可能会丢失其他人写的代码,一定要确认好
[root@k8s-master week7]# git push test master // 直接push是有问题的
To git@gitee.com:ops-mx/test.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitee.com:ops-mx/test.git'
hint: 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 the 'Note about fast-forwards' in 'git push --help' for details.
[root@k8s-master week7]# git push test master -f // 用push -f 强制执行
Counting objects: 7, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 939 bytes | 0 bytes/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:ops-mx/test.git
+ 7b7290a...407a835 master -> master (forced update)
跨版本合并
假如我前两次提交完后,有同事又提交了一次代码,ID是ffc2903这个,那我想合并 c05a218 和 7b7290a 应该怎么办?
[root@k8s-master test]# git rebase -i HEAD~3 // 先变基到第三个版本
pick c05a218 commit homework for seventh week
pick 7b7290a add annotations
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 commit's 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 homework for seventh week
s 7b7290a add annotations // 将第二次提交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 commit's 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 commit's message is:
commit homework for seventh 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
查看本地仓库提交日志
[root@k8s-master golang12]# git log
commit 2a8cc7ee3d9a6ff591b9cbef0ccc99bbf27cd4a8 // 同事提交的代码还在
Author: zhangg-stars <191xxxxxxx@qq.com>
Date: Wed Jul 5 15:44:33 2023 +0800
week7 homework
commit cd07aff29d80d75ae4a22a5fc7e2ae376b6c9314 // 对比这个commit ID,已经发生了变化,第二次提交已经没有了,现在是第一次和第三次
Author: ops-mx <805xxxxxx@qq.com>
Date: Wed Jul 5 10:58:45 2023 +0800
commit homework for seventh week