0

I am new to git, and have created a local branch by following git checkout -b child syntax, then I went on and merged someone else's branch using git merge origin/parent. Note: I did not push my local branch i.e. child up yet.

  1. git status shown nothing to commit, working tree clean - which is confusing, my limited knowledge may be the cause Update - I learned that git merge commits the code too, I should have probably used git merge --no-commit

  2. I want to undo my merge to start afresh - How do I do it?

Disclaimer There are tons of git related questions out there, which maybe tangentially related to the above questions, but please realize that for a new guy it is quite overwhelming, internet is boon and bane.

EDIT - 1 I am also reading whether I should have performed git merge first place, or should have used something like git fetch

EDIT - 2 git reset HEAD~x is NOT what I am looking for, I just want to un-do the whole git merge

Vivek Shukla
  • 767
  • 6
  • 21
  • 1
    My favourite resource for fixing git issues: https://sethrobertson.github.io/GitFixUm/fixup.html – DaveyDaveDave Oct 06 '20 at 20:32
  • 1
    Does this answer your question? (Note that a merge is just a commit, as you'll see if you run `git log`) [How do I undo the most recent local commits in Git?](https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git) – DaveyDaveDave Oct 06 '20 at 20:34
  • thank you @DaveyDaveDave how do I undo a whole merge, `git reset HEAD~` only took me one commit back – Vivek Shukla Oct 06 '20 at 20:40
  • `git reset --hard` doesn't work either @DaveyDaveDave – Vivek Shukla Oct 06 '20 at 20:43
  • you can use `git reset HEAD~x` where `x` is the number of commits before the last you had. Ex. `git reset HEAD~3` in case your merge is counting and you had 2 commits in this new branch – ignacio Oct 06 '20 at 20:45
  • @ignacio my point is I just merged somebody else's branch to my local, do I need to really learn what they have committed, I just want to undo git merge, and start with a clean slate. – Vivek Shukla Oct 06 '20 at 20:47
  • I am also reading whether I should have performed git merge first place, or should have used something like `git fetch` – Vivek Shukla Oct 06 '20 at 20:48
  • `git reset` almost certainly *is* what you are looking for here. What to reset *to* depends on whether `git pull` actually did a real merge, or whether it did a fast-forward instead of a merge. – torek Oct 06 '20 at 21:34
  • `git reset --hard` will throw away uncommited changes, but `git merge origin/` commits the code too. Is there another variant of `git reset` which I should look for, @torek? – Vivek Shukla Oct 07 '20 at 00:43
  • If the merge that `git pull` ran was a true merge, you got a new merge commit, and you would want to reset to `HEAD~1`. If the merge that `git pull` ran was a fast-forward, you'd want to reset to the commit that was in place before the fast-forward operation (which is almost, but not quite, a simple checkout). If you had work that you had not committed, a true merge would not be possible in general and at least some if not all fast-forward cases would be rejected so there wouldn't be anything to do. Are you sure you had uncommitted work? – torek Oct 07 '20 at 09:25
  • Note that you can do a soft or mixed reset: typically to get back to the old commit, you'd do a hard reset, but if you had uncommitted work, you would probably want soft or mixed instead. If you got a fast-forward that preserved uncommitted work, you'll want one of those followed by individual checkout operations, which is messy and painful and not something I like to do, which is why I'm careful not to use `git pull` at all. :-) – torek Oct 07 '20 at 09:27

1 Answers1

0

If you want to undo merge action, you can follow this :

  1. git reflog, this will show you every event you have done in git
  2. copy <hash-id> commit action bellow your merge action in there
  3. paste that <hash-id> into this command git reset --hard <hash-id>, this will reset git HEAD.
Andy Winarko
  • 645
  • 1
  • 9
  • 20