Steve
Steve

Reputation: 4563

How do I revert to the previous commit without deleting the latest commit?

Say I have 2 commits which I have already pushed to my remote branch.

How do I revert to Commit B without deleting Commit A? I just want to compare the result between these 2 commits.

Note: Code comparison is not needed. I just want to compare the output of Commit A vs Commit B

Upvotes: 6

Views: 4222

Answers (5)

Vijay122
Vijay122

Reputation: 964

You can do an interactive rebase, remove the commits you don't require, and do a hard push on your remove branch.

git rebase -i     //opens interactive rebasing window

pick #commitID2 My commit 2
pick #commitID1 My commit 1    <- To unpick/remove this commit, using editor remove 
                                  line->

save the window.
git push origin branchname -f 

Upvotes: 0

Keshan Nageswaran
Keshan Nageswaran

Reputation: 8188

How do I revert to Commit B without deleting Commit A?

You can easily use git rebase -i <commit_hash>

  • Select one commit before commit B i.e. Commit C

HEAD <- Commit A <- Commit B <- Commit C

  • git rebase -i <commit_hash_commitC>
  • Above command will list commits in your text editor that looks something like this:
pick f7f3f6d Commit B
pick a5f4a0d Commit A

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  • Remove line that represents Commit B then save and exit the text editor.
  • Git will replay all the commits mentioned in the interactive editor and will stop, which will delete Commit B completely.

Upvotes: 2

Matt
Matt

Reputation: 13973

I strongly disagree with the other answers advising you to use git revert. This would actually really revert the changes introduced by Commit A and produce a commit on its own.

Since you want to have a look at the state at a point back in time, you could just checkout Commit B directly so you can inspect the contents. Afterwards checkout the original branch to go back to the latest commit.

git checkout $HASH_OF_COMMIT_B   # now you are in a detached head state at commit B
git checkout $BRANCH             # now you are back at the tip of the branch (commit A)

A lot of tools let you see the difference between two references directly without the need to checkout. On the command line, this could be done with git diff:

git diff $HASH_OF_COMMIT_A..$HASH_OF_COMMIT_B

Upvotes: 15

Ian MacDonald
Ian MacDonald

Reputation: 14030

git revert <Commit B>

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit). git docs


However, if you just want to see the difference between the two:

git diff <Commit B>..<Commit A>

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on disk. git docs

Upvotes: 0

Mohammad Dohadwala
Mohammad Dohadwala

Reputation: 746

Reverting Commit B will not delete Commit A. It will simply create commit C which will be undo everything you did in Commit B

git revert commit-b-hash

Upvotes: 0

Related Questions