Custom Search

Tuesday, May 26, 2015

How to Rebase a Pull Request


https://github.com/edx/edx-platform/wiki/How-to-Rebase-a-Pull-Request

http://eli.thegreenplace.net/2014/02/19/squashing-github-pull-requests-into-a-single-commit

1)
#cd puppet-rjil

#git branch
* contrail_quota
  master

2)
#git remote -v
origin    https://github.com/sajuptpm/puppet-rjil.git (fetch)
origin    https://github.com/sajuptpm/puppet-rjil.git (push)

3)
Add official remote


a)
#git remote add official_remote https://github.com/JioCloud/puppet-rjil.git

b)
#git remote -v
official_remote    https://github.com/JioCloud/puppet-rjil.git (fetch)
official_remote    https://github.com/JioCloud/puppet-rjil.git (push)
origin    https://github.com/sajuptpm/puppet-rjil.git (fetch)
origin    https://github.com/sajuptpm/puppet-rjil.git (push)

4)
Fetch the latest version of master


a)
#git fetch official_remote

remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/JioCloud/puppet-rjil
 * [new branch]      master     -> official_remote/master
 * [new branch]      qaenv      -> official_remote/qaenv
 * [new branch]      revert-587-add-keys-yatin -> official_remote/revert-587-add-keys-yatin
 * [new branch]      testenv    -> official_remote/testenv

b)
#git branch
* contrail_quota
  master

5)
Squash your changes.


This step is not always necessary, but is required when your commit history
is full of small, unimportant commits (such as "Fix pep8", "Add tests").
It involves taking all the commits you've made on your branch, and squashing
them all into one, larger commit. Doing this makes it easier for you to resolve
conflicts while performing the rebase, and easier for us to review your pull request.

a)
First, find the commit (commit id) that is base of your branch using the command "git merge-base".
That command will return a commit hash (id).
Or 
you can find the commit id using "#git log -n 10" command.

#git merge-base master
#git merge-base contrail_quota master
cf390dff46cd1492e4fe5cfc932975c026133444

b)
If your merge base commit hash (id) is abc123, you would run "$ git rebase --interactive abc123".
Your text editor will open with a file that lists all the commits in your branch,
and in front of each commit is the word "pick". It looks something like this:

#git rebase --interactive cf390dff46cd1492e4fe5cfc932975c026133444

----

pick 0754175 set default quota in contrail
pick a354793 temp change for testing
pick d13540f updated quota values for testing
pick 3213161 added saju's ssh pub key
pick f4e5c53 updated quota value for testing

c)
Then arrange it based on your requirement.
You want to replace the word "pick" with the word "squash" or "s".
I want to make it as two commits.

commit1 = 0754175 + d13540f + f4e5c53
commit2 = 3213161 + a354793

So I arranged it like this:

----

pick 0754175 set default quota in contrail
squash d13540f updated quota values for testing
squash f4e5c53 updated quota value for testing

pick 3213161 added saju's ssh pub key
squash a354793 temp change for testing

-------

Save and close the file, and a moment later a new file should pop up in your editor,
combining all the commit messages of all the commits. Reword (change) this commit
message as you want, and then save and close that file as well.
This commit message will be the commit message for the one, big commit that you
are squashing all of your larger commits into. Once you've saved and closed that
file, your commits have been squashed together, and you're done with this step.

6)
How to reword/change commit message if you want

To reword/change commit message, use the command "reword".
For example you might perform an interactive rebase and replace this:

a)
#git rebase --interactive cf390dff46cd1492e4fe5cfc932975c026133444

It looks something like this:

pick 3581104 set default quota in contrail
pick cc43285 added saju's ssh pub key

b)
You want to replace the word "pick" of commit you want to change message with the word "reword".
It looks something like this:

reword 3581104 set default quota in contrail
pick cc43285 added saju's ssh pub key

-----

Save and close the file, and a moment later a new file should pop up in your editor,
showing you the current wording of the commit message.
Reword this commit message as you want, and then save and close that file as well.
This commit message will be the new commit message for your commit,

7)
Perform the rebase


a)
Note the id of commit which comes just before your first commit in this branch.
#git log -n 4
 

b)
#git branch
* contrail_quota
  master

b)
#git rebase official_remote/master
First, rewinding head to replay your work on top of it...
Applying: set default quota in contrail
Applying: added saju's ssh pub key

c)
Again, Note the id of commit which comes just before your first commit in this branch.
There should be some differents if there was some new commits in the master of official repo.
#git log -n 4

----
Git will start replaying your commits onto the latest version of master.
You may get conflicts while doing so: if you do, git will pause and ask you to
resolve the conflicts before continuing. This is exactly like resolving conflicts
with a merge: you can use git status to see which files are in conflict, edit
the files to resolve the conflicts, and then use git add to indicate that the
conflicts have been resolved. However, instead of running git commit,
you instead want to run git rebase --continue to indicate to git that it should
continue replaying your commits. If you've squashed your commits before doing
this step, you'll only have to pause to resolve conflicts once -- if you didn't
squash, you may have to resolve your conflicts multiple times.

8)
Force-push to update your pull request


As explained above, when you do a rebase, you are changing the history on your
branch. As a result, if you try to do a normal git push after a rebase, git will
reject it because there isn't a direct path from the commit on the server to the
commit on your branch. Instead, you'll need to use the -f or --force flag to tell
git that yes, you really know what you're doing. When doing force pushes,
it is highly recommended that you set your push.default config setting to simple,
which is the default in Git 2.0. To make sure that your config is correct, run:

#git config --global push.default simple

Once it's correct, you can just run:

#git push -f
origin contrail_quota


And check your pull request. It should be updated!



No comments:

Post a Comment