Remove pushed commits from Git

I have the following history in my branch.

  • "commit 3" (origin/master)
  • "commit 2"
  • "commit 1" (master 2 behind)
  • ... previous commits

I want to remove commit 2 and 3 from the history. So that commit 1 is the latest. I am the only person using this repository.

I am using SourceTree and in the Terminal I entered the following:

git rebase -i

Then I get a colorful screen where I can't enter commands. What can I do?

EDIT:

push error

ssh: connect to host <myhost> port 22: Bad file number
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Answers


If you want to cancel the commits but keep the modifications you made in the files :

git reset --soft HEAD^2

If you want to both cancel the commits AND cancel the modifications in your local files :

git reset --hard HEAD^2

Head^X means the Xth commit before last commit on the current branch.


All these answers seem to show how to remove commit2 & commit3 from your local repository, but do not address the remote repository.

First, exit the interactive rebase you are in. That only affects local history. You might need to exit the text editor you are in or run git rebase --abort. I'm not quite sure of your exact state.

Once you are back to your normal command line, checkout master since it is already at the desired commit1.

git checkout master

Then force origin/master to update to master.

git push -f

As a warning to other readers, never do a forced push on a shared repository with other developers! This works for the original poster because they are the only one using their remote repository.


You can do revert on those two commits: 2 and 3:

git revert "commit 3"
git revert "commit 2"

or git reset:

git reset --hard "commit 1"

If you want to commit on top of the current HEAD with the exact state at a different commit, undoing all the intermediate commits, then you can use reset to create the correct state of the index to make the commit.

# reset the index to the desired tree
git reset 56e05fced

# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to 56e05fced"

# Update working copy to reflect the new commit
git reset --hard

Need Your Help

Debugging Best Practices for C++ STL/Boost with gdb

c++ stl boost gdb

Debugging with gdb, any c++ code that uses STL/boost is still a nightmare. Anyone who has used gdb with STL knows this. For example, see sample runs of some debugging sessions in code here.

jeditable datepicker causing blur when changing month

jquery jquery-ui

I'm using a jquery-ui datepicker inside of jeditable. Changing the month causes a blur. In order to be able to keep functioning after the blur, I had to set settings.onblur="cancel"