【Git】远程仓库

本博客的环境是 Ubuntu/Linux

文章目录

集中式与分布式的区别

引荐自关于Git这一篇就够了

Git是分布式版本控制系统,还有 svn,cvs 的集中式版本控制系统

区别在于,集中式版本控制,在每次写代码时都需要从服务器拉取一份下来,并且如果服务器丢失,那所有的数据就丢失了,本地只保存当前的版本信息。集中式将代码都放在一个服务器上集中管理,本地的所有回滚等操作都需要服务器支持

分布式版本控制,通常也有一台充当 "中央服务器" 的电脑,但当你从主仓库拉取一份代码后,你的电脑也成为了服务器,相当于每个人的电脑都是一台服务器,都有完整的版本库,可以自由的在本地进行回滚,修改,提交,完成开发后合并到主仓库即可。

集中式都有一个主版本号,所有的版本迭代都以这个版本号为主,而分布式因为每个客户端都是服务器,所以没有固定的版本号,而使用由哈希算法生成的一个 commit id,用于回滚操作。同时也有一个master主仓库,是一切分支仓库的主仓库,我们可以推送提交到master并合并到主仓库上,主仓库的版本号会迭代一次。我们客户端上的 git 版本号无论迭代多少次,都跟master无关,只有合并时,master才迭代一次

远程仓库

上述说到,Git 是分布式版本控制,有一个"中央服务器",我们熟知的 GitHubGitee 就充当了 "中央服务器"的作用。我们可以在上面创建仓库,这就是远程仓库,然后在本地机器上拉取一份代码下来,在本地进行开发,开发完成后提交给远程仓库,进行合并。

本篇博客,以Gitee为例

新建远程仓库

Gitee

点击"创建",如此就创建了一个远程仓库

克隆远程仓库

克隆/下载远程仓库到本地,需要使用git clone命令

sh 复制代码
git clone [远程仓库链接]

链接的获取在仓库页面右上角的"克隆/下载"

链接最常用的是 ------ HTTPSSSH

  • 使用 HTTPS 方式没有要求,直接使用命令即可
sh 复制代码
ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ git clone https://gitee.com/bao-bao-hai-mian/remote_git.git

Cloning into 'remote_git'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
Receiving objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ ll
total 12
drwxrwxr-x  3 ubuntu ubuntu 4096 Sep 17 16:09 ./
drwxrwxr-x 10 ubuntu ubuntu 4096 Sep 15 14:48 ../
drwxrwxr-x  3 ubuntu ubuntu 4096 Sep 17 16:09 remote_git/
ubuntu@VM-12-11-ubuntu:~/lesson/gitcode$ cd remote_git/
ubuntu@VM-12-11-ubuntu:~/lesson/gitcode/remote_git$ ll
total 20
drwxrwxr-x 3 ubuntu ubuntu 4096 Sep 17 16:09 ./
drwxrwxr-x 3 ubuntu ubuntu 4096 Sep 17 16:09 ../
drwxrwxr-x 8 ubuntu ubuntu 4096 Sep 17 16:09 .git/
-rw-rw-r-- 1 ubuntu ubuntu  954 Sep 17 16:09 README.en.md
-rw-rw-r-- 1 ubuntu ubuntu 1315 Sep 17 16:09 README.md
  • SSH 使用了公钥加密和公钥登录机制,使用该协议需要将本地的公钥设置到远程仓库中,由 Git 进行管理
  1. 首先要生成本地的公钥和私钥。在用户主目录下查看是否有.ssh目录,再看该目录下是否有id_rsa(私钥)id_rsa.pub(公钥)。如果没有,使用如下命令创建:
sh 复制代码
ssh-keygen -t rsa -C "邮箱"
#后续一路回车即可
  1. 将公钥配置到远程仓库中
sh 复制代码
cat id_rsa.pub
#再复制公钥

回到Gitee,点击头像 -> 设置 -> SSH公钥 -> 粘贴公钥

如此就可以使用 SSH 方式克隆远程仓库了

sh 复制代码
ubuntu@VM-12-11-ubuntu:~/lesson$ git clone git@gitee.com:bao-bao-hai-mian/remote_git.git

Cloning into 'remote_git'...
The authenticity of host 'gitee.com (180.76.198.77)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
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
Receiving objects: 100% (4/4), 2.10 KiB | 2.10 MiB/s, done.

