Git:将更改移出主分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14506910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: move changes off of master branch
提问by Meltemi
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
- 在一个
working-branch
- 切换到
master
git merge working-branch
git push
cap deploy
(到登台)- 泡一杯新茶
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 clearmaster
so I can continue editing onworking-branch
- to spin edits into an entirely new branch
new-working-branch
and then discardworking-branch
?
- 防止对 master 的直接编辑(可能是警告)
- 将所有编辑移至
working-branch
并清除,master
以便我可以继续编辑working-branch
- 将编辑旋转到一个全新的分支
new-working-branch
然后丢弃working-branch
?
Took a risk and tried recommendation in the latter part of "Branches" section of this pagebut 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!?!
冒险并尝试在本页“分支”部分的后半部分推荐,但这只是抹去了我所有的编辑!?!也许是因为两个分支上的aftergit branch dubious-experiment
和git checkout master
thegit status
是相同的(在 master 上不是“干净的”)。所以git reset --hard <SHA1sum>
消除了两者的所有变化!?!
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>
回答by Chronial
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
如何将更改移动到新分支new-working-branch
然后丢弃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.
由于您尚未向 master 提交任何内容,因此您无需对 master 进行任何更改。如果您愿意,现在可以丢弃您的工作分支。
How to move the changes over to working-branch
如何将更改移至 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:
如果您的更改不与 master 上的任何更改冲突,但不在工作分支中,则可以更简单地完成此操作:
git stash
git checkout working-branch
git stash pop
回答by KurzedMetal
If you already committed your changes to master
but didn't push to anywhere...
如果您已经将更改提交到master
但没有推送到任何地方...
create a new branch for the last changes
为上次更改创建一个新分支
git checkout -b newfeat master
replay all the changes (move the commits) on top of your working-branch
branch
在您的working-branch
分支上重放所有更改(移动提交)
git rebase --onto working-branch origin/master newfeat
change to master
branch and reset it to the state of the last push
更改为master
分支并将其重置为上次推送的状态
git checkout master
git reset --hard origin/master
At this point you have:
此时你有:
master
pointing to the last pushed commit (origin/master
)working-branch
never changed- a new
newfeat
branch that contains all the new commits and is ahead ofworking-branch
.
master
指向最后推送的提交 (origin/master
)working-branch
从未改变- 一个
newfeat
包含所有新提交并在working-branch
.
回答by Patrick B.
I used for similar cases:
我用于类似情况:
git branch -f <branch-name>
git checkout <branch-name>
or
或者
git checkout -B <branch-name>
.
.
Both variants move the branch branch-name
to your current commit with out reseting-hard your tree.
两种变体都将分支移动branch-name
到您当前的提交,而无需重置您的树。
回答by Rob Starling
I generally recommend the following Git setting:
我通常推荐以下 Git 设置:
git config push.default nothing
With this, you will at least have to name the branch when you push. It won't stop you from committing to master locally, but when you realize you have, you can move those commits to a branch without affecting anyone else.
有了这个,您至少必须在推送时命名分支。它不会阻止您在本地提交 master,但是当您意识到自己已经提交时,您可以将这些提交移动到一个分支,而不会影响其他任何人。
回答by Mike Monkiewicz
1. prevent direct edits on master (warning perhaps)
1. 防止在 master 上直接编辑(可能是警告)
You're not the only one to want this. The best idea I've come across is to put the git branch directly in your shell prompt. My prompt looks like this:
你不是唯一想要这个的人。我遇到的最好的想法是将 git 分支直接放在你的 shell 提示符中。我的提示是这样的:
[user@host directory:git_branch]
I also color the git_branch entry, so it's quite obvious what I'm working on at all times. Thesetwolinks on Stack Overflow should help with your prompt.
我还为 git_branch 条目着色,因此很明显我一直在处理什么。 Stack Overflow 上的这两个链接应该有助于您的提示。
2. to move all edits over to working-branch and clear master so I can continue editing on working-branch
2.将所有编辑移至工作分支并清除母版,以便我可以继续在工作分支上进行编辑
or
或者
3. to spin edits into an entirely new branch new-working-branch and then discard working-branch?
3. 将编辑旋转到一个全新的分支 new-working-branch 然后丢弃 working-branch?
These are really the same question - how to move changes off of master onto a branch, whether it's an old branch or a new branch. And your own answer is correct. Although at second glance, assuming you're on master, you could more simply run:
这些实际上是相同的问题 - 如何将更改从 master 移到分支上,无论是旧分支还是新分支。而你自己的答案是正确的。尽管乍一看,假设您在 master 上,您可以更简单地运行:
git branch new_branch
git reset --hard origin/master
I prefer to just reset master to origin/master rather than worry about a specific commit SHA. But your steps were essentially correct. As to why you lost changes, I'd have to think that by mistake there wasn't a branch pointer to Q when you reset master. No other explanation makes sense. Again, having the branch shell prompt will help avoid these mistakes. Further more, I'm a big fan of using gitk or git log --graph to verify where my branches are before I move them around. Since I can't easily use gitk at work, I have an alias in my .gitconfig called "graph," which is essentially a command-line version of it:
我更喜欢将 master 重置为 origin/master 而不是担心特定的提交 SHA。但是你的步骤基本上是正确的。至于为什么您丢失更改,我不得不认为当您重置 master 时,错误地没有指向 Q 的分支指针。没有其他解释有意义。同样,拥有分支 shell 提示将有助于避免这些错误。此外,我非常喜欢在移动分支之前使用 gitk 或 git log --graph 来验证它们的位置。由于我在工作中无法轻松使用 gitk,因此我的 .gitconfig 中有一个名为“graph”的别名,它本质上是它的命令行版本:
[alias]
graph = log --graph --all --date=short --pretty=format':%C(yellow)%h%Cblue%d%Creset %s %Cgreen %aN, %ad%Creset'
This will show the graph on the far left, the commit SHA in yellow, the branches in blue, the commit message in white, and the author & date in green. Of course this can be modified to your own liking.
这将在最左边显示图表,黄色显示提交 SHA,蓝色显示分支,白色显示提交消息,绿色显示作者和日期。当然,这可以根据自己的喜好进行修改。
[edited to make the above commands simpler]
[编辑使上述命令更简单]
==============================
==============================
In response to the comment below:
回应以下评论:
Start with
从...开始
A-B < origin/master
\
C-D < master
Now perform git checkout -b new_branch
现在执行 git checkout -b new_branch
A-B < origin/master
\
C-D < master, new_branch
Now checkout master, git checkout master
. Note that git checkout -b new_branch && git checkout master
is the same as git branch new_branch
if you were already on master. I edited the above answer to reflect this.
现在结帐大师,git checkout master
。请注意,git checkout -b new_branch && git checkout master
这与git branch new_branch
您已经在 master 上的情况相同。我编辑了上面的答案以反映这一点。
Now reset master to origin/master, git reset --hard origin/master
现在将 master 重置为 origin/master, git reset --hard origin/master
A-B < master, origin/master
\
C-D < new_branch
Because you had a branch (new_branch) pointing at D, no changes are lost. If I've made a mistake, please elaborate where.
因为您有一个指向 D 的分支 (new_branch),所以不会丢失任何更改。如果我犯了错误,请详细说明在哪里。
回答by DigitalRoss
Get in the habit of typing $ git status
before you actually do a git command that will modify something.
养成$ git status
在实际执行会修改某些内容的 git 命令之前打字的习惯。
Given that, you have probably edited your file but not checked it in, because you would run git status
before the commit. In this case, git does the right thing if you just switch branches, then commit.
鉴于此,您可能已经编辑了您的文件但没有将其签入,因为您会git status
在提交之前运行。在这种情况下,如果你只是切换分支,然后提交,git 会做正确的事情。
If you havefired a commit off to master, then just move the file between branches with something like this:
如果你已经向 master 提交了一个提交,那么只需在分支之间移动文件,如下所示:
$ git checkout --patch master <somefile>
You don't really have to reset master if you are just going to merge the same file with it, but since presumably you haven't pushed anything yet you should just reset to your remote tracking branches...
如果你只是想合并同一个文件,你真的不需要重置 master,但是因为你可能还没有推送任何东西,你应该重置到你的远程跟踪分支......
$ git reset master origin/master
$ git reset stage origin/stage # whatever