how to rebase and keep commits in chronological order?

两盒软妹~` 提交于 2019-12-03 05:52:43

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!).

If you want to keep the original dates, you are stuck with rebasing according to your dates.

However, if you want to create a pull request and get the commits sorted correctly in a tool that sorts by author date (TFS and GitHub both do), you might want to change the dates instead.

Windows: git rebase --force-rebase --exec "ping localhost -n 3" --exec "git commit --amend --date=now" master

Linux: git rebase --force-rebase --exec "sleep 2" --exec "git commit --amend --date=now" master

Note that doing git rebase --ignore-date master does not work, because the date only has second resolution and different commits will get the same date and be sorted in an unspecified order. If you use --exec and --ignore-date together, the --ignore-date parameter will be ignored. Because it is incompatible with interactive mode, which --exec uses.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!