当我们从远程仓库克隆后,实际上 Git 会自动把本地的 master 分支和远程的 master 分支连接起来,并且远程仓库的默认名称是 origin

在本地我们可以使用 git remote命令查看远程仓库信息

sh 复制代码
git remote #显示远程库信息
git remote -v #显示更详细信息

ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git remote
origin
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git remote -v
origin	git@gitee.com:bao-bao-hai-mian/remote_git.git (fetch)
origin	git@gitee.com:bao-bao-hai-mian/remote_git.git (push)

使用 git remote -v显示有fetch(拉取)push(推送)的地址,如果没有推送/拉取权限,就看不到相应的地址

向远程仓库推送

推送的命令如下:

sh 复制代码
git push [远程主机名] [本地分支名]:[远程分支名]
#例:
git push origin master:master

#如果本地分支与远程分支相同,则可以简短一点
git push [远程主机名] [分支名]

在推送前,要对仓库的user.nameuser.email进行配置,与远程仓库的用户名和邮箱相同

sh 复制代码
git config -l #以列表形式查看配置
git config user.name "用户名"
git config user.email "邮箱"

#还可以带上 --global选项,使该配置在本机器所有仓库生效
git config --global user.name "用户名"
git config --global user.email "邮箱"

接下来完成开发后,就可以正常add添加commit提交push推送,三板斧

示例:在本地创建file.txt并编辑内容,然后推送到远程仓库

sh 复制代码
#新建并编辑文件
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ vim file.txt
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ cat file.txt 
hello git
#添加到暂存区
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git add .
#提交到本地版本库
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git commit -m "add file:file.txt"
[master 4b2de81] add file:file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
 #推送到远程仓库
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git push origin master:master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 317 bytes | 317.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 7c4399be
To gitee.com:bao-bao-hai-mian/remote_git.git
   523bb3a..4b2de81  master -> master

如果是 SSH 协议,不需要每一次推送都输入密码,方便推送操作。但如果是 HTTPS 协议,每次推送时需要输入口令

从远程仓库拉取

如果远程仓库有修改,本地仓库并不会随着改变,本地仓库类似于对远程仓库的一次拷贝,后续若没有推送操作,双方并不会影响

为了保持本地仓库和远程仓库的一致性,基于最新的版本进行开发,可以进行拉取操作

sh 复制代码
git pull [远程主机名] [远程分支名]:[本地分支名]
#若远程与本地分支名相同,则可以简写
git pull [远程主机名] [分支名]

配置Git

忽略指定文件

在日常开发中,我们有些文件不想或不应该提交到远端,比如保存了数据库密码的配置文件,Git 使用.gitignore文件实现

在 Git 工作区的根目录下创建一个 .gitignore文件,然后把要忽略的文件名填进去,Git 就会自动忽略这些文件

Gitee在创建远程仓库的时候,就可以手动选择生成.gitignore文件

.gitignore文件使用如下:

  • 可以直接指定要忽略的文件名
  • *.so:表示忽略所有以.so结尾的文件
  • !文件名:表示不忽略该文件

上述文件表明,忽略所有以.so.conf结尾的文件,但不忽略b.so,指定忽略a.txt

sh 复制代码
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ touch .gitignore
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ vim .gitignore 
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ touch a.txt lib.so b.so
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git add .
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git commit -m "test .gitignore"
[master 19ee94c] test .gitignore
 2 files changed, 10 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 b.so

可以看到,最后也确实忽略了a.txtlib.so

不过 Git 也支持强制添加

sh 复制代码
git add -f [文件名] #带-f选项,表示强制添加

Git 还支持检查一个文件是否被忽略

sh 复制代码
git check-ignore -v [文件名]

ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git check-ignore -v lib.so
.gitignore:3:*.so	lib.so

Git 会告诉我们,.gitignore的第3行规则忽略了该文件

给命令配置别名

实际使用 Git 命令时,有些命令较长,Git 支持对命令进行简化

sh 复制代码
git config alias.[别名] [命令]

#带上 --global选项使得配置所有仓库生效
#例:
git config --global alias.st status
git config --global alias.last 'log -l'

标签管理

Git 标签(tags) 是一种用于标记特定提交的机制,通常用于标识重要的版本点,例如发布版本。Git 标签是固定的,不会随着提交的改变而改变,可以帮助我们在项目的历史中轻松找到重要的节点

