Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] DOC: Added rebasing and squashing section to contributing.rst #6176

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
162 changes: 162 additions & 0 deletions doc/developers/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,168 @@ and Cython optimizations.
<http://astropy.readthedocs.org/en/latest/development/workflow/development_workflow.html>`_
sections.

Rebasing and squashing commits
------------------------------

Squashing commits
^^^^^^^^^^^^^^^^^

To keep the history clean and to help backtrack regressions to a single commit we
prefer less (but meaningful) number of commits.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd reword this to we prefer to have less commits that are more meaningful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm I'm not quite sure about this.... It still retains the same meaning but makes it unnecessarily verbose. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think it should be we prefer less (but more meaningful) commits.

Please try to avoid merging master into a feature / bugfix branch as it makes it hard
to rebase and squash intermediate commits later. Ideally it would be great to start
branch again off of current master, ``git cherry-pick`` the commits that are needed
from ``my-feature-branch``, and then ``push -f`` the result of that operation to
``my-feature-branch``. Squashing commits is done as follows:

1. Rename your current feature branch in case something goes wrong::

$ git branch -m my-feature-branch my-feature-branch-bak

2. Update local master::

$ git checkout master
$ git pull upstream master

3. Delete the ``my-feature-branch``. The code will still be under the branch
``my-feature-branch-bak`` in case you need it::

$ git branch -D my-feature-branch

4. Create a new feature branch (could be named ``my-feature-branch``) and cherry pick
meaningful commits::

$ git checkout -b new-feature-branch
$ git cherry pick *commit-1 id*
$ git cherry pick *commit-2 id*
.
.
.

5. Force push into your PR branch::

$ git push origin new-feature-branch -f

Squashing via Interactive Rebasing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Another way to squash commits is through interactive rebasing which is one the most powerful
features of ``rebase``. If you want to squash the latest 3 commits together (this number can
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use $ git log --oneline to see past commits?

be found out by typing ``$ git log --oneline``):

1. Rewind ``HEAD`` to the parent of the last commit you want to edit::

$ git rebase -i HEAD~3

2. This will open an editor that might look something like this::

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice!


3. Change ``pick`` to ``fixup`` or ``f`` for the second and third commit like::

pick f7f3f6d changed my name a bit
f 310154e updated README formatting and added blame
f a5f4a0d added cat-file

4. Save and quit the editor. This will immediately popup an editor for editing the
commit message as you're squashing some commits together and might want to change
the original commit message.

5. After saving and quiting you will see a message like::

Successfully rebased and updated refs/heads/my-feature-branch.

6. Once successfully rebased, force push onto your PR branch by::

$ git push -u -f origin my-feature-branch


.. note::

Detailed guides on **interactive rebasing** can be found:

* In the *Scipy Development Workflow* in the `rebasing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinions but I think we could copy those here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'll copy the scipy interactive rebasing tut here and give a link for the other two.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually just added the squashing part of that tutorial so thought of keeping this link here. I can copy the rest of it too if you want.

<http://docs.scipy.org/doc/numpy/dev/gitwash/development_workflow.html#rebasing-on-master>`_
and `squashing
<http://docs.scipy.org/doc/numpy/dev/gitwash/development_workflow.html#
rewriting-commit-history>`_ sections

* On `gitready.com
<http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html>`_.

* On `atlassian.com
<https://www.atlassian.com/git/tutorials/merging-vs-rebasing/conceptual-overview>`_.

Rebasing
^^^^^^^^

You should generally use ``rebase`` over ``merge`` to integrate changes from one branch into
another branch. Merging creates a "merge commit" every time you incorporate upstream changes.
This pollutes the feature branch's history and makes it hard for other developers to understand
the history of the project. On the other hand ``rebase`` effectively rewrites the project history
by creating brand new commits for each commit in the original branch. This creates a much cleaner
project history.
To rebase:

1. Update your master branch::

$ git checkout master
$ git pull upstream master
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like doing
git fetch upstream to get the scikit-learn/scikit-learn changes from master
git checkout master to switch to your master branch
git rebase upstream/master
is better because git pull creates merge commits, right?


2. Switch to feature branch::

$ git checkout my-feature-branch

3. Rebase on master::

$ git rebase master

.. note::

This can create merge conflicts and you might need to resolve them. If you encounter any other
problem or wish to undo any changes, you can use `git reflog
<https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog>`_. Detailed information on
this can be found `here
<https://git-scm.com/docs/git-reflog>`_.

Resolving merge conflicts
^^^^^^^^^^^^^^^^^^^^^^^^^

If you changed the same part of the same file differently in the two branches you’re merging
together, Git won’t be able to merge them cleanly. This is called a merge conflict. To resolve a
merge conflict you can either:

* Open the files manually and resolve them as git adds standard resolution markers to the
files that have conflicts.

* Use a graphical tool by running ``git mergetool``. This tool will walk you through all the
conflicts.

.. note::

A detailed guide on solving merge conflicts can be found on `git-scm.com
<http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging#Basic-Merge-Conflicts>`_

.. _easy_issues:

Easy Issues
Expand Down