Git 多人协作(1)
一.Git标签管理
在git中,我们可以对某次commit进行标识,这就类似取了一个别名
例如在项目的最后一个版本的commit起名叫v1.0
除此之外,tag也可以相对于较难记住的commitID,更快的回退
1.标签管理实操
标签默认是对最新的提交进行。
git tag name
我们可以看.git目录中,refs目录中出现了我们刚才创建的tag v1.0
wujiahao@VM-12-14-ubuntu:~/gitcode$ git tag v1.0
wujiahao@VM-12-14-ubuntu:~/gitcode$ git tag
v1.0
wujiahao@VM-12-14-ubuntu:~/gitcode$ tree .git
.git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│?? ├── applypatch-msg.sample
│?? ├── commit-msg.sample
│?? ├── fsmonitor-watchman.sample
│?? ├── post-update.sample
│?? ├── pre-applypatch.sample
│?? ├── pre-commit.sample
│?? ├── pre-merge-commit.sample
│?? ├── prepare-commit-msg.sample
│?? ├── pre-push.sample
│?? ├── pre-rebase.sample
│?? ├── pre-receive.sample
│?? ├── push-to-checkout.sample
│?? └── update.sample
├── index
├── info
│?? └── exclude
├── logs
│?? ├── HEAD
│?? └── refs
│?? └── heads
│?? └── master
├── objects
│?? ├── 04
│?? │?? └── 0b11c786d345995146e3d0198c474c1f8952bc
│?? ├── 08
│?? │?? └── 0d576c1e3c2ad8acf17244c87739cb72b6ffff
│?? ├── 0b
│?? │?? └── 90920fbcc60186429de1a9a7b0c647b7f11bb1
│?? ├── 0d
│?? │?? └── d9cb32de5463be039fa0f75da38aa2d05e6634
│?? ├── 18
│?? │?? ├── 812885f304b7ba2baa67a109a2762ebb6f85c3
│?? │?? └── 9474800700657c5683bb58cdf6fea2107e74c6
│?? ├── 19
│?? │?? └── 275f78861842cc96073125596f824ad2c3db49
│?? ├── 1b
│?? │?? └── b0b34c302f771dcfa9802add7e50b441259c4e
│?? ├── 1d
│?? │?? └── f0d760d3221b517addd824c6a56db52e0b038f
│?? ├── 1e
│?? │?? └── 0f9aca0d540f6d3fe7785a582e1eeaf95643e2
│?? ├── 28
│?? │?? └── 01fc14dda879c6fd540b2c387cc868e72406a8
│?? ├── 38
│?? │?? └── 3c28173e7c9712639e1244efd19135e2ba4471
│?? ├── 3d
│?? │?? └── 2f4f4b5007b40be8e15a9cc89b546ff4bf0e6d
│?? ├── 44
│?? │?? ├── eee88f3839781bdc49019caa8f72a015a6ee8d
│?? │?? └── f6622c467604e776b367f75b31304538fcd2ae
│?? ├── 4f
│?? │?? └── ce86a4f91ad7e5246688230bcbe6e89736fb15
│?? ├── 51
│?? │?? └── 584fed14c8938ffe18b2df8c4a9a0bcc11e84d
│?? ├── 52
│?? │?? └── e957c3c26040457443bd3c6156d5d0162adbf7
│?? ├── 5d
│?? │?? └── 6ba7fca1d961ad5b6f259ae8e121a2faeba0e5
│?? ├── 67
│?? │?? └── 0bc4f0675b521a88779dc45cc4fdea0cbd810f
│?? ├── 77
│?? │?? └── c800fee24486621533d69d7bfc787b61cdc481
│?? ├── 79
│?? │?? ├── 453aa701d541b13bc0c2b845b47a13e47402a4
│?? │?? └── b9f3a9374bd9a7b3a6b8c9eebf9b7c0ae26453
│?? ├── 7c
│?? │?? └── 30ecf41d889624d3242c1920eb025751c19455
│?? ├── 7e
│?? │?? └── 72350d0cf30e83aaef277fb5b1bfb3aae742ce
│?? ├── 80
│?? │?? └── 26ce2e2a2c6c929e31faa52375d5e488644a44
│?? ├── 82
│?? │?? └── dfd358e4ae0d7006bdb0af4438c45feabe2b79
│?? ├── 86
│?? │?? └── 450d7fe2b82254f694805388bf926b799dfa24
│?? ├── 89
│?? │?? └── d2d3e2fcc1d50d87aa0ed4b3cc25f2eeeabe74
│?? ├── 97
│?? │?? └── bd85ac99dc567d9226fa812d8ae1ccedab9eb4
│?? ├── 9c
│?? │?? └── ec1466e4c1ee9185a531516ae3181c0b28721b
│?? ├── a1
│?? │?? └── 8479ab13c25d12e250ea02dad4612d3f4c1850
│?? ├── a6
│?? │?? └── bb306b5e81e26e52e1cd8f6048400b7b08b74c
│?? ├── b0
│?? │?? └── 7776b6954f533a53bf006dda2e518c5a69c02e
│?? ├── b4
│?? │?? └── 7abca718313436618c19ad9f570865109aa416
│?? ├── bd
│?? │?? └── bf68105f7474d18e9fac942c532cb4e5c2e111
│?? ├── c3
│?? │?? └── 0287c46608bf6fedf4e299b8f3638bc19cb0c9
│?? ├── c6
│?? │?? └── 687eedc8d5643592347ce48c2139d4dc186168
│?? ├── d6
│?? │?? ├── 511164c38e0c209df017210968e3b8fcae4082
│?? │?? └── a7559f3b38c1b98b8792d5dfc40e51b7e84d98
│?? ├── dc
│?? │?? └── f05d4b8470c862f190ab7c255207b7c69081dd
│?? ├── de
│?? │?? └── f788081cb845aac209a47068fdb80116e75eaa
│?? ├── e1
│?? │?? ├── 3801e52a5bc62ddc905cd51c32155b6253a272
│?? │?? └── 43915eefd8ef9d0d05272d61a95e71cc9ed8cb
│?? ├── e6
│?? │?? └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│?? ├── ee
│?? │?? └── c77b0afdbbf5841ba813ffe48e63f7f9354bd8
│?? ├── f2
│?? │?? └── c78b5dc22a0d610f6d407d7f6f27c41c6b1de2
│?? ├── f5
│?? │?? └── aa7498c931ae9bc6cf0edfa400f7ceb28816a9
│?? ├── fa
│?? │?? └── 687dd0cc5ffdfc9cb3887c19cdeb315c550f4d
│?? ├── info
│?? └── pack
├── ORIG_HEAD
├── packed-refs
└── refs├── heads│?? └── master└── tags└── v1.056 directories, 74 files
可以简单验证
wujiahao@VM-12-14-ubuntu:~/gitcode$ cat .git/refs/tags/v1.0
dcf05d4b8470c862f190ab7c255207b7c69081dd
wujiahao@VM-12-14-ubuntu:~/gitcode$ git log
commit dcf05d4b8470c862f190ab7c255207b7c69081dd (HEAD -> master, tag: v1.0)
我们可以指定特定的提交打标签,需要带上对应提交的commitID
wujiahao@VM-12-14-ubuntu:~/gitcode$ git log --pretty=oneline --abbrev-commit
dcf05d4 (HEAD -> master, tag: v1.0) merge master
e143915 modify ReadMe
52e957c merge fix_branch
080d576 fix e
3d2f4f4 merge with no-ff
77c800f MD abcdReadMe
79453aa fix merge ReadMe
b47abca MD masterReadMe
e13801e MD dev1ReadMe
c30287c modify ReadMe
82dfd35 delete file5
c6687ee add doc4 and doc5
1881288 add some files
f5aa749 add first word
wujiahao@VM-12-14-ubuntu:~/gitcode$ git tag v0.9 e143915
并且,带上选项-m后可以对指定的标签添加具体描述
git show tagname 查看tag名对应的提交信息
wujiahao@VM-12-14-ubuntu:~/gitcode$ git tag -a v0.8 -m "important tag"
wujiahao@VM-12-14-ubuntu:~/gitcode$ git tag
v0.8
v0.9
v1.0
wujiahao@VM-12-14-ubuntu:~/gitcode$ git show v0.8
tag v0.8
Tagger: wjh <2339023797@qq.com>
Date: Thu Sep 18 19:30:55 2025 +0800important tagcommit dcf05d4b8470c862f190ab7c255207b7c69081dd (HEAD -> master, tag: v1.0, tag: v0.8)
Merge: e143915 52e957c
Author: wjh <2339023797@qq.com>
Date: Mon Sep 15 22:33:16 2025 +0800merge masterdiff --cc ReadMe
index fa687dd,51584fe..a6bb306
--- a/ReadMe
+++ b/ReadMe
@@@ -1,6 -1,5 +1,7 @@@hello Linuxhello gitinsert into master
- a,b,c,d
+ a,b,c,d,e+i am coding....+i am done.
+
删除标签
git tag -d v1.0
wujiahao@VM-12-14-ubuntu:~/gitcode$ git tag -d v1.0
Deleted tag 'v1.0' (was dcf05d4)
wujiahao@VM-12-14-ubuntu:~/gitcode$ git tag
v0.8
v0.9
以上的操作都是对本地仓库标签的操作,当然我们也可以将标签的修改推送到远程仓库。
查看我们当前的远端仓库,是没有标签的。
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push origin v0.8
Username for 'https://gitee.com': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.com':
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 163 bytes | 163.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 144e68ae
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.git* [new tag] v0.8 -> v0.8
此时我们刷新远端仓库,已经同步
如果有很多标签想一次性推送:
git push origin --tags
想删除远端的tag:
git push origin :v0.8
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push origin v0.8
Username for 'https://gitee.com': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.com':
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 163 bytes | 163.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 144e68ae
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.git* [new tag] v0.8 -> v0.8
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push origin :v0.8
Username for 'https://gitee.com': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.com':
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag c145b9b2
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.git- [deleted] v0.8
二.Git多人协作实操
我们已经学习了大部分的Git操作,我们可以试着模拟一下多人协作开发的场景。
1.实操模拟
我们需要实现以下目标。为此,我们需要多用户对文件进行协作,这里的用户1为Linux用户,用户2为Windows用户。
由于我们不方便直接在master分支上操作,所以我们的操作是:在远程仓库创建一个新分支dev,两个用户将文件提交至dev,最后处理完冲突后再合并到master分支。
当前的状态
在执行pull操作前用户1的本地仓库状态
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch
* master
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch -rorigin/HEAD -> origin/masterorigin/master
因为远端此时创建了分支dev,我们拉取远端
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git pull
From https://gitee.com/wjhwujiahao/linux-fundamentals-learning* [new branch] dev -> origin/dev
Already up to date.wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch -a
* masterremotes/origin/HEAD -> origin/masterremotes/origin/devremotes/origin/master
在用户1的本地创建dev分支并追踪远端的dev分支
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git checkout -b dev origin/dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Switched to a new branch 'dev'
此时可以查看到已追踪成功
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch -vv
* dev c68d4e6 [origin/dev] add ignoremaster c68d4e6 [origin/master] add ignore
用户1向test.txt写入aaa,并add,commit,push。
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push
Username for 'https://gitee.com': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 291 bytes | 291.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 9f49ae86
To https://gitee.com/wjhwujiahao/linux-fundamentals-learning.gitc68d4e6..1d9f627 dev -> dev
此时可以查看到,远端的dev分支已经有了我们的修改
此时用户1的准备工作已经做好,我们开始做用户2的准备工作
首先创建一个文件夹,在该文件夹下克隆仓库。
此时的状态
在当前目录下右键,选择用powershell打开。
可以查看当前用户2的仓库分支状态
创建用户2的本地dev分支并追踪
PS D:\git\linux-fundamentals-learning> git checkout -b dev origin/dev
branch 'dev' set up to track 'origin/dev'.
Switched to a new branch 'dev'
此时我们执行push发现是有问题的。
这是因为发生了冲突,我们需要手动消除并push。
最后一步,合并远端master和dev
在gitee中,我们可以看到提交的各种信息。