Remove pushed commits from Git

I have the following story in my thread.

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

I want to remove commit 2 and 3 from history. So commit 1 is the last. I am the only one 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 cannot enter commands. What can I do?

EDIT:

press the button

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.
+4
source share
4 answers

If you want to undo the commit, but save the changes you made to the files:

git reset --soft HEAD^2

If you want to undo commit And undo changes in local files:

git reset --hard HEAD^2

Head^X means the Xth commit to the last commit in the current branch.

+2

, , , commit2 commit3 , .

, . . , , , git rebase --abort. .

, , commit1.

git checkout master

origin/master, .

git push -f

, ! , , .

+1

You can return to these two commits: 2 and 3:

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

or git reset:

git reset --hard "commit 1"
0
source

If you want to commit on top of the current HEAD with the exact state for another commit, having canceled all the intermediate commits, you can use reset to create the correct index state to 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
0
source

Source: https://habr.com/ru/post/1524062/


All Articles