four-eyes
four-eyes

Reputation: 12500

git reset --hard to commit does not work - Tip is HEAD is behind

I want to reset my local branch to the commit as288sa. The commit is stored on the remote repository and my local repository. I want to reset using the --hard flag.

git reset --hard as288sa 

Checking out files HEAD is now at as288sa

I then want to add and commit the reset and push that commit to the remote repository.

git add -A
git commit -m "reseted hard"

On branch myBranch nothing to commit, working tree clean

git push origin myBranch

rejected error: failed to push some refs to git@... Updates were rejected because the tip of you current branch is behind its counterpart. Integrate the remote changes (e.g. git pull) before pushing changes

So I did a git pull, then git add -A and a git commit as well as a git push and tried then the git reset --hard as288sa. I get the same error message again.

What is happening?

Upvotes: 2

Views: 687

Answers (1)

Mark Adelsberger
Mark Adelsberger

Reputation: 45819

You don't "add and commit a reset"; add and commit is how you apply working tree changes to create a new commit, whereas reset just moves a ref to point to an existing commit. I would expect the add and commit commands to complain about nothing to add / empty commit, unless you had unrelated changes in your working tree.

Well, anyway, then you try to push but as you see git doesn't like it because you're trying to push a ref to an ancestor and git doesn't think that's a good idea. It tells you to do a pull, but when you do that undoes your reset by moving your local ref for myBranch right back to where the origin had put it before.

So that's what happened... but before I tell you how you can fix it, I should mention that what you're doing will cause anyone else pulling myBranch to have a problem, and if there are very many users this probably isn't what you want to do.

To do what you asked, after the reset but without doing a pull (and don't even worry about add or commit) you can do a push with the -f flag. You may or may not have permission to do this, but even if you do, seriously, it is likely not what you want to do.

Instead what I suspect you really should do is revert the commit - i.e. create a new commit that reverses the changes from the previous commits returning the tree to the same state as an earlier commit but in a way that preserves the published history. Look at the docs for git revert

Upvotes: 5

Related Questions