[Git] ~Git笔记_5/6
==== 远程分支 ====
协同开发——简单实例 "shell log"
服务端:
[jesse@localhost ToWang]$ ls [jesse@localhost ToWang]$ [jesse@localhost ToWang]$ [jesse@localhost ToWang]$ mkdir git_test [jesse@localhost ToWang]$ ls git_test [jesse@localhost ToWang]$ cd git_test/ [jesse@localhost git_test]$ ls [jesse@localhost git_test]$ [jesse@localhost git_test]$ 建立proj目录, 然后git初始化。 [jesse@localhost git_test]$ git init Initialized empty Git repository in /workMe/ToWang/git_test/.git/ [jesse@localhost git_test]$ git config --global user.name "jesse0110" [jesse@localhost git_test]$ git config --global user.email "jesse0110@test.com" [jesse@localhost git_test]$ [jesse@localhost git_test]$ [jesse@localhost git_test]$ touch test01.txt [jesse@localhost git_test]$ echo test01 > test01.txt [jesse@localhost git_test]$ [jesse@localhost git_test]$ [jesse@localhost git_test]$ touch test02.txt [jesse@localhost git_test]$ echo test02 > test02.txt [jesse@localhost git_test]$ [jesse@localhost git_test]$ [jesse@localhost git_test]$ cat test01.txt test01 [jesse@localhost git_test]$ cat test02.txt test02
// 以上工程中,简单的创建了两个文件
- 将修改提交:
[jesse@localhost git_test]$ git add . [jesse@localhost git_test]$ git commit -m "git_server" [master (root-commit) b0ca28f] git_server 2 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 test01.txt create mode 100644 test02.txt
- 建立仓库:
—— git clone --bare 将当前proj 打包成仓库,供客户下载
[jesse@localhost git_test]$ git clone --bare . /opt/test.git //建立仓库 Initialized empty Git repository in /opt/test.git/
客户端:
user1, user2下载工程:
[jesse@localhost git_test]$ cd .. [jesse@localhost ToWang]$ git clone 10.1.95.8:/opt/test.git/ user1 // user1 下载仓库 Initialized empty Git repository in /workMe/ToWang/user1/.git/ jesse@10.1.95.8's password: remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 0 (delta 0) Receiving objects: 100% (4/4), done. [jesse@localhost ToWang]$ git clone 10.1.95.8:/opt/test.git/ user2 // user2 下载仓库 Initialized empty Git repository in /workMe/ToWang/user2/.git/ jesse@10.1.95.8's password: remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 0), reused 0 (delta 0) Receiving objects: 100% (4/4), done. [jesse@localhost ToWang]$ [jesse@localhost ToWang]$ ls git_test user1 user2 //下载仓库完毕 [jesse@localhost ToWang]$ cd user1/ [jesse@localhost user1]$ ls test01.txt test02.txt [jesse@localhost user1]$ cat test01.txt test02.txt test01 test02 //下载无误 [jesse@localhost user1]$ [jesse@localhost user1]$ [jesse@localhost user1]$ git config --global user.name "user1" [jesse@localhost user1]$ git config --global user.email "user1@test.com" [jesse@localhost user1]$ cd ../user2/ [jesse@localhost user2]$ [jesse@localhost user2]$ ls -a . .. .git test01.txt test02.txt [jesse@localhost user2]$ git config --global user.name "user2" [jesse@localhost user2]$ git config --global user.email "user2@test.com"
- user2 修改工程:
[jesse@localhost user2]$ //下载完毕,user2开始添加文件test_user2.txt [jesse@localhost user2]$ touch test_user2.txt [jesse@localhost user2]$ echo test_user2 > test_user2.txt [jesse@localhost user2]$ cat test_user2.txt test_user2 //添加无误 [jesse@localhost user2]$ git add // 本地提交
[jesse@localhost user2]$ git commit -m "user2 add test_user2.txt" [master 6e803b8] user2 add test_user2.txt 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 test_user2.txt [jesse@localhost user2]$ [jesse@localhost user2]$ git push origin // 远程推送 jesse@10.1.95.8's password: Counting objects: 4, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 307 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To 10.1.95.8:/opt/test.git/ b0ca28f..6e803b8 master -> master
- user1 同步工程:
[jesse@localhost user2]$ cd ../user1/ //user1开始操作 [jesse@localhost user1]$ ls test01.txt test02.txt
[jesse@localhost user1]$ git fetch jesse@10.1.95.8's password: remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From 10.1.95.8:/opt/test b0ca28f..6e803b8 master -> origin/master [jesse@localhost user1]$ git diff master origin/master //下载远程仓库 diff --git a/test_user2.txt b/test_user2.txt new file mode 100644 index 0000000..f078340 --- /dev/null +++ b/test_user2.txt @@ -0,0 +1 @@ +test_user2 [jesse@localhost user1]$ ls test01.txt test02.txt [jesse@localhost user1]$ [jesse@localhost user1]$ git merge origin/master //远程仓库与本地库合并 Updating b0ca28f..6e803b8 Fast-forward test_user2.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 test_user2.txt
[jesse@localhost user1]$ ls test01.txt test02.txt test_user2.txt //合并完毕 [jesse@localhost user1]$ cat test_user2.txt test_user2
==== 远程分支的创建 ====
- 跟踪分支
从远程分支检出的本地分支,称为跟踪分支(tracking branch)。跟踪分支是一种和远程 分支有直接联系的本地分支。在跟踪分支里输入git push,Git 会自行推断应该向哪个服 务器的哪个分支推送数据。反过来,在这些分支里运行git pull 会获取所有远程索引,并把它们的数据都合并到本地分支中来.
$ git push ssh://git@dev.lemote.com/rt4ls.git master // 把本地仓库提交到远程仓库的master分支中 $ git remote add origin ssh://git@dev.lemote.com/rt4ls.git $ git push origin master
我从master分支创建了一个issue5560分支,做了一些修改后,使用git push origin master提交,但是显示的结果却是'Everything up-to-date',
发生问题的原因是git push origin master 在没有track远程分支的本地分支中默认提交master分支,因为master分支默认指向了origin master 分支,这里要使用git push origin issue5560:master 就可以把issue5560推送到远程的master分支了。
- 控制远程分支
如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的master分支,或者作为另外一个名叫test的分支,那么可以这么做。
$ git push origin test:master // 提交本地test分支作为远程的master分支 $ git push origin test:test // 提交本地test分支作为远程的test分支
如果想删除远程的分支呢?类似于上面,如果:左边的分支为空,那么将删除:右边的远程的分支。
$ git push origin :test // 刚提交到远程的test将被删除,但是本地还会保存的,不用担心
1. 远程分支就是本地分支push到服务器上的时候产生的。比如master就是一个最典型的远程分支(默认)。
$: git push origin master
除了master之外,我们还可以随便创建分支,然后push到服务器上去。例如:
$: git push origin develop Counting objects: 27, done. Delta compression using up to 2 threads. Compressing objects: 100% (15/15), done. Writing objects: 100% (15/15), 7.30 KiB, done. Total 15 (delta 10), reused 0 (delta 0) To git@search.ued.taobao.net:projects/search.git 1b95a57..779dbe1 develop -> develop
2. 远程分支和本地分支需要区分,所以,在从服务器上拉取特定分支的时候,需要指定本地分支名字。
$: git checkout --track origin/develop
注意该命令由于带有--track参数,所以要求git1.6.4以上!这样git会自动切换到develop分支。
3. 同步本地远程分支:
$: git fetch origin
4. 提交分支数据到远程服务器:
$: git push origin <local_branch_name>:<remote_branch_name>
例如:
$: git push origin develop:develop
当然如果当前在develop分支下,也可以直接
$: git push
5. 删除远程分支develop:
$: git push origin :develop
http://hi.baidu.com/kebey2004/item/bed22c7bf3921f366e2
http://hi.baidu.com/kebey2004/item/cdf7af992856bcdd1f4271d89f6dc
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律