Return to previous commit without breaking current

So, I wanted to return to my previous team without destroying the last one I have. How can I do it? Doing a git reset HARD will destroy my current commit correctly?

+6
source share
3 answers

Assuming you don’t have any changes that you don’t git checkout $OLD_COMMIT , git checkout $OLD_COMMIT will bring you there, and git checkout $BRANCH will return you.

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

You may also be interested in: git stash - temporarily save uncommitted changes to git show $SHA1 to show old commit changes as diff git show ${REF}:${PATH} to show the contents of $ PATH in $ REF

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

+13
source

Use git revert . This allows you to undo a specific commit without breaking the commit since then.

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 .

+1
source

Performing any reset for the previous commit will remove your subsequent commits from your branch so that they are no longer part of it. The end is the elements that you see when you do git log .

This does not mean that those commits are destroyed immediately, but since you do not intend to get rid of them, you probably want to create another branch and use it to view your previous commit so that your original branch remains unchanged. (Keywords for creating a new branch: git branch <name> or git checkout -b <name> . **)

Hard and soft reset has more in common with what happens to files in the working tree, and, more importantly, unmanaged changes. In essence, the only things that were destroyed when specifying --hard were your unstated changes. As long as you leave the branch in your last commit (or you remember the commit identifier), you can always restore files by going to this branch or commit. (i.e. git checkout )

** You can look around without creating an additional branch, and Daniel Pittman's answer describes this.

0
source

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


All Articles