1

I have a Git commit picture that looks like this:

(master)(origin/master)<---o
                           | \
                           |  \
                           |  |
                           |  |
                           |  o--->commit D
                           |  |
                           |  |
                           |  o--->commit C
                           |  |
                           |  |
               commit B<---o  |
                           |  |
                           | /
               commit A<---o/
                           |
                           |
                    earlier commits

I would like to turn it into:

(master)(origin/master)<---o--->commit B
                           |
                           |
                           o--->commit A
                           |
                           |
                    earlier commits

Can anyone help? I want to forfeit commits C and D. And can anyone tell me what is the better way to learn Git? It seems that I am often restrained by Git rather than being able to harness its power. Thanks.

Jonas
  • 534
  • 8
  • 16

1 Answers1

3

I want to forfeit commits C and D.

You just need to git reset --hard HEAD~ to move back to the previous commit. --hard will discard all changes and give you a clean working directory.

And can anyone tell me what is the better way to learn Git?

Keep using it. Experience is the best teacher.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Is there any number I should put after `HEAD~`? – Jonas Apr 09 '15 at 04:21
  • No, just `git reset --hard HEAD~` as I've typed it – user229044 Apr 09 '15 at 04:31
  • The local side looks good now after using your command. But how about the origin side? – Jonas Apr 09 '15 at 04:38
  • If you want to discard the commits from your remote, you need to `git push -f` after performing the reset. Note that anybody else can reintroduce those commits by pushing. You need to coordinate with everybody else to remove those commits, if other people are using the repository. – user229044 Apr 09 '15 at 04:39
  • Thx. It works. I have the feeling that learning Git using an on-demand cookbook recipe style is not going to help me understand Git much. Is there anything I can read to understand the idea/philosophy behind Git and its various commands? – Jonas Apr 09 '15 at 04:44