RebeccaK375
RebeccaK375

Reputation: 901

Git reset - branch is behind master

I made several commits (let's say with IDs: 1, 2, 3, 4) and then realized that I made a mistake in commit 3 and want to go back to the version of code at commit 2.

I did:

git reset --hard 2

Now git says:

On branch master. 
Your branch is behind origin/master by 11 commits and can be fast forwarded.

I am wondering how I can "push" my code so that everyone has this version.

Upvotes: 1

Views: 6517

Answers (3)

LeGEC
LeGEC

Reputation: 51780

Depending on what you need :

one way to just remove commit 3 from the history is :

$ git rebase -i <id of commit 2>
# an editor will open a text editor, asking you to describe what you want to do
# delete the line containing <commit 3>, save and exit

git will output some messages indicating its progress, and you should end up with a new master branch containing all the commits except <commit 3>.

You will then need to add the --force-with-lease option to push this edited master branch :

git push --force-with-lease origin master

Upvotes: 4

David Deutsch
David Deutsch

Reputation: 19015

The message you got indicates that you have already pushed commits 3 and 4, so a hard reset is not what you want to do - that command changes history, and you should never try to change the history of a commit that has been pushed. Instead, do a git pull (so that you are up to date). Then git revert 3 to undo the changes you made in commit 3.

Upvotes: 1

Roy Wang
Roy Wang

Reputation: 11260

git push -f origin to force push and alter history of your repo. Might be better to just git revert the 2 commits to keep history.

Upvotes: 3

Related Questions