In my Git repository, I created three commits in a row: commit1, commit2, and commit3.
I then realized I messed up in commit2 and commit3, and decided to go back to commit1. To do that, I ran
git checkout commit1
Now I am in commit1. How do I delete commit2 and commit3?
Based on your description and under the assumption you were on some branch called mybranch before checking out commit1 (C1 in my graphs below), you must be in the following situation:
C1 [HEAD]
\
C2 -- C3 [mybranch]
Commits C2 and C3 still appear in the output of git log because they're still reachable from the mybranch refererence. Also, note that HEAD is detached. What you should do is...
Reattach HEAD to mybranch, by running
git checkout mybranch
This should put you in the following situation:
C1
\
C2 -- C3 [HEAD -> mybranch]
Reset the mybranch branch to its tip's grandparent, by running
git reset --hard mybranch~2
That should put you in the following situation:
C1 [HEAD -> mybranch]
Because commits C2 and C3 have now become unreachable (i.e. "deleted"), they're not shown on this last graph.
This may be a bit cheeky, but here is an explanation of why the other two answers won't work. As correctly pointed out by cmbuckley in his comment,
git resetresets the state of the current branch you’re on (so you’d need to be on the branch to do that). If you’ve checked outcommit1, you’re probably not on a branch (detached HEAD state).
Since the OP (Imray) is in detached HEAD state, running git-reset before reattaching HEAD to the branch will not move the branch reference in question. Here is a toy example illustrating this.
# set things up
$ mkdir test
$ cd test
$ git init
Initialized empty Git repository in /Users/jubobs/Desktop/test/.git/
# create a first commit
$ touch README
$ git add .
$ git commit -m "add README"
[master (root-commit) 85137ba] add README
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
# create a second commit
$ printf "foo\n" > README
$ git commit -am "write 'foo' in README"
[master 3948e84] write 'foo' in README
1 file changed, 1 insertion(+)
# inspect the log
$ git log --graph --decorate --oneline --all
* 3948e84 (HEAD, master) write 'foo' in README
* 85137ba add README
# check out the second commit (which detaches the HEAD)
$ git checkout 3948e84
Note: checking out '3948e84'.
# (boilerplate stdout is omitted...)
HEAD is now at 3948e84... write 'foo' in README
# reset to the first commit (equivalent to 'git reset --hard 85137ba')
$ git reset --hard HEAD^
HEAD is now at 85137ba add README
$ git log --graph --decorate --oneline --all
* 3948e84 (master) write 'foo' in README
* 85137ba (HEAD) add README
Note that the git reset command moved HEAD to the initial commit, but did not move the master branch whatsoever. The second commit is not "deleted", because it's still reachable from master; it is therefore listed in the output of git log.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With