Git合并分支代码的正确方法

1、环境准备

假定您之前已经装好了git,而且已经能git pull、git add、git commit -m “评论”、git push和创建分支等操作;

我的操作目的是:将分支开发的代码合并到主干上,所以使用git checkout master,切换到主干上。

进入Windows PowerShell,cd到你要操作项目的根目录,也就是.git文件所在的目录;

 2、git status

获取当前本地的状态

PS D:\phpstudy_pro\WWW\program> git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   client/coupon.php
        new file:   server/view/base/main_chat.html
        new file:   server/view/js/complaint/datatables_template.js

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   server/user/adapay_apply.html
        modified:   server/user/buy_list.html

3、git stash

暂时缓存本地的修改

PS D:\phpstudy_pro\WWW\program> git stash 

4、git branch -a

 获取所有本地仓库和远程仓库列表

PS D:\phpstudy_pro\WWW\program> git branch -a
* master
  multi_client
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/multi_client

 5、 git merge --no-ff remotes/origin/multi_client -m "合并分支代码"

  将分支叫multi_client的分支合并进来

PS D:\phpstudy_pro\WWW\program> git merge --no-ff remotes/origin/multi_client -m "合并多端聊天代码"
Merge made by the 'ort' strategy.
 client/chat.php                |  1 +
 client/user.php                | 16 +++++++++++++++-
 im/app/Events.php              | 26 +++++++++++++++++++++++++-
 server/user/apply_binding.html |  2 +-
 4 files changed, 42 insertions(+), 3 deletions(-)

6、git stash pop

将之前git stash缓存的“本地修改”再还原到本地

PS D:\phpstudy_pro\WWW\program> git stash pop
On branch master
Your branch is ahead of 'origin/master' by 12 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   client/coupon.php
        new file:   server/view/base/main_chat.html
        new file:   server/view/js/complaint/datatables_template.js

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   server/user/adapay_apply.html
        modified:   server/user/buy_list.html
        modified:   server/user/goods_list.html
        modified:   server/view/base/main_goods.html
        modified:   server/view/base/main_user.html
        modified:   server/view/complaint/main_community_comment.html
        modified:   server/view/complaint/main_earnest.html
        modified:   server/view/js/approve/main_set_badge.js
        modified:   server/view/js/approve/sms_batch.js
        modified:   server/view/js/common/load_sys_menu.js
        modified:   server/view/js/login/login.js
        modified:   server/view/login.html
        modified:   server/view/main.html
        modified:   test.php

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .htaccess
        nginx.htaccess
        res/images/2023-04-17/
        res/images/2023-04-18/
        res/images/instructions.webp
        vendor/workerman/workerman.log

Dropped refs/stash@{0} (790243dd7a185f79caf09283c16706ef26a0c935)

7、git push 

最后别忘了,git push将合过来的新代码上传到远程仓库上

PS D:\phpstudy_pro\WWW\program> git push
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.                         
Writing objects: 100% (1/1), 245 bytes | 245.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0        
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/saycome/php-program.git
   6fb11c88..cc6c41f8  master -> master

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当你想将某个分支代码合并到测试分支(通常称为`test`或`dev`)时,你需要遵循以下步骤: 1. **切换到目标分支**: 首先,确保你已经在测试分支上工作,可以使用命令行中的 `git checkout test` 或者 `git switch test` 来切换。 2. **获取远程分支更新**: 如果有任何远程分支的更新,你需要拉取这些更改,输入 `git pull origin <branch_name>`,其中 `<branch_name>` 是你想合并分支名称。 3. **查看并选择合并策略**: 在Git中,你可以用 `git merge` 或 `git rebase` 进行合并,这取决于你的团队偏好和项目需求。`merge` 更保守,会创建一个新的提交合并历史,而 `rebase` 则会基于目标分支的新历史重新排列你的提交。 4. **合并代码**: 使用 `git merge branch_name` 命令执行合并操作,如果发现冲突,Git会提示你解决冲突后保存文件。 5. **检查冲突和错误**: 打开合并冲突的文件,修复冲突,并确保所有修改都是正确的。 6. **添加、提交和推送**: 解决完冲突后,添加修改 (`git add .` 或指定具体文件),然后用 `git commit -m "Merge branch <branch_name> into test"` 提交这次合并。最后推送到远程仓库:`git push origin test`. 7. **代码审查(可选)**: 如果你们团队有代码审查流程,现在可能需要请求他人审核你的合并。 8. **构建和测试**: 在推送后,确保构建系统和自动化测试通过,确认新代码没有引入新的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁静的海2006

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值