简体   繁体   中英

how to go back in git?

Here is how my git log looks like-

Nikhil@admin-PC MINGW32 ~/Desktop/facebook by nick/Addmie/nodejs (messageadded)
$ git log
commit fcf7a0bf45dffdf2da197f84b1b3e4c21715b5f1 (HEAD -> messageadded, origin/messageadded)
Author: codewithnick <nikhilsingh526452@gmail.com>
Date:   Fri Jun 19 00:02:46 2020 +0530

    i have added the message feature it is not live chat yet]

commit 1e5fb40e7deb0787cd8be83b28f39f5acc06b8bf (origin/profileadded, profileadded, ajaxadded)
Author: codewithnick <nikhilsingh526452@gmail.com>
Date:   Sat Jun 13 23:57:23 2020 +0530

    i have seprated profiles

commit 1f8440134b0147efd454b966ffc464ff5843f51e (origin/ajaxadded, exploreadded)
Author: codewithnick <nikhilsingh526452@gmail.com>
Date:   Sat Jun 6 00:37:30 2020 +0530

    6/6/2020 12:37

I made some errors in my code after the last commit I made in the message added branch. How do I delete all my changes in the working directory and go back to the time of past commit?

Git provides you quite some options to work through this

Temporarily switch to a different commit

If you want to temporarily go back to a particular commit. Just experiment around. All you have to do is check out the desired commit:

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout <hash>

Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:

git checkout -b old-state <hash>

To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.)

Hard delete unpublished commits

If you want to overwrite your changes strictly:

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard <hash>

# Alternatively, if there's work to keep:
git stash
git reset --hard <hash>
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

Note: This will reset your changes though.

Undo published commits with new commits

On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

# This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

#Similarly, you can revert a range of commits using commit hashes:
git revert a867b4af..0766c053 

# Reverting a merge commit
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit.
git commit

A useful link is this git-scm.com section discussing reverts.

to undo/revert the last commit you can do the following, using the commit hash that you get from the git log command:

git revert <commit hash>

There are few ways to do so. I will list two of them.

Git reset

git reset --hard <commit hash>

This will reset the repo to commit hash version.But now the remote origin still has HEAD point to commit D, if we directly use git push to push the changes, it will not update the remote repo, we need to add a -f option to force pushing the changes.

git push -f

Reset is flawed and is not a very good idea to use to.The second way is to use

Revert

git revert <commit hash>

This creates a new commit which reverts a previous commit. The HEAD will point to the new reverting commit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM