远程分支管理#180804

本文通过实战演示了如何使用Git进行分支管理,包括创建、切换、推送分支等操作,并展示了不同分支间的更新同步过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 添加新文件
  • 创建新分支
[root@localhost ~]# cd /tmp/test/
[root@localhost test]# ls
1.txt  README.md
[root@localhost test]# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:hellopasswd/test
   d3231be..798b8a1  master     -> origin/master
Updating d3231be..798b8a1
Fast-forward
 2.doc | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 2.doc
[root@localhost test]# ls
1.txt  2.doc  README.md

[root@localhost test]# git branch
* master

查看所有分支

[root@localhost test]# git ls-remote origin
798b8a137d65fd7ed86d116246881582bd509f7d	HEAD
798b8a137d65fd7ed86d116246881582bd509f7d	refs/heads/dev
798b8a137d65fd7ed86d116246881582bd509f7d	refs/heads/master

远程分支

[root@localhost test]# git checkout -b dev origin/dev
fatal: Cannot update paths and switch to branch 'dev' at the same time.
Did you intend to checkout 'origin/dev' which can not be resolved as commit?
[root@localhost test]# git fetch
From github.com:hellopasswd/test
 * [new branch]      dev        -> origin/dev

[root@localhost test]# git checkout -b dev origin/dev
Branch dev set up to track remote branch dev from origin.	#分支dev设置为跟踪来自origin的远程分支dev
Switched to a new branch 'dev'	#切换到一个新分支
[root@localhost test]# git branch
* dev
  master
[root@localhost test]# echo "333" >> 3.txt
[root@localhost test]# git add 3.txt 
[root@localhost test]# git commit -m "add 3.txt"
[dev 9dc62e6] add 3.txt
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 3.txt
[root@localhost test]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 326 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:hellopasswd/test.git
   798b8a1..9dc62e6  dev -> dev

查看dev和master文件的不同

当dev和master文件都有更新时,在push时,两个分支都会同时更新,若本地分支比远程的分支多,

指定分支

[root@localhost test]# git push origin dev
Everything up-to-date

本地分支比远程分支多

[root@localhost test]# git branch dev2
[root@localhost test]# git branch
* dev
  dev2
  master
[root@localhost test]# git checkout dev2
Switched to branch 'dev2'

[root@localhost test]# echo "dev2" >> dev2.txt
[root@localhost test]# git add dev2.txt 
[root@localhost test]# git commit -m "add dev2.txt"
[dev2 f5a6198] add dev2.txt
 Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 dev2.txt

[root@localhost test]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Everything up-to-date
[root@localhost test]# git push origin dev2
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:hellopasswd/test.git
 * [new branch]      dev2 -> dev2

查看是否存在dev2

转载于:https://my.oschina.net/hellopasswd/blog/1922486

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值