Git:将更改从主分支移开

编程入门 行业动态 更新时间:2024-10-24 19:22:30
本文介绍了Git:将更改从主分支移开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

基本问题,但这一直发生在我身上:

  • 在工作分支
  • 切换到 master
  • git merge working-branch
  • git push
  • code> cap deploy (转场)
  • 做一杯新茶

然后我回来想想其他的东西,然后开始做一些改变......而仍然是主人。

什么是简单的方法:b $ b

  • 可防止对主人进行直接编辑(可能会警告)
  • 将所有修改移至 working-branch 并清除 master ,所以我可以继续编辑 working-branch 编辑成一个全新的分支 new-working-branch ,然后放弃 working-branch ?
  • 冒着风险,在分行 此页面的部分,但刚刚被删除我所有的编辑!?!也许是因为在 git branch dubious-experiment 和 git checkout master git status 在两个分支上都是相同的(不是主人干净)。所以 git reset --hard< SHA1sum> 消除了对这两者的所有更改!?!

    git branch dubious-experiment M --- N ----- O ---- P --- Q(master和dubious-experiment) git checkout master #注意下一个命令:确保git status是#clean,你绝对是在master和#dubious-experiment分支的第一个工作是# git reset --hard< SHA1sum of commit N>

    解决方案

    从您的描述中,我假定您没有提交任何改变 - 是否正确?

    如果是的话,这是您的答案: 如何防止直接编辑要掌握

    您需要在编辑器中设置它,但这可能很困难。在你的提示和你的编辑器中显示你当前的分支会有很大帮助。 如何将更改移动到新分支 new-working-branch ,然后放弃工作分支

    git checkout -b新工作分支 git add ... git commit -mmycommit

    由于您还没有提交任何东西来掌握,所以您无需在主控上更改任何内容。如果您喜欢,现在可以放弃您的工作分支。

    如何将更改移至 working-branch

    git checkout -b temp-branch git add ... git commit -mmycommit git rebase --onto working-branch master git checkout working-branch git reset --hard temp-branch git branch -d temp-branch

    如果您的更改与master上的任何更改没有冲突,但不在working-branch中,这可能是做了很多简单:

    git stash git checkout working-branch git stash pop

    Basic question but this happens to me all the time:

    • Make changes in a working-branch
    • Switch to master
    • git merge working-branch
    • git push
    • cap deploy(to staging)
    • make a new cup of tea

    then I come back and think of something else and start making some changes...while still on master.

    What's an easy way to either:

  • prevent direct edits on master (warning perhaps)
  • to move all edits over to working-branch and clear master so I can continue editing on working-branch
  • to spin edits into an entirely new branch new-working-branch and then discard working-branch?
  • Took a risk and tried recommendation in the latter part of "Branches" section of this page but that just wiped out ALL my edits!?! perhaps because after git branch dubious-experiment and git checkout master the git status on both branches was identical (not 'clean' on master). So git reset --hard <SHA1sum> wiped out all changes on both!?!

    git branch dubious-experiment M---N-----O----P---Q ("master" and "dubious-experiment") git checkout master # Be careful with this next command: make sure "git status" is # clean, you're definitely on "master" and the # "dubious-experiment" branch has the commits you were working # on first... git reset --hard <SHA1sum of commit N>

    解决方案

    From your description, I assume that you did not commit any changes yet – is that correct?

    If yes, here’s your answers:

    How to prevent direct edits to master

    You would need to set that in your editor, but that will probably be difficult. Displaying your current branch in your prompt and your editor helps a lot.

    How to move the changes into a new branch new-working-branch and then discard working-branch

    git checkout -b new-working-branch git add … git commit -m "mycommit"

    As you didn’t commit anything to master yet, you don’t need to change anything on master. You can now discard your working-branch if you feel like it.

    How to move the changes over to working-branch

    git checkout -b temp-branch git add … git commit -m "mycommit" git rebase --onto working-branch master git checkout working-branch git reset --hard temp-branch git branch -d temp-branch

    If your changes don’t conflict with any changes that are on master, but not in working-branch, this can be done a lot simpler:

    git stash git checkout working-branch git stash pop

    更多推荐

    Git:将更改从主分支移开

    本文发布于:2023-11-26 18:36:53,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:移开   分支   Git

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!