Press enter after choosing selection

Git Tip: Commit part of a file

by ejk

I recently found myself in the position of having edited a single file of code for two different projects at the same time (I know, I know, I should have branched first before editing) and not wanting to commit both of them at the same time. Well git has an awesome feature for staging code changes based on section, rather than the entire file. Just stage the file with

git add --patch filename.code

and it will present you with an interactive prompt for each section of changed lines within the file, allowing you to include that section or not in the new commit.

I was able to easily commit the changes that were ready and roll them out while holding off on the changes that weren't ready. Oh, and I immediately moved those changes to a new branch so as not to run into this problem again.

Source and further info: commit-only-part-of-a-file-in-git

Comments

Graphic for blog posts

Blog Post