2

here is my situation.

1) I forked a project from someone else

2) I created a separate branch and edited some files on it

3) I created a Pull Request from that branch and Merged It to Master of mine.

4) Now my Master contains those commits.

5) I created a Second Branch. And Edited some files.

6) I then created a pull request for the 'Original' Project Repo.

7) The Owner of the Project asked me to REMOVE those commits I merged by myself, because that code is not needed.

Now, the problem is : I can't figure out how to delete commit from my separate branch. Can someone help me ???

Detailed view of my situation

CS Pei
  • 10,869
  • 1
  • 27
  • 46
Junwoo HWANG
  • 31
  • 1
  • 5
  • You might inform the owner you're not very familiar with Git and politely request they fix the problem. – Schwern Sep 15 '17 at 01:21

2 Answers2

2

Simplest thing to do is to rebase your new branch on origin/master.

You started with this.

A - B - C [master]
          [origin/master]

A, B, and C are existing commits. master is your local branch and origin/master tracks the last place you saw the upstream master branch. They both point at commit C.

After you merged your new branch into master, you had something like this.

          1 - 2 - 3 [master]
         /
A - B - C [origin/master]

The upstream remains at C, but your local master is now 3 commits past it. Your local master is contaminated with local commits.

After you made your second branch off master and committed to it, you had this.

                    4 - 5- 6 [second]
                   /
          1 - 2 - 3 [master]
         /
A - B - C [origin/master]

As you can see, second also contains 1, 2, and 3 -- your work from the first branch. To fix this, move 4, 5, and 6 to be on top of origin/master instead.

git rebase --onto origin/master master second

That says to move everything from (but not including) master to (and including) second onto origin/master. That is 4, 5, and 6. The result is this.

          1 - 2 - 3 [master]
         /
A - B - C [origin/master]
         \
          4' - 5' - 6' [second]

Note: this assumes 4, 5, and 6 do not rely on 1, 2, and 3. If they do, leave everything as it is and note the dependency in the pull request.

In general, when working with an upstream repository...

  1. Branch from origin/master.
  2. Do not merge nor commit anything to master.

This ensures your pull requests don't get contaminated with local changes or work from other pull requests.

Schwern
  • 153,029
  • 25
  • 195
  • 336
0

In your case, you may try to perform a

git reset --hard commit_hash

where commit_hash is the commit before the first commit to remove. You have now a master branch at its initial state, before the two merges. At this point, just merge again from your second branch and it should work

Another solution is to try:

git rebase -i commit_hash

where commit_hash is again the commit before the first commit to remove. In the text editor git opens for you, you need to remove the two lines of the two commit you want to remove. Save the file, close the editor and git will complete the rebase operation, removing the two commits.

WARNING

Both instructions will rewrite the git history and you need to perform a forced git push

git push --force

Yusef Maali
  • 2,201
  • 2
  • 23
  • 29