Live data from Hacker News

Some bad Git situations and how I got myself out of them

ohshitgit.com

1–10 of 352 posts

Re: Some bad Git situations and how I got myself out of them

#7
Actually the easiest thing is simply not to care about how your log looks.

If you don't then there are ry only two things you need to know how to do:

If you didn't push to origin do an ammend. If you did, revert soft and commit the previous code to revert it (you can also put a stash or patch to apply it back).

Which frankly is what the article does, basically.

Re: Some bad Git situations and how I got myself out of them

#8

The screwed up and committed to master should end with: git reset --hard origin/master (assuming that the remote is called "origin") With the example in the text, you have to know the number of commits you've made to master.

It was also a bit pointless to checkout a new branch just to switch back. The whole thing should be

    git branch some-new-branch-name
    git reset --hard origin/master
    git checkout some-new-branch-name

Re: Some bad Git situations and how I got myself out of them

#10
> Oh shit, I accidentally committed to the wrong branch!

Other ways to do it (that don't require to retype the commit message): - rebase onto the correct branch:

  git branch foo

  git reset --hard HEAD~

  git rebase --onto name-of-the-correct-branch HEAD foo

  git checkout name-of-the-correct-branch

  git merge foo

  git branch -D foo

- cherry-pick

  git reset --hard HEAD~

  git checkout name-of-the-correct-branch

  git cherry-pick name-of-the-branch-you-mistakenly-committed-to@{1} (or git cherry-pick HEAD@{2})

> Oh shit, I tried to run a diff but nothing happened?!

You probably want to know `git diff HEAD` too.

Edit: formatting.

Post reply on HN