user1955184
user1955184

Reputation: 599

Sending pull request for different issues-Github

I have forked a repository, cloned it to get a local copy to work on two issues.Let's call them Issue 1 and Issue 2 . Firstly,I applied three commits which addresses the problem faced in Issue 1. Then I did git push origin master to push the changes from my local master branch to origin/master. After this I sent a pull request to fix Issue 1. So Issue 1's pull request has three commits attached to it.

After that to address Issue 2, I created another branch called upstream via git checkout -b upstream upstream/master (I believe this adds a remote upstream (which is the same url as that of origin) and gives me a local branch upstream which starts off from upstream/master).

Now I make two commits to address Issue 2, cherry-pick the two commits with git cherry-pick <SHA> (which just gave me 'nothing to commit' message). After this I did git push origin upstream to populate the changes in my local upstream branch with that in origin. When I sent a new pull request to Issue 2, the three commits of Issue 1 got attached with the two commits I made to address Issue 2. Messing everything up.

(Later I also made a 4th commit to origin/master to address a problem in Issue 1 which implies the pull request to Issue 1 ,now has 4-commits attached to it.)

Then locally I made a commit to upstream branch addressing Issue 2. What do I want now ? I want to be able to push this commit and update this commit to Issue 2's pull request ALONE and I want to separate the 4-commits involved in Issue 1 from the 3-commits involved in Issue 2.

Everything is a bit messed up as of now. I am new to git and github,can someone please help me with this ?

Upvotes: 1

Views: 172

Answers (1)

VonC
VonC

Reputation: 1323203

Everything is a bit messed up as of now.

That is because the idea of making pull request from your fork is always to make them from a dedicated branch, never from master (or any other branch which can evolve on the upstream original repo)

Plus, you don't create a branch name upstream, you add a remote named upstream referencing the first repo that you have forked, in order to track upstream/master (the master from the original repo).

See "Pull new updates from original Github repository into forked Github repository".

enter image description here

At any moment you would then be able to do:

git checkout master
git pull upstream master

git checkout issue1
git rebase master
git push -f origin

git checkout issue2
git rebase master
git push -f origin

If you had pull requests done for issue1 and issue2, the push -f (which rewrites the commits of those branches) would trigger an automatic update of your pull request, without you having to do anything.

See more at "Couple tips on pull-requests".


I suspect your situation is this

     (master)
--o--o--o--x--x--x (issue1)
                  \
                   y--y--y  (upstream)

First: rename your "upstream" branch:

 git branch -m upstream issue2

Delete your remote upstream branch named "upstream" (that will cancel the associated pull request, and you will remake a pull request for that issue once we have fixed the branch locally)

 git push :upstream

Now, make sure you have master in sync with the master from upstream (that is, from the original repo you have forked).
Make sure you don't have work in progress (the reset --hard part would erase it)

# url of the original repo
git remote add upstream https://github.com/user/repo

git fetch upstream
git checkout master
git reset --hard upstream/master

That will make your repo looks like:

          O--O (master, upstream/master)
         /
--o--o--o--x--x--x (issue1)
                  \
                   y--y--y  (issue2)

Now you need to rebase your two issue branches on top of master, starting with issue2, in order to separate it from issue1:

 git checkout issue2
 git rebase --onto master issue1 issue2

Then issue1:

git issue1
git rebase master

You get:

                x--x--x (issue1)
               /
--o--o--o--O--O  (master, upstream/master)
               \
                y--y--y  (issue2)

Now, you can push those two branches:

git push -f -u origin issue1
git push -f -u origin issue2

And make pull requests from there.

Upvotes: 1

Related Questions