[Git]:Git当前分支的修改转移到其他分支

Git当前分支的修改转移到其他分支

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

(1) 修改尚未commit

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

# A分支下操作 把暂存区内的修改存储起来
$ git stash 
git checkout B
# B分支下操作
$ git stash pop
# 查看修改显示:对应的修改合入B分支了
$ git status -sb

git stash list查看当前stash暂存区的内容。

操作示例

# 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


(2) 修改已经commit的:

method 1:

# 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

method 2:

$ git reset HEAD^              // 撤回到刚刚commit之前的状态

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值