Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 1
    Building on this, if the commits were already pushed to remote, git revert <commit> would be better in place of git reset --hard HEAD^ to "erase" the commit. Commented Jun 28, 2018 at 19:12
  • That's what I was looking for, but one thing, if commit which I wnat to remove isn't last one, can I use git reset --hard commitName to remove specific one?
    – pablocity
    Commented Jun 28, 2018 at 19:18
  • Question asked to move the commit; this will copy it. That can be corrected (either with more cherry-picking or a rebase), but it turns out to be overly complicated, considering that in this case the only thing "wrong" is the location of the branch refs, and the existing commits can be used as-is. See torek's answer. (And revert is nothing but cost in this case, since nothing has been pushed.) Commented Jun 28, 2018 at 19:35
  • @pablocity: No; that will reset to that commit, deleting everything after it. Use git rebase -i and delete the commit you want.
    – SLaks
    Commented Jun 28, 2018 at 19:42
  • This is all correct as well: if you do need to copy a commit (to change the graph's structure, instead of / in addition to changing which commit each name points-to), git cherry-pick will do it. Git books teach this stuff in what is in my opinion the wrong order: cherry-pick should come first, then rebase, because rebase is an automated series of cherry-picks. (And in all cases, the key to understanding any of it starts with the graph and how snapshots become change-sets.)
    – torek
    Commented Jun 28, 2018 at 22:05