Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rebase and keep commits in chronological order?

Tags:

git

git-rebase

I would like to rebase a branch on the master branch but in such a way that all commits show up in chronological order in the git log. Is this possible without git rebase --interactive and rearranging the commits manually?

Background: I am using git for keeping track of the puppet configuration of a server farm. The master branch is always in a known good state, so that all existing servers can retrieve their configuration from the puppet master server.

Each new server gets its own branch, so whenever I work on the configuration of a new server, such as changing a domain name, configuring an SSL certificate I check out its branch and commit all configurations.

After the new configuration is completed, I rebase the changes onto the master branch:

# git checkout new_config
Switched to branch 'new_config'
# git rebase master
First, rewinding head to replay your work on top of it...
Applying: fix routing rules
Applying: fix netmask
Applying: configure new ip address
# git checkout master
Switched to branch 'master'
# git merge new_config
Updating 21a3120..b0b79d7
Fast-forward
 files/custom/xxxx |   45 +++++++++++++++++++++++++++++++++++++++++++++
 files/custom/yyyy |   38 --------------------------------------
 manifests/site.pp |    6 +++---
 3 files changed, 48 insertions(+), 41 deletions(-)
#

The new commits are now on top of the log but they have their original (past) dates. It looks like this:

commit b0b79d7924ec97e367664ccc26aaf0021916a30d
Author:
Date:   Sun Jul 12 17:14:41 2015 +0200

    configure new ip address

commit f60d00abd57d6b8582f49bf1322efb88d44ee86e
Author:
Date:   Fri Jul 10 13:19:45 2015 +0200

    fix netmask

commit 6eaae6c328faf55e5725f65a947bbb23ea29b166
Author:
Date:   Fri Jun 12 14:05:25 2015 +0200

    fix routing rules

commit 21a31200e6694c640b2cb526d773af11cd703ff1
Author:
Date:   Wed Jul 15 15:08:41 2015 +0200

    (most recent commit on master before rebase)

commit a7fa9cfa9c317fbbeb7dac8a89009c7d935fdd11
Author:
Date:   Wed Jul 15 11:56:59 2015 +0200

    (second most recent commit on master)

Note how the old timestamps of the branch commits show up at the top of the log, instead of being merged further down in the log according to their commit dates. I have to run another git rebase --interactive to rearrange the commits so that my log file shows all commits in chronological order.

like image 621
nn4l Avatar asked Jul 18 '15 14:07

nn4l


1 Answers

Your question is a bit underspecified, because git log always sorts its output, but it takes options telling it how to sort:

Commit Ordering

By default, the commits are shown in reverse chronological order.

--date-order

    Show no parents before all of its children are shown, but otherwise show commits in the commit timestamp order.

--author-date-order

    Show no parents before all of its children are shown, but otherwise show commits in the author timestamp order.

--topo-order

    Show no parents before all of its children are shown, and avoid showing commits on multiple lines of history intermixed.

Hence, with no options, git log shows commits in chronological order no matter what.

I think what you're asking is, instead, how you can change the time stamps on the rebased commits. By default, rebase preserves the time stamps.

Note that there are two time stamps on each commit: the author date, and the committer date.


The rebase documentation describes these two options:

--committer-date-is-author-date, --ignore-date

    These flags are passed to git am to easily change the dates of the rebased commits (see git-am(1)). Incompatible with the --interactive option.

Consulting the git am documentation gives a better description of these:

--committer-date-is-author-date

    By default the command records the date from the e-mail message as the commit author date, and uses the time of commit creation as the committer date. This allows the user to lie about the committer date by using the same value as the author date.

--ignore-date

    By default the command records the date from the e-mail message as the commit author date, and uses the time of commit creation as the committer date. This allows the user to lie about the author date by using the same value as the committer date.

You can see both author and committer dates using the fuller format with git log, for instance (git log --format=fuller).

I believe you want to use --ignore-date, which makes each rebased commit happen "as of right now" (this assumes I'm interpreting your question correctly!).

like image 198
torek Avatar answered Sep 19 '22 18:09

torek