newBike
newBike

Reputation: 15012

How to commit fine change and keep log simple using git

When I develop an new feature, I may tune my code many times for a better performance. In the 100 test times, I may got better performance at time 1, time 13, time 43, ...

Should I commit the code when got a better performance?

If so, I will get lots of small commit logs in my history if there are some small changes (eg. typo, or just make the code be more beautiful). But most time I can not do it at once, it usually takes me many and many times.

What's the better way to log above small changes?

Upvotes: 2

Views: 130

Answers (2)

Atropo
Atropo

Reputation: 12541

I prefer having a large number of small feature related commits, in this way you can easily revert a commit to go back to a previous state.

When you're working with other developers you've to consider company policies when pushing on a central repository.

When working on your local copy, git gives you the tools to merge your commits using rebase:

git rebase -i HEAD~5

Will open an interactive rebase on the last 5 commits, you can then choose the squash or fixup option to merge them together:

 #  s, squash = use commit, but meld into previous commit
 #  f, fixup = like "squash", but discard this commit's log message

In this way you can then push just a single commit to the central repo.

Upvotes: 0

Boris Brodski
Boris Brodski

Reputation: 8715

In case you what to reduce an mount of commits and simultaneously keep a detailed history I would recommend to use feature branch + the squash method. Here is an example:

Suppose you are on the master branch with no local changes.

First create a feature branch

$ git checkout -b tuning

Then perform your tuning commiting each relevant step

... small change ...
$ git commit -am "Time 1"

... small change ...
$ git commit -am "Time 2"

... small change ...
$ git commit -am "Time 3"

...

Now the history looks like this

* (tuning) Time 3
|
* Time 2
|
* Time 1
|
* (master)

Next create a temporary branch and squash all changes to a single commit on master branch. This is done by reseting to the original master branch preserving local content and git index (--soft), then checking out master branch and then commiting into it.

git checkout -b tmp
git reset --soft master
git checkout master
git commit -m "Tuned (see tuning branch for more details)"
git branch -D tmp

Now you get a single commit on the master branch and a whole history of this commit on the tuning branch.

* (master) Tuned (see tuning branch for more details)
|
| * (tuning) Time 3
| |
| * Time 2
| |
| * Time 1
| /
*
|

Upvotes: 3

Related Questions