Basic question but this happens to me all the time:
- Make changes in a
working-branch - Switch to
master git merge working-branchgit pushcap 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)
- move all edits over to
working-branchand clearmasterso I can continue editing onworking-branchor - spin edits into an entirely new branch
new-working-branchand then discardworking-branch?
Took a risk and tried the 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-branchand then discardworking-branchAs 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-branchIf your changes don’t conflict with any changes that are on master, but not in working-branch, this can be done a lot simpler: