My master branch is ahead of origin/master by 14 commits. I want it to be in sync with origin/master as those fourteen commits are for specific branches and not master. How do I roll back?
-
http://stackoverflow.com/questions/3719068/move-commits-from-master-onto-a-branch-using-git – NightShadeQueen Jul 29 '15 at 19:05
-
1What do you want to do with the local commits? Are they on master or other branches? (Wasn't sure from your description) – rholmes Jul 29 '15 at 19:10
3 Answers
If you want your local master to be the same as origin/master, then you want to checkout master
and run:
git reset --hard origin/master
Note this will discard any changes in the working directory.

- 232,980
- 40
- 330
- 338
Your master branch has some (14) commits which are for some other branch and not the master. So, first the work must be copied to its correct branch. Suppose the work was actually meant to be in the branch called dev-branch. Before we do that we need to switch to dev-branch.
git checkout dev-branch
Now, bring the work from master to dev-branch using the git merge command. git merge command merges the specified branch which the current (dev-branch) branch.
git merge master
In case of merge conflicts follow these instructions: http://softwarecave.org/2014/03/03/git-how-to-resolve-merge-conflicts/. If there are no conflicts proceed to next step.
After the commits have been copied to their correct branch unwanted commits from the master branch have to be removed and as per your requirement local master needs to be in sync with remote master (origin/master). git reset --hard command discards changes from the working directory to a specified state which we specified here as the state of remote master.
git reset --hard origin/master
By now all the work has been moved to your required branch, commits have been discarded from local master and local master is in sync with remote master.

- 1,359
- 9
- 10
Assuming you haven't push your code to your remote...
$git checkout yourBranch
$git rebase master
That will bring the changes from master to yourBranch
Now get rid of the commits in master
$git rebase -i HEAD~15
That will open a window with a list of the last 15 commits, delete all those lines. Then save, then push
Edit: as per @skeggse suggestion, use the last 15 commits. See comments for explanation

- 3,779
- 2
- 29
- 34
-
When you rebase interactively, and delete all the lines in the file, rebase will cancel the operation. – skeggse Jul 29 '15 at 19:40
-
1That's the best I've figured out...just make sure to delete the fourteen lines corresponding to the correct commits lol – skeggse Jul 29 '15 at 19:42
-
`git rebase -i HEAD~15` and then deleting 15 lines is a weird misuse of rebasing. Use `git reset HEAD~15` to just *move* the branch without all the weird hassle. – user229044 Jul 29 '15 at 20:45
-
he wants to delete 14 commits, it is not a weird usage of rebase, it's exactly what he needs to do. He also doesn't want to lose those commits, that's why I told him/her to rebase first against master first – Guillermo Mansilla Jul 29 '15 at 20:55