[Solved] Trouble with github master being ahead of local repo

[Solved] Trouble with github master being ahead of local repo


I seem to have gotten things quite messed up with my github repo. I work on two separate computers and what happened is that I made changes to a site on one computer without first getting the latest version that was on my github.

-After the changes were made I wasn’t able to commit these to the master branch without doing a pull from master.

-I did this and then commited the new changes only to find that it really messed up my code with a lot of duplicates, and random gibberish.

-I reverted to an old version of my code locally, but now I cannot push updates to the master because it thinks the master is ahead.

I get the error:

and have 1 and 2 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean```

Basically I need the local version on my computer to overwrite the master branch. How could I do this?


Once you’ve pushed changes to somewhere public you should never overwrite them. Well it is probably ok because chances are you are the only one who has cloned your github repo but imagine if there were others. You would now have messed up their copies.) But you can do a “do over” instead.

Do a git diff of your local master and the remote master. Save the results somewhere. This is the set of changes you want to keep.

Using git log Find the last commit that both your local master and and the remote master had in common. Let’s say it is abc123.

Then do git reset --hard abc123. This sets the status of your local master to the point where the branches diverged destroying your local changes in the process but don’t worry we saved them in that diff.

Now you can successfully git pull. Afterwards your local branch is in sync with remote master.

Now apply that diff to your local master clearing up any conflicts in the process. git commit this (as one or more commits as you see fit.)

git push the new commits. Everything should be synced up again.


Thank you sir, I think that solved my issue. Good thing I learned this when it was just me using the repo!