0

I have a pull request on an Open-Source repository with one commit e.g., commit a. Now someone requested me to change some code in that commit. If I open that branch in my Android Studio and change the requested code and then If I commit again, there will be two commits. I want to change the code and only have one commit in my pull request.

Things I have tried:

I tried resetting the HEAD to initial commit by git reset HEAD~2 --soft and then git commit -a -m "combined commit message" but then when I am pushing this branch to my remote repository then a pop-up appears which says "This branch has conflicts with the remote branch which need to be resolved" with options merge and rebase. I know what that means. It's because I am changing the same code which is there in my remote branch. So I want to rebase. Then there is a received commit a. Then I again run git reset HEAD~2 --soft and again git commit -a -m "I have received the commit and I am combining these two commits again" but when I again push it, the same pop up appears.

Any help? How to change the same code and still have one commit in that pull request?

Younes
  • 462
  • 7
  • 15

1 Answers1

2

After making your code changes, you can run a git commit --amend and then force push the changes to your fork/branch. The amend and force push will then take the changes but only show it as the same commit in your fork/branch.

Note: If using github, the changes will get picked up in the pull request (PR), but there will be a log stating that you force pushed in the PR (In my experience, maintainers of repo's that i've submitted PRs to have never really pointed it out or raised any concern about it).

git commit --amend

You don't need to make changes here. So just save it with the same message, unless you want to actually also change the commit message. Then force push:

git push -f

This will override the existing commit with your new changes (and new commit message if changed in the previous step)

Note: The standard and widely popular git force pushing PSA (public safety announcement) applies - use force push with care. But if you've tested your code and it works, you should be fine.

m_vemuri
  • 672
  • 3
  • 10