相比于难以记住的commit idtag很好的解决了这个问题,因为tag一定要给一个让人容易记住,且有意义的名字。当我们需要回退到某个重要版本时,直接使用标签就能很快定位到

创建标签

sh 复制代码
#会给当前分支的最近一次提交打上标签
git tag [标签名] 

#查看所有标签
git tag

#给指定commit id 打上标签
git tag [标签名] [commit id]

#查看标签信息
git show [标签名]

#创建带说明的标签,-a指定标签名,-m指定描述
git tag -a [标签名] -m "描述" [commit id] 

示例:

sh 复制代码
#删除一些文件
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ rm a.txt b.so lib.so 
#添加&提交
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git add .
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git commit -m "rm files"
[master 7637b05] rm files
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 b.so
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git log
commit 7637b05a643918778b562c0effc1135c6d803ab8 (HEAD -> master)
Author: bao-bao-hai-mian <1076847758@qq.com>
Date:   Tue Sep 17 21:22:56 2024 +0800

    rm files
#给最近的一次提交打上标签
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git tag -a v1.0 -m "rm files"
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git tag
v1.0
#查看标签信息
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git show v1.0
tag v1.0
Tagger: bao-bao-hai-mian <1076847758@qq.com>
Date:   Tue Sep 17 21:24:12 2024 +0800

rm files

commit 7637b05a643918778b562c0effc1135c6d803ab8 (HEAD -> master, tag: v1.0)
Author: bao-bao-hai-mian <1076847758@qq.com>
Date:   Tue Sep 17 21:22:56 2024 +0800

    rm files

diff --git a/b.so b/b.so
deleted file mode 100644
index e69de29..0000000

操作标签

如果标签打错了,也可以删除

sh 复制代码
git tag -d [标签名]

因为创建的标签都只存储在本地,不会自动推送到远程,所以可以在本地安全删除。

如果要推送某个标签到远程,使用命令

sh 复制代码
git push origin [标签名]

ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git push origin v1.0
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 741 bytes | 741.00 KiB/s, done.
Total 7 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 7f4c2899
To gitee.com:bao-bao-hai-mian/remote_git.git
 * [new tag]         v1.0 -> v1.0

成功推送后,远程也可以看到标签

若本地有很多标签,也可以一次性推送

sh 复制代码
git push origin --tags 

如果误推送了错误的标签,需要

  1. 在本地删除标签
  2. 将删除推送给远程
sh 复制代码
git tag -d [标签]
git push origin :[本地标签]
#例:推送时要指明标签在.git文件中的路径
git tag -d v1.0
git push origin :refs/tags/v1.0

多人协作

Git 的分布式版本控制正是服务于多人协作

在实际开发中,每个用户都有自己的gitee/github账号,如果要多人进行协同开发,必须要将用户添加进开发者,用户才有权限进行代码提交

实际开发中,在任何情况下其实都不允许直接在 master分支上修改代码,这是为了保证主分支的稳定,所以在开发新功能时。常常会新建其他分支,供开发时迭代使用

Gitee上也可以很简单的创建新分支

接下来,通过两个用户,分别在LinuxWindows上针对同项目进行协作开发

首先就是克隆远程仓库

双端都使用如下命令

sh 复制代码
git clone [远程仓库链接]

如果远程仓库创建了新的分支,可以通过 Git 拉取到本地

sh 复制代码
git pull [远程仓库] [远程分支]:[本地分支]

#拉取远程仓库的新分支
git pull origin
#拉取(更新)远程仓库某分支的内容到本地分支
git pull origin dev:dev

查看仓库分支

sh 复制代码
git branch #只能查看本地分支
git branch -r #查看远程仓库分支
git branch -a #远程和本地都查看

git branch -v #查看本地分支信息
git branch -vv #在显示信息的基础上额外显示连接的远程分支

本地分支与远程分支连接

将本地分支和远程分支连接,可以简化我们在拉取和推送时的命令

如果没有建立连接,那么拉取和推送时都需要输入完整命令

sh 复制代码
git pull [远程仓库] [远程分支]:[本地分支]
git push [远程仓库] [本地分支]:[本地分支]

而建立连接后,例如本地dev远程dev建立连接,那么在本地dev拉取和推送只需简单的命令

sh 复制代码
git pull
git push

