4

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.
stefana
  • 2,606
  • 3
  • 29
  • 47

4 Answers4

2

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.

kgautron
  • 7,915
  • 9
  • 39
  • 60
  • I get "ambiguous argument 'HEAD^2': unknown revision or path" – stefana Jan 28 '14 at 12:43
  • Maybe your index is detached from the branch? Another option is to replace HEAD^2 by the identifier of the commit that you wanna go back to. – kgautron Jan 28 '14 at 12:46
  • I know how to set the current branch. The problem is that the latest two commits can be pulled. I want to completely remove them. – stefana Jan 28 '14 at 12:47
  • Ok so on your local repo you are still on commit 1. Then what you need to do is to pull the commits, then do a git revert on those commit, this will create an anticommit that will cancel them. Then commit and push. – kgautron Jan 28 '14 at 12:50
  • If I do that and execute the reset commands, then I am back at the same situation as explained in OP. – stefana Jan 28 '14 at 12:52
1

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.

Mike Monkiewicz
  • 4,481
  • 1
  • 22
  • 19
  • This seems to be the solution, but when I enter the push command it says "Bad file number","Could not read from remote repository". However, I can push via the user interfance. – stefana Jan 28 '14 at 13:53
  • Can you enter the full text of the error? And did the forced push from the user interface (SourceTree, I assume) work? – Mike Monkiewicz Jan 28 '14 at 14:02
  • It works via the UI, but not in terminal. I will add error in OP. – stefana Jan 28 '14 at 14:03
  • This problem is unrelated to your commits. You have an issue connecting to your repository via SSH. Try looking at [Git SSH error: “Connect to host: Bad file number”](http://stackoverflow.com/questions/7144811/git-ssh-error-connect-to-host-bad-file-number) or GitHub's suggestion for [Error: Bad file number](https://help.github.com/articles/error-bad-file-number) – Mike Monkiewicz Jan 28 '14 at 14:51
0

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"
user2699113
  • 4,262
  • 3
  • 25
  • 43
  • `git revert` will create new commits to reverse the affects of commit2 and commit3. But the original commit2 & commit3 are of course still there. `git reset --hard commit1` will move the pointer to the HEAD branch back to commit1, so you can work from there - letting you pretend you never even made commit2 & commit3. – Mike Monkiewicz Jan 28 '14 at 13:29
0

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
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Lalit Sachdeva
  • 6,469
  • 2
  • 19
  • 25