[git] Git, How to reset origin/master to a commit?

I reset my local master to a commit by this command:

git reset --hard e3f1e37

when I enter $ git status command, terminal says:

# On branch master
# Your branch is behind 'origin/master' by 7 commits, and can be fast-forwarded.

#   (use "git pull" to update your local branch)
#
nothing to commit, working directory clean

Since I want to reset origin/header as well, I checkout to origin/master:

$ git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 2aef1de... master problem fixed for master. its okay now.

and reset the header by this command:

$ git reset --hard e3f1e37
HEAD is now at e3f1e37 development version code incremented for new build.

Then I tried to add commit to origin/header that I was not successful.

$ git commit -m "Reverting to the state of the project at e3f1e37"
# HEAD detached from origin/master
nothing to commit, working directory clean

Finally, I checkout to my local master.

$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 7 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Since, I reset the head of origin/master I expect local and origin should be in same direction but as you see, git is saying that my local/master is behind origin/master by 7 commits.

How can I fix this issue? The things that I'm looking for is Head of local/master and origin/master point to same commit. Following image shows what I did. Thanks.

enter image description here

This question is related to git git-checkout git-reset

The answer is


Assuming that your branch is called master both here and remotely, and that your remote is called origin you could do:

git reset --hard <commit-hash>
git push -f origin master

However, you should avoid doing this if anyone else is working with your remote repository and has pulled your changes. In that case, it would be better to revert the commits that you don't want, then push as normal.


Since I had a similar situation, I thought I'd share my situation and how these answers helped me (thanks everyone).

So I decided to work locally by amending my last commit every time I wanted to save my progress on the main branch (I know, I should've branched out, committed on that, kept pushing and later merge back to master).

One late night, in paranoid fear of loosing my progress to hardware failure or something out of the ether, I decided to push master to origin. Later I kept amending my local master branch and when I decided it's time to push again, I was faced with different master branches and found out I can't amend origin/upstream (duh!) like I can local development branches.

So I didn't checkout master locally because I already was after a commit. Master was unchanged. I didn't even need to reset --hard, my current commit was OK.

I just forced push to origin, without even specifying what commit I wanted to force on master since in this case it's whatever HEAD is at. Checked git diff master..origin/master so there weren't any differences and that's it. All fixed. Thanks! (I know, I'm a git newbie, please forgive!).

So if you're already OK with your master branch locally, just:

git push --force origin master
git diff master..origin/master

The solution found here helped us to update master to a previous commit that had already been pushed:

git checkout master
git reset --hard e3f1e37
git push --force origin e3f1e37:master

The key difference from the accepted answer is the commit hash "e3f1e37:" before master in the push command.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to git-checkout

How can I switch to another branch in git? Git checkout - switching back to HEAD What is git tag, How to create tags & How to checkout git remote tag(s) How can I move HEAD back to a previous location? (Detached head) & Undo commits error: pathspec 'test-branch' did not match any file(s) known to git git checkout all the files How can I check out a GitHub pull request with git? "Cannot update paths and switch to branch at the same time" error: Your local changes to the following files would be overwritten by checkout Git command to checkout any branch and overwrite local changes

Examples related to git-reset

How to undo the last commit in git How can I move HEAD back to a previous location? (Detached head) & Undo commits What is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'? Git, How to reset origin/master to a commit? Can I delete a git commit but keep the changes? What is the meaning of git reset --hard origin/master? How to git reset --hard a subdirectory? git undo all uncommitted or unsaved changes Unstaged changes left after git reset --hard How do I use 'git reset --hard HEAD' to revert to a previous commit?