Git – master is ahead of origin/master – Don't want it to be

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?

  • Customising the default TFS Git build template
  • Using SubModules on AppHarbor
  • reverting push'd git commit
  • GIT - cannot push because my path is interpreted by git
  • How do I update my core project when using composer?
  • not sure what to use rebase or merge?
  • Jenkins credentials plugin and GIT client not working
  • gitlab6.0, Error Connection refused - connect(2) (Errno::ECONNREFUSED)
  • Git cloning: remote end hung up unexpectedly, tried changing postBuffer but still failing
  • TortoiseGit - When does it ask for a username and password?
  • How to run “grunt build” during Heroku deployment (angular and ruby-on-rails app)
  • Cannot commit my .vim contents to my git repo
  • 3 Solutions collect form web for “Git – master is ahead of origin/master – Don't want it to be”

    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.

    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.

    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

    Git Baby is a git and github fan, let's start git clone.