建立连接有两种方式

  1. 创建本地分支时,指明连接的远程分支
sh 复制代码
git checkout -b [本地分支] [远程分支]
#例
git checkout -b dev origin/dev
  1. 本地分支创建完后,在指明连接
sh 复制代码
git branch --set-upstream-to=[远程分支] [本地分支]
#例
git branch --set-upstream-to=origin/dev dev

使用如下命令可以查看连接情况

sh 复制代码
git branch -vv 

ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git branch -vv
* dev    4b2de81 [origin/dev] add file:file.txt
  master 4b2de81 [origin/master] add file:file.txt

场景一

如果两个用户都对本地仓库的file.txt做修改并先后提交推送,会发生什么

假设Linux用户先完成开发并推送至远程仓库

sh 复制代码
#file.txt原先的内容
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ cat file.txt 
hello git
#开发
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ vim file.txt 
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ cat file.txt 
hello git
coding on Linux...
#添加,提交,推送
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git add .
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git commit -m "md file.txt:add one line"
[dev 62451e5] md file.txt:add one line
 1 file changed, 1 insertion(+)
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git push origin dev:dev
Username for 'https://gitee.com': bao-bao-hai-mian
Password for 'https://bao-bao-hai-mian@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), 292 bytes | 292.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 c4458a8a
To https://gitee.com/bao-bao-hai-mian/remote_git.git
   4b2de81..62451e5  dev -> dev

此时在远程仓库的dev分支下也能看到修改

此时再在Windows上进行开发并推送

sh 复制代码
#查看file.txt原先内容
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev)
$ cat file.txt
hello git
#完成开发
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev)
$ cat file.txt
hello git
coding on Windows...
#添加,提交,推送
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev)
$ git add .
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev)
$ git commit -m "md file.txt:add one line"
[dev 72df7f6] md file.txt:add one line
 1 file changed, 1 insertion(+)
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev)
$ git push origin dev:dev

To https://gitee.com/bao-bao-hai-mian/remote_git.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'https://gitee.com/bao-bao-hai-mian/remote_git.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

但是此时,因为Linux用户先一步完成开发并推送到远程仓库,导致远程仓库和Windows的内容不同,推送失败,Git也给出了报错信息------"更新被拒绝因为远程仓库有本地仓库没有的东西,这通常是因为另一个用户推送了相同的分支,建议先使用git pull"

所以,建议在推送前,先拉取一下远程仓库,同步一下数据

sh 复制代码
#从远程仓库拉取内容
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 272 bytes | 22.00 KiB/s, done.
From https://gitee.com/bao-bao-hai-mian/remote_git
   4b2de81..62451e5  dev        -> origin/dev
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
#有提示产生了合并冲突
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev|MERGING)
$ cat file.txt
hello git
<<<<<<< HEAD
coding on Windows...
=======
coding on Linux...
>>>>>>> 62451e5e20daec9dec13e9243719e8fce343f8ef

Git提示产生了合并冲突,我们需要手动更改代码,再提交推送。
注意:产生了合并冲突,在解决冲突后,一定要记得再提交推送一次

sh 复制代码
#解决了合并冲突
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev|MERGING)
$ cat file.txt
hello git
coding on Linux...
coding on Windows...
#推送三板斧
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev|MERGING)
$ git add .
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev|MERGING)
$ git commit -m "solve the merge conflict"
[dev ab49781] solve the merge conflict
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (dev)
$ git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 16 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 567 bytes | 567.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 7b4c81a4
To https://gitee.com/bao-bao-hai-mian/remote_git.git
   62451e5..ab49781  dev -> dev

远程仓库可以看到最终结果

由此,两名开发者已经可以进行协同开发了,不断的git pull/add/commit/push,遇到冲突,就手动解决再推送

要想看到小伙伴的代码,只需要pull一下即可


最后,虽然是在分支上进行多人协作开发,但最终目的还是将开发后的代码合并到master上,让我们的项目运行最新的代码

接下来是建议流程:

  1. 切换到master,先git pull拉取最新内容
  2. 在切换到dev,合并master分支,如果有冲突,解决冲突
  3. 最后切换到master,合并dev,最后推送到远程仓库
