moey
moey

Reputation: 10907

How to Temporarily Remove the Last Commit with Git?

Suppose I have the following git commits:

  1. aaaaaa
  2. bbbbbb
  3. cccccc (the latest)

I would like to go back to bbbbbb and continue the development from there. Essentially, I want to remove cccccc for now and be able to retrieve it in the future (I know I'd still need the cccccc). Should I revert cccccc or rollback to bbbbbb?

Upvotes: 10

Views: 7281

Answers (3)

GoZoner
GoZoner

Reputation: 70235

Just do:

git checkout -b dev_on_b bbbbbbb

which will leave the branch with cccccc alone and give you a new branch starting from bbbbbb. When you want to return to using ccccc you can do either of:

git merge branch_with_cccccc          # bring cccccc into dev_on_b

or

git checkout branch_with_ccccc

Upvotes: 9

manojlds
manojlds

Reputation: 301547

Just do

git branch for_later
git reset --hard bbbbbb

Of course, don't do this if cccccc is already pushed. In that case, use revert.

Upvotes: 8

James Black
James Black

Reputation: 41848

The correct solution depends on whether this is a public git repository or not.

If you use this command, git revert HEAD^ then it will revert to (2), which may be the correct thing.

But, you will need to make certain that you revert back to (3) when you do a (4) and then merge in the changes when you commit again.

You may want to look at this, as it will discuss different options, based on different situations: http://book.git-scm.com/4_undoing_in_git_-_reset,_checkout_and_revert.html

Upvotes: 1

Related Questions