开发者

Git big commit best practices

开发者 https://www.devze.com 2022-12-17 08:03 出处:网络
I\'m using git and been working heavily on my local repository the last week. I like to make many small commits (like 2 o 3 files) instead of big ones, but this time between time pressure and that th

I'm using git and been working heavily on my local repository the last week.

I like to make many small commits (like 2 o 3 files) instead of big ones, but this time between time pressure and that the change involved many files, I've got a whole lot of files unstaged.

My question is, what is the best practice in this scenario? Is there any tool that lets me divide the work in small commits?

Note that I do know how to stage files one at a time and make the small commits by hand, but I'm afraid to make a mistake开发者_运维百科 and create a commit that leaves the code in an inconsistent state (like not compiling or working badly).

Thanks!


git add -i should help you divide commits to chunks, then git stash rest of code, check if everything works, git commit, git stash pop and loop.


You can stage part of a file with git add --patch. This should allow you to commit sets of related changes, without committing other unrelated changes that occur in the same files.

And if you want to test the current HEAD without losing all your uncommitted work, git stash is your friend.


Personally, I find git add -i a bit cumbersome to work with as I find it hard to see what it is actually doing.

However, git gui turns out to be quite good at per-hunk and also per-line staging and unstaging. Occasionally it needs a little support by doing a quick temporary edit before staging and then immediately reversing the edit, but most of the time git gui works really well for me.

0

精彩评论

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

关注公众号