6

I forked some repo to my profile and then made changes and pushed the changes to my profile under branch name dev_branch. Using this branch, I raised a pull request to source repo's staging branch. The owner now wants some changes to be done before merging. How should I update the pull request to incorporate the requested changes?

Exactly same question asked here : How to update a pull request from forked repo?. But this didn't work for me.

When I try to push to my personal remote repo, I get the following error :

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I tried doing a fresh clone and then applying the changes, did git commit --amend and then tried to push git push -u origin dev_branch. Still it gives the same error.

I tried this gist as well : https://gist.github.com/lolindrath/4454638 . Still same error.

What the hell is wrong? Do github really support --amend?

Note : I don't have commit-id in my message, if that matters.

Naveen
  • 7,944
  • 12
  • 78
  • 165
  • I think the message clearly gives you an idea of how to proceed. You should pull first and then push. Make necessary changes that is required and then stage it as a new commit. – Anoop Toffy Jun 22 '17 at 06:27
  • Don't try to amend if it is not really needed. – Anoop Toffy Jun 22 '17 at 06:27
  • @AnoopToffy : I have tried pull as well. Think logically, if its a fresh clone, why it needs a pull again. I think `git commit --amend` changes commit id and hence a difference arises between local and remote. Hence the problem – Naveen Jun 22 '17 at 06:35
  • just make new commits on top of `dev_branch`. This will allow your pull-request branch to have a history for you and your reviewer. When you want to merge the pull request you have two options: 1) push the commits as they are in the history of the target branch; 2) squash the commits into one and just then push the commits to the target branch to have a single corresponding commit in the target branch history. Also note that changing the contents of the commit (also the message) requires you to amend that commit, but when pushing, you need to alter the history, thus having to use `--force`. – andreim Jun 22 '17 at 06:36
  • 1
    this is written also in the link added by @AnoopToffy. – andreim Jun 22 '17 at 06:37

1 Answers1

2

You can make new commits, but you should rebase your branch on top of upstream/master anyway: that way, you make sure your branch is still working on top of the latest from the original repo:

git remote add upstream /url/original/repo
git fetch upstream
git rebase upstream/master

git push --force

You don't have to hesitate to force push: that will update the existing pull request accordingly. See more at "Git - When to use force push".


Think logically, if its a fresh clone, why it needs a pull again.

You don't: you don't need to pull from your fork ('origin').
You need to fetch (not pull) from the original repo (the one you have forked) because by the time you have added your new commits and need to add new ones, the original repo might have evolved on its own.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250