Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: move changes off of master branch

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> 
like image 918
Meltemi Avatar asked Jan 24 '13 17:01

Meltemi


People also ask

How do you move uncommitted changes to a new branch?

Create a new feature branch, say feature, and then switch to that branch. Implement the feature and commit it to our local repository. Push to the feature branch to the remote repository and create a pull request. After other teammate's review, the new change can be merged into the master or release branch.

How do I push changes from one branch to another?

Push Branch to Another Branch In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.


2 Answers

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 
like image 161
Chronial Avatar answered Oct 13 '22 17:10

Chronial


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 of working-branch.
like image 25
KurzedMetal Avatar answered Oct 13 '22 18:10

KurzedMetal