开发者

How do I avoid committing small changes in git?

I'm wondering, how could I avoid a commit in really small changes of code. For instance, sometimes I miss a space between parameters or that k开发者_C百科ind of tiny code formatting. The reason I ask this is because later I have to push my commits to a remote repository and I don't want to include those small changes.

Any ideas? Thanks


Update: As others pointed out, do not do any rebasing, or history re-writing of any sorts if you've pushed to a remote origin and shared this code with other developers. Short answer: It's dangerous and risky!

I'd recommend checking out the rebase command for this. It does exactly what you are asking for

What this does is take smaller commits and combine them into larger ones

To use it:

git rebase -i HEAD~5

Your editor will pop up with the last 5 commits from the head of the current branch, with some documentation. In your case you will want to use squash. The site I linked explains it really well, they have this example:

pick 01d1124 Adding license
squash 6340aaa Moving license into its own file
squash ebfd367 Jekyll has become self-aware.
squash 30e0ccb Changed the tagline in the binary, too.

This will package the previous 3 commits and put them all under the one you've marked as pick. You can then modify the commit message and so forth.

Have fun


The only way to make a change is with a commit. However, you could amend a previous commit to include the new change if you like. This may be the best solution when you haven't pushed the previous commit to a remote repository yet.

There's good information about amending git commits in the Git user's manual and in a blog post.


One of the best ways to address this is to use git difftool to run a visual tool like xdiff or winmerge on every modified file before committing. This makes it easy to undo minor changes and will catch significant changes you forgot you made and aren't ready. I catch a fair number of bugs this way. For example, a missing or extra html tag can be hard to spot when you are looking at code, but is easy to spot when you do a diff.


One option is to work on a separate branch, and then squash all the commits of the branch into a single commit when you feel like you've done enough, see this question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