问题
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 clearmaster
so I can continue editing onworking-branch
- to spin edits into an entirely new branch
new-working-branch
and then discardworking-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>
回答1:
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
回答2:
If you already committed your changes to master
but didn't push to anywhere...
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
git rebase --onto working-branch origin/master newfeat
change to master
branch and reset it to the state of the last push
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
.
回答3:
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.
回答4:
I generally recommend the following Git setting:
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.
回答5:
Get in the habit of typing $ git status
before you actually do a git command that will modify something.
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.
If you have fired a commit off to master, then just move the file between branches with something like this:
$ 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...
$ git reset master origin/master
$ git reset stage origin/stage # whatever
回答6:
1. prevent direct edits on master (warning perhaps)
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:
[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. These two links on Stack Overflow should help with your prompt.
2. to move all edits over to working-branch and clear master so I can continue editing on working-branch
or
3. to spin edits into an entirely new branch new-working-branch and then discard 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:
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:
[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.
[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
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.
Now reset master to 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.
来源:https://stackoverflow.com/questions/14506910/git-move-changes-off-of-master-branch