sh 复制代码
#切换到master,拉取最新内容
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git checkout master 
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git pull
Already up to date.
#切回dev,合并master,有冲突解决冲突
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git checkout dev
Switched to branch 'dev'
Your branch is up to date with 'origin/dev'.
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git merge master 
Already up to date.
#切回master,合并dev,最后推送至远程仓库
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git checkout master 
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git merge dev
Updating 4b2de81..ab49781
Fast-forward
 file.txt | 2 ++
 1 file changed, 2 insertions(+)
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git add .
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git commit -m "merge dev"
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 644090b5
To https://gitee.com/bao-bao-hai-mian/remote_git.git
   4b2de81..ab49781  master -> master

最后在远程仓库的master分支也能看到修改后的内容

开发完成后,就可以将dev分支删除了

远程仓库删完分支后,在本地依然还能看到远程分支

sh 复制代码
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git branch -a
  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

需要使用命令来删除

sh 复制代码
git remote prune [远程仓库]

ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git remote prune origin 
Pruning origin
URL: https://gitee.com/bao-bao-hai-mian/remote_git.git
 * [pruned] origin/dev
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git branch -a
  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

可以看到,远程仓库的dev被删除了,最后删除本地的dev分支即可

总结一下,在同一分支下进行多人协作的工作模式如下:

  • 首先,可以试图使用 git push origin [分支] 推送自己的修改
  • 如果推送失败,则因为远程分支比本地更新,需要先git pull拉取新内容
  • 如果合并有冲突,则解决冲突,并在本地提交
  • 没有冲突或者解决完冲突后,再用git push origin [分支]推送就能成功
  • 功能开发完成后,使用建立的流程,使得master合并开发分支,并推送,最后删除开发分支

场景二

一般情况下,如果有多需求需要多人同时进行开发,并不会在一个分支上进行多人开发,而是一个需求或一个功能点就创建一个feature分支

现在同时有两个需求需要开发,所以需要两个用户各自创建一个分支来完成开发

本次展示在本地创建分支,再通过推送的方式发到远端
Linux用户

sh 复制代码
#创建并切换feature-1分支
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git checkout -b feature-1
Switched to a new branch 'feature-1'
#完成需求开发
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ vim function1
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ cat function1 
demand1 Done!
#推送三板斧
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git add .
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git commit -m "complete demand1"
[feature-1 4898bf6] complete demand1
 1 file changed, 1 insertion(+)
 create mode 100644 function1
#因为远程没有仓库,所以可以在推送时指明建立
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git push --set-upstream origin feature-1
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 285 bytes | 285.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 f542d8bc
remote: Create a pull request for 'feature-1' on Gitee by visiting:
remote: https://gitee.com/bao-bao-hai-mian/remote_git/pull/new/bao-bao-hai-mian:feature-1...bao-bao-hai-mian:master
To https://gitee.com/bao-bao-hai-mian/remote_git.git
 * [new branch]      feature-1 -> feature-1
Branch 'feature-1' set up to track remote branch 'feature-1' from 'origin'.

因为远程仓库并没有feature-1分支,可以有以下两种方式

  1. 指定推送,远程仓库会创建指定分支
sh 复制代码
git push [远程仓库] [本地分支]:[远程分支]
#例
git push origin feature-1:feature-1

但是此时只是创建远程分支,并没有建立连接,建立连接可以简化拉取和推送的命令

建立连接

sh 复制代码
git branch --set-upstream-to=[远程分支] [本地分支]
#例
git branch --set-upstream-to=origin/feature-1 feature-1
  1. 推送时,直接建立连接
sh 复制代码
git push --set-upstream [远程仓库] [本地分支]:[远程分支]
#例,若本地和远程分支名相同,可简写
git push --set-upstream origin feature-1

Windows

sh 复制代码
#创建并切换分支
$ git checkout -b feature-2
Switched to a new branch 'feature-2'
#完成需求开发
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (feature-2)
$ cat funciton2
demand2 Done!
#三板斧
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (feature-2)
$ git add .
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (feature-2)
$ git commit -m "complete demand2"
[feature-2 4dff28d] complete demand2
 1 file changed, 1 insertion(+)
 create mode 100644 funciton2
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (feature-2)
$ git push --set-upstream origin feature-2:feature-2
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 273.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 1e4a5894
remote: Create a pull request for 'feature-2' on Gitee by visiting:
remote: https://gitee.com/bao-bao-hai-mian/remote_git/pull/new/bao-bao-hai-mian:feature-2...bao-bao-hai-mian:master
To https://gitee.com/bao-bao-hai-mian/remote_git.git
 * [new branch]      feature-2 -> feature-2
branch 'feature-2' set up to track 'origin/feature-2'.

此时,双方都完成推送,在本地,你看不见他的文档,他看不见你的文档,推送时也没有任何冲突,互补影响

此时,远程仓库状态:


如果此时,Windows用户生病了,需要你帮忙完成一下需求开发,你需要

  1. 拉取feature-2分支
  2. 完成开发
  3. 推送
sh 复制代码
#拉取分支
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git pull origin feature-2:feature-2
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 253 bytes | 253.00 KiB/s, done.
From https://gitee.com/bao-bao-hai-mian/remote_git
 * [new branch]      feature-2  -> feature-2
 * [new branch]      feature-2  -> origin/feature-2
#查看连接
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git branch -vv
* feature-1 4898bf6 [origin/feature-1] complete demand1
  feature-2 4dff28d complete demand2
  master    ab49781 [origin/master] solve the merge conflict
#建立连接
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git branch --set-upstream-to=origin/feature-2 feature-2
#切换分支
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git checkout feature-2
Switched to branch 'feature-2'
Your branch is up to date with 'origin/feature-2'.
#帮忙开发
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ vim funciton2 
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ cat funciton2 
demand2 Done!
Help coding
#三板斧
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git add .
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git commit -m "help coding"
[feature-2 8103ba9] help coding
 1 file changed, 2 insertions(+), 1 deletion(-)
ubuntu@VM-12-11-ubuntu:~/lesson/remote_git$ git push
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), 279 bytes | 279.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 9c4b1a99
To https://gitee.com/bao-bao-hai-mian/remote_git.git
   4dff28d..8103ba9  feature-2 -> feature-2

然后,Windows用户休息回来,继续开发或者开发已经完成,推送至远程

  1. 首先要拉取分支
  2. 继续开发或推送

开发完成,就可以合并分支了。还是使用建议的流程

  1. 切换到master,先git pull拉取最新内容
  2. 在切换到feature2,合并master分支,如果有冲突,解决冲突
  3. 最后切换到master,合并feature2,最后推送到远程仓库
sh 复制代码
#切换到master
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (feature-2)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
#拉取最新内容
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (master)
$ git pull
Already up to date.
#切回feature-2
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (master)
$ git checkout feature-2
Switched to branch 'feature-2'
Your branch is up to date with 'origin/feature-2'.
#合并master
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (feature-2)
$ git merge master
Already up to date.
#切回master
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (feature-2)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
#合并feature-2
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (master)
$ git merge feature-2
Updating ab49781..8103ba9
Fast-forward
 funciton2 | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 funciton2
#三板斧
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (master)
$ git add .
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (master)
$ git commit -m "merge feature-2"
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (master)
$ git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag ca41d591
To https://gitee.com/bao-bao-hai-mian/remote_git.git
   ab49781..8103ba9  master -> master
Lenovo@ljh MINGW64 ~/Desktop/code/remote_git (master)
$

Linux后提交,也是一样的流程,此处不过多展示

合并完后,删除开发分支

以上就是本篇博客的所有内容,感谢你的阅读

如果觉得本篇文章对你有所帮助的话,不妨点个赞支持一下博主,拜托啦,这对我真的很重要。

相关推荐
Beau_Will2 分钟前
数据结构-树状数组专题(1)
数据结构·c++·算法
哎呦喂-ll29 分钟前
Linux进阶:环境变量
linux
Rverdoser30 分钟前
Linux环境开启MongoDB的安全认证
linux·安全·mongodb
hunandede34 分钟前
av_image_get_buffer_size 和 av_image_fill_arrays
c++
PigeonGuan41 分钟前
【jupyter】linux服务器怎么使用jupyter
linux·ide·jupyter
东华果汁哥1 小时前
【linux 免密登录】快速设置kafka01、kafka02、kafka03 三台机器免密登录
linux·运维·服务器
咖喱鱼蛋2 小时前
Ubuntu安装Electron环境
linux·ubuntu·electron
ac.char2 小时前
在 Ubuntu 系统上安装 npm 环境以及 nvm(Node Version Manager)
linux·ubuntu·npm
肖永威2 小时前
CentOS环境上离线安装python3及相关包
linux·运维·机器学习·centos