Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best practice for melding into NEXT commit in git rebase -i?

Tags:

git

Because of its cheapness, I use commit to keep my daily WIP snapshots. So my commit history contains many snapshot commits and several key commits mixed together.

Before going to push my work to a remote repository, I may feel like cleaning my history to contain only key commits by melding proceeding snapshot commits into directly following key commits. The date information of key commits is important to me because it recalls me when and what I have changed.

Using squash/fixup commands in git rebase -i may be of some help but they meld into PREVIOUS commits and their author dates are not what I want.

Re-ordering commits then squashing often results in unnecessary conflicts, which is also what I want to avoid.

So. What is the best practice to satisfy my desire?

[Update] 2016-07-03
Inspired by torek's answer, I came up with a solution: use git rebase -i as usual, set the target commit that I want previous commits to meld into to edit, then execute git reset HEAD~n where n is the number of previous comments that I want to meld. And commit the melded changes with:

% git commit --amend --date "$(grep DATE .git/rebase-merge/author-script | cut -f2 -d '=')"

After that, finish the rest of work by git rebase --continue.

like image 507
A.Miya Avatar asked Jun 30 '16 00:06

A.Miya


People also ask

Should I merge before commit?

If there were uncommitted worktree changes present when the merge started, git merge --abort will in some cases be unable to reconstruct these changes. It is therefore recommended to always commit or stash your changes before running git merge.

Do I need to commit before git rebase -- continue?

For each change you make, you'll need to perform a new commit, and you can do that by entering the git commit --amend command. When you're finished making all your changes, you can run git rebase --continue . As before, Git is showing the commit message for you to edit.

When should I merge and when should I rebase?

If the feature branch you are getting changes from is shared with other developers, rebasing is not recommended, because the rebasing process will create inconsistent repositories. For individuals, rebasing makes a lot of sense. If you want to see the history completely same as it happened, you should use merge.

What is the safest and fastest way to combine commits from two different branches?

The git merge command integrates the independent lines of development into a single branch. The command is used to combine two branches and also to merge multiple commits into one history. Once you make changes in the local repository and ready to share it with your team members, then execute git push.


1 Answers

Here is one way: use rebase as usual. Set the disposition for the fixup-or-squash-commit to edit. When Git stops to let you edit the fixup, use git reset --soft HEAD^; git commit --amend --reset-author-date, but be aware that this uses the date of "right now". For more control, and to pick up the date from the squash/fixup, grab the author-date from that commit and use --date: you will probably want to write a little shell script to do that.

(Use git log --pretty=format:%ad --no-walk to extract the date from the current commit, before the git reset --soft HEAD^ step. You may want to save the commit message text, or even the entire ID of the commit: the soft-reset leaves the commit in the repository, where it is protected by both the in-progress rebase and the various reflogs, so you can extract whatever you want from it afterwards, as well as before. Of course the author and committer and their dates, and the message, cover everything useful here.)

To squash much further back, you can do multiple rebases, or multiple squashes in the interactive edit, or git reset --soft further back than HEAD^.

(I never bother with this: the specific date stamps don't seem that useful here.)

like image 191
torek Avatar answered Oct 05 '22 04:10

torek