git 当前分支修改转移到其他分支

本文介绍如何在Git中高效地将一个分支上的修改转移到另一个分支,包括未提交的修改使用stash和stash pop,已提交的修改使用cherry-pick的方法。
摘要由CSDN通过智能技术生成

需要在branch B 分支上修改代码, 发现不小心在branch A分支下进行了修改, 又不想在branch B上重新修改一遍代码,
怎么做更高效呢?

  1. 修改尚未commit

把branch A上修改的代码转移到branch B下,即可

# A分支下操作
$ git stash

# B分支下操作
$ git stash pop
# 查看修改显示:对应的修改合入B分支了
$ git status -sb

操作示例

# A分支下操作
$ git status -sb
## A
test.php +++
student.php +++
$ git stash
Saved working directory and index state WIP on A: 38ea1a4 Merge branch 'C' into A

# B分支下操作
$ git stash pop
On branch B
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified: test.php
        modified: student.php
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (1bb7061e772ceb0712d72fe5528efe2ea10c6cfd)
$ git status -sb
## B
 M test.php
 M student.php


...other operator

  1. 修改已经commit
# A分支下操作
$ git log
commit 542ed9acb7d54c4431eda351db53f616d826a2bd
Author: test <test.com>
Date:   Fri Apr 24 09:19:31 2020 +0800
    test update
# 根据commit时的备注,找到修改代码的commitID,复制下来
# 如 542ed9acb7d54c4431eda351db53f616d826a2bd

# B分支下操作
# 转移commit
$ git cherry-pick commitID
# 如 git cherry-pick 542ed9acb7d54c4431eda351db53f616d826a2bd

# 查看修改显示:对应commitid的修改合入B分支了
$ git status -sb
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值