Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Amit0617/31f80bfa4133a36f833194e20898be9b to your computer and use it in GitHub Desktop.
Save Amit0617/31f80bfa4133a36f833194e20898be9b to your computer and use it in GitHub Desktop.
Create separate pull request for every commit using simple example steps

One simple thing will be to before creating changes to file for next commit in separate pull request, checkout new branch tracking upstream repository branch against which you want to create your pull request.

But let say you have alreay committed the changes into the branch which might have already unaprroved pull requests. In thaaat case you have to do it little bit new way actually you have to cherry-pick the commit and put on a new branch. Then pull request against that other repository will be automatically creating new pull request against same original branch of that repository. So how do we do that? first you will need commit hash of that commit which you want to put on top of new branch. To get commit hash of that particular commmit do git log in the branch where you commited the required changes. There you will see the hash, commit message, timings and other info which will help you identify the required commit. After copying that hash(long ascii string - sha1) you can quit using q

NOW let's come to the meat part. But before that make sure your upstream is that other repository(remote repository which you have forked) by git remote --verbose and if not then do git remote add upstream https://github.com/other-username/other-repo or git remote add upstream git@github.com:other-username/other-repo

git checkout -b new-branch-name --track upstream/branch-remote
git cherry-pick copied-hash

FOR EXTRA SECURITY- you can confirm your tracked branch using git status it should say something like

On branch develop4
Your branch is up to date with 'upstream/develop'.

where develop4 is the new local branch. upstream/develop shows that develop branch is against which we want to create pull request.

After this merge-conflict might arise then simply open the files and make edits to keep what you want and then close it. There might be

<<<<HEAD
few lines from previous state of the files
======
few your lines you might have entered by replacing them
>>>>commithash commit-message

Make sure to remove them too. Keep in mind the you have to make edits same way you want your files to look finally. You would surely like to delete tags(HEAD and commithash commit-message) and divider(=====) atleast and in most cases you would like to change the state of files from above condition to

few your lines you might have entered by replacing them

Close the editor.

git add -u

(assuming those files were already tracked mostly and you haven't created any new file to create PR and wants to propose some chages only).

git status

Now those files which you edited should be green(if you don't have colors, it would be under Changes to be commited:)(Why am i behaving like viewer have never used git before? Maybe I am trying to make it helpful for everyone). and then the normal drill

git commit -m "commit message"
git push -u origin new-branch-name

And then proceed for creating pull request from github with that branch.

If it was difficult to follow their is easier one. Let say you are on a branch which has the commits you wanted to create for new pull request. Or your fork is in following condition

---forked from other repository---
---created 1st commit---
---created 2nd commit---
---created 3rd commit---

and you want to create pull request with 3rd commit only. So do

git log

Copy the hash of 3rd commit (or simply the commit you want to create pull request with).
And checkout new branch with that commit.
HOW?

git checkout -b new-branch-name commit-hash

and then proceed with normal drill of creating pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment