going back to previous commit without destroying the current one

So I wanted to go back to my previous commit without destryoing the latest one I have now. How do I do this? Doing a git reset HARD will destroy my current commit right?

  • error: unpack failed: error Object too large (1,957,642 bytes), rejecting the pack. Max object size limit is 1,048,576 bytes
  • list all files having a git attribute set
  • undo git update-index --assume-unchanged <file>
  • Prevent Git from Deleting Ignored Files
  • Migrating virtualenv and Github between computers
  • What is GIT_WORK_TREE, why have I never needed to set this ENV var, why now?
  • Fetch/Pull Part of Very Large Repository?
  • How to remove subversion remote in git?
  • Git: access a remote repository over ssh using a key file but without using ~/.ssh/config
  • Delete all local git branches
  • Git: List git branches, sort by (and show) date
  • How do I change my default browser in git's BASH shell?
  • 3 Solutions collect form web for “going back to previous commit without destroying the current one”

    Assuming you don’t have any changes that you have not checked in, git checkout $OLD_COMMIT will take you there, and git checkout $BRANCH will take you back.

    That will put you, potentially, on a detached head; see git checkout --help for the details, but if all you want is to run tests on the old version or something that should be fine.

    You might also be interested in:
    git stash – to store away uncommitted changes temporarily
    git show $SHA1 to show the changes of the old commit as a diff
    git show ${REF}:${PATH} to show the content of $PATH in $REF

    (Ref can be a sha, or a branch, or whatever, in the last git show invocation.)

    Use git revert. This allows you to undo a specific commit without upsetting commits since.

    If I understand your case correctly, you have:

    * abc123de a_good_commit
    * bc123def a_bad_commit
    * c123def4 another_good_commit

    You can either git revert HEAD^ or use a specific hash: git revert bc123def.

    Doing any kind of reset to a previous commit will remove your later commits from your branch such that they’re no longer part of it. A commit being the items you see when you do git log.

    This doesn’t mean those commits are instantly destroyed, but as your intention isn’t to get rid of them, you’d probably want to create another branch and use that to look at your previous commit so that your original branch is left with the latest commits intact. (Keywords for creating a new branch are: git branch <name> or git checkout -b <name>. **)

    Hard vs soft reset has more to do with what happens to the files in your working tree, and more importantly to uncommited changes. In essense, the only things destroyed when you specify --hard are your uncommited changes. As long as you’ve left a branch at your latest commit (or you remember the commit id), you can always recover the files by switching back to that branch or commit. (i.e. git checkout)

    ** You can actually look around without creating an extra branch and Daniel Pittman’s answer describes that.

    Git Baby is a git and github fan, let's start git clone.