0

So the master branch of my upstream repo is in bit of a mess.

Commits on upstream branch looks like below.

A - Latest commit
|
B
|
C - Bad commit
|
D - Commit I want the upstream master branch to be at.

on my forked branch I can do a git reset --hard D. But how can I apply these changes back to the upstream master branch. While creating a PR(Pull Request) from forked branch to upstream branch there are no changes shown.

NOTE: Direct commits on upstream branch is prohibited, it has to be done using a Pull Request from the Fork.

EDIT This is how i have cloned the remote repository.

git clone http://myrepo.git
git remote add upstream http://main-repo.git

Now origin and upstream are like below.

origin - http://myrepo.git
upstream - http://main-repo.git

Forked repository is basically a mirror clone of the remote repository. On the bit bucket UI there is an option to create a fork and enable fork syncing between origin & upstream.

When I say forked branch it means that the branch which is on the forked repository.

Mohammed Aamir K
  • 311
  • 5
  • 17
  • What do you mean by upstream repository? Do you have permission to force push? If the upstream repository is not yours, then why you would like to reset other's repository? – Chuck Lu Oct 13 '20 at 05:44
  • @ChuckLu - ````origin```` would be my forked repository and ````upstream```` is the remote repository. – Mohammed Aamir K Oct 13 '20 at 06:03
  • @ChuckLu And no permission to force push on upstream repo. – Mohammed Aamir K Oct 13 '20 at 06:11
  • Still confused about your description, what do you mean by forked branch? It's the first time I hear it, I just know the forked repository concept on github. – Chuck Lu Oct 13 '20 at 06:12
  • Actually for the concept of repository, for example user1 have a repository1 on github,like user1/repository1. User2 forked the repository1 of user1, and user2 will get a forked repository on github user2/repository1. User2 can clone the forked user2/repository1 to local folder repository1. Here, the user1/repository1 should be the upstream repository, and user2/repository1 is a simple remote repository. Now you can make it clear in your description, which remote repository you want to change(rewrite history)? – Chuck Lu Oct 13 '20 at 06:22
  • @ChuckLu - Edited the question explaining the forked branch. – Mohammed Aamir K Oct 13 '20 at 06:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222939/discussion-between-chuck-lu-and-mohammed-aamir-k). – Chuck Lu Oct 13 '20 at 06:41

2 Answers2

2

If changes need to be done by pull request, you cannot remove commits.

Instead, you will want to revert; this creates a new commit, which reverses the effects of the bad one. Both changes will remain part of history.

Create a new branch as normal, then run the command: git revert hash_of_C (where you substitute the hash of commit C). Review the change, fine-tune the commit message with git commit --amend if needed, then push for review as normal.

If you absolutely need to remove the commit, you'll need to discuss that with the project lead. Editing history that's already part of upstream master is a major disruption to the whole team, most likely you'll be told to revert instead. If the project lead agrees that commit C must not be part of history, they'll probably remove it themselves, most likely by temporarily giving themselves permission to push directly to master. The whole team will need to be alerted as well.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • Agreed with your point of reverting a single commit.````git revert -m has_of_C```` works as it was a merge commit. But I was thinking on the lines of reverting all commits ie C,B,A so that the upstream repository is clean and at commit D. Could something like this be achieved. In my scenario right now between A and D there are around 32 commits which include merges too and the direct commits are just package updates. So there is no objection in reverting all the commits. – Mohammed Aamir K Oct 13 '20 at 06:18
  • That would require force-pushing to the already-published master branch. Only the project lead can approve that. – Jiří Baum Oct 13 '20 at 06:50
2

The operation you want is a git push with --force, and since you do not have permission to do this.
Then the alternative operation could a revert operation, and there is an elegant solution for you to revert multi commits.
How to revert multiple git commits?

Chuck Lu
  • 173
  • 12