Git:将更改从主分支移开 [英] Git: move changes off of master branch

查看:125
本文介绍了Git:将更改从主分支移开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  • 工作分支

  • 切换到 master

  • git merge working-branch


  • git push
  • code> cap deploy
    (转场)
  • 做一杯新茶



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

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


  1. 可防止对主人进行直接编辑(可能会警告)

  2. 将所有修改移至 working-branch 并清除 master ,所以我可以继续编辑 working-branch
  3. 编辑成一个全新的分支 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:

  1. prevent direct edits on master (warning perhaps)
  2. to move all edits over to working-branch and clear master so I can continue editing on working-branch
  3. 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:将更改从主分支移开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