CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2020-02-21
by CodeWizard

Original Post

Original - Posted on 2010-11-06
by Cascabel



            
Present in both answers; Present only in the new answer; Present only in the old answer;

TL;DR use `git reflog` && `git reset`
---
###Full answer:
Before answering, let's add some background, explaining what this `HEAD` is.
***`First of all what is HEAD?`*** ==================================
`HEAD` is simply a reference to the current commit (latest) on the current branch. <br/> There can only be a single `HEAD` at any given time (excluding `git worktree`).
The content of `HEAD` is stored inside `.git/HEAD` and it contains the 40 bytes SHA-1 of the current commit.
--- ***`detached HEAD`*** === If you are not on the latest commit - meaning that `HEAD` is pointing to a prior commit in history it's called ***`detached HEAD`***.
[![Enter image description here][1]][1]
On the command line, it will look like this - SHA-1 instead of the branch name since the `HEAD` is not pointing to the tip of the current branch:
[![Enter image description here][2]][2]
[![Enter image description here][3]][3]
--- ###A few options on how to recover from a detached HEAD:
--- ###[`git checkout`][4]
git checkout <commit_id> git checkout -b <new branch> <commit_id> git checkout HEAD~X // x is the number of commits t go back
This will checkout new branch pointing to the desired commit. <br/> This command will checkout to a given commit. <br/> At this point, you can create a branch and start to work from this point on.
# Checkout a given commit. # Doing so will result in a `detached HEAD` which mean that the `HEAD` # is not pointing to the latest so you will need to checkout branch # in order to be able to update the code. git checkout <commit-id>
# Create a new branch forked to the given commit git checkout -b <branch name>
---
###[`git reflog`][5]
You can always use the `reflog` as well. <br/> `git reflog ` will display any change which updated the `HEAD` and checking out the desired reflog entry will set the `HEAD` back to this commit.
**Every time the HEAD is modified there will be a new entry in the `reflog`**
git reflog git checkout HEAD@{...}
This will get you back to your desired commit
[![Enter image description here][6]][6]
---
###***[`git reset --hard <commit_id>`][7]***
"Move" your HEAD back to the desired commit.
<!-- language: lang-sh -->
# This will destroy any local modifications. # Don't do it if you have uncommitted work you want to keep. git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep: git stash git reset --hard 0d1d7fc32 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: ([Since Git 2.7][8]) you can also use the `git rebase --no-autostash` as well.
---
###***[`git revert <sha-1>`][9]*** "Undo" the given commit or commit range. <br/> The reset command will "undo" any changes made in the given commit. <br/> A new commit with the undo patch will be committed while the original commit will remain in the history as well.
<!-- language: lang-sh -->
# Add a new commit with the undo of the original one. # The <sha-1> can be any commit(s) or commit range git revert <sha-1>
---
This schema illustrates which command does what. <br/> As you can see there, `reset && checkout` modify the `HEAD`.
[![Enter image description here][10]][10]
[1]: http://i.stack.imgur.com/OlavO.png [2]: https://i.stack.imgur.com/qplvo.png [3]: http://i.stack.imgur.com/U0l3s.png [4]: https://git-scm.com/docs/git-checkout [5]: https://git-scm.com/docs/git-reflog [6]: http://i.stack.imgur.com/atW9w.png [7]: https://git-scm.com/docs/git-reset [8]:https://github.com/git/git/blob/master/Documentation/RelNotes/2.7.0.txt [9]: https://git-scm.com/docs/git-revert [10]: http://i.stack.imgur.com/NuThL.png



This depends a lot on what you mean by "revert".
## Temporarily switch to a different commit
If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:
<!-- language: lang-sh -->
# This will detach your HEAD, that is, leave you with no branch checked out: git checkout 0d1d7fc32
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 0d1d7fc32
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, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:
<!-- language: lang-sh -->
# This will destroy any local modifications. # Don't do it if you have uncommitted work you want to keep. git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep: git stash git reset --hard 0d1d7fc32 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.
If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again.
## 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.
<!-- language: lang-sh -->
# 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. Be sure and write a good message describing what you just did git commit
The [`git-revert` manpage][1] actually covers a lot of this in its description. Another useful link is [this git-scm.com section discussing git-revert][2].
If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section).
You may also find this answer helpful in this case: https://stackoverflow.com/questions/34519665/how-to-move-head-forward-checkout-revet-reflog-reset/34519716#34519716
[1]: http://schacon.github.com/git/git-revert.html [2]: https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_undoing_merges

        
Present in both answers; Present only in the new answer; Present only in the old answer;