Web Testing/Automation/CodeReviewProcess: Difference between revisions

 
(10 intermediate revisions by the same user not shown)
Line 56: Line 56:
=== Using interactive rebase to squash commits ===
=== Using interactive rebase to squash commits ===


You will use Git's interactive rebase feature to take all of the commits that are currently in the branch and ''squash'' them into a single commit.  
You will use git's interactive rebase feature to take all of the commits that are currently in the branch and ''squash'' them into a single commit.  


''Note:'' If all the commits are ones that were added only to the feature branch then you should not run into any issues. If your feature branch includes commits from master that were pulled in during development of the feature, you could encounter conflicts when doing the interactive rebase which you'll need to address. In this case it might be simpler to go with the second option ''merge with --squash''.
''Note:'' If all the commits are ones that were added only to the feature branch then you should not run into any issues. If your feature branch includes commits from master that were pulled in during development of the feature, you could encounter conflicts when doing the interactive rebase which you'll need to address. In this case it might be simpler to go with the second option [[Web_Testing/Automation/CodeReviewProcess#Using_merge_with_--squash_to_squash_commits |''merge with --squash'']].


# Determine how many commits need to be combined. You can do this by typing the
Complete the following steps, which should be done from the feature branch:
 
* Start up an interactive rebase session to squash all of the commits in the feature branch that are not currently in the ''master'' branch:
** <code>git rebase -i master</code>
* This will pop open a code editor (whichever one is configured to be used with git) with a file that includes some rebasing instructions. The file will look something like this:
[[File:Git-rebase-1.jpg]]
* This is convenient as git provides you with some instructions. In this example, you want to combine the second and third commit with the first, so you would edit the file to look like this:
[[File:Git-rebase-2.jpg]]
* Note that you can just type an <code>s</code> instead of the whole word <code>squash</code>.
* When you save and close the file git will pop open another file in the editor to edit the commit message:
[[File:Git-rebase-3.jpg]]
* From here you can either choose to accept the three individual commit messages that were picked up from the three commits, or you can remove them (or comment them out) and provide your own commit message. When you save and close this file you'll be back at the command line with a message similar to this:
** <code>Successfully rebased and updated refs/heads/my_branch.</code>
 
* If you have done this for your own pull request you can now push the new commit to your branch, forcing the new commit to overwrite any previous commits:
** <code>git push -f origin my_branch</code>
* If you are doing this on behalf of someone else, in order to squash their commits before merging, you can merge this new branch into your local master branch and then push the master branch:
** <code>git checkout master</code>
** <code>git pull upstream master</code>
** <code>git merge my_branch</code>
** <code>git push upstream master</code>
 
=== Using merge with --squash to squash commits ===
 
This method is a bit simpler than the above, but it requires an extra step which can be prone to user error. This is because when you do a ''merge with --squash'', you lose the author information, so you need to know what those values need to be and then update them afterwards. Here's an example.
 
If we have a feature branch called <code>my_branch</code> and want to merge it into master and squash the commits, we would do the following:
* Switch to the <code>master</code> branch: <code>git checkout master</code>
* Merge the feature branch: <code>git merge my_branch --squash</code>
* This will bring the changes in, but not commit them, so you need to do that: <code>git commit</code>
** This will open your editor to specify the commit message, and it will be pre-populated with information from each commit in the feature branch. You can use some of these commit messages, or you can create your own. Make a note of the ''author'' specified in this file as you will need to use that value to update the author.
* After you save the file you will be returned to the command line and the new commit will be added to the repo.
* Now you need to update the author of this new commit to give ownership back to the original author (because you will currently be recorded as the author): <code>git commit --amend --author='exact_author_from_the_previous_commits'</code>
** This will open your code editor one last time, which will allow you to edit the commit message again, but you won't need to change it as you will have already edited it in the step before, so just save and close the file.
* As you are likely doing this in order to squash commits on behalf of someone else before merging, you can now push your local master branch to the main repo in order to merge the changes: <code>git push upstream master</code>
Confirmed users
425

edits