Issue #26 (Git Committing a Selected Range)10/19/22
|
Get a weekly dose of tools, articles, and other resources covering remote work, productivity, career well-being, and lots more.
|
|
|
A neat little feature in VS Code's source control panel is the ability to
stage and commit a selected range from the code you're working on.
As an example, let's say I'm working in a JavaScript file and I've added a few new functions to that file. I might have added two functions that do different things. If I commit all the changes, my commit message will likely be a bit vague, something like "Added two JS functions".
What I would prefer is to commit each function separately so my commit messages make sense. This way, if I have to revert something, it's easy to see the exact change in each commit message.
With selected ranges, I can do exactly that. To do this, I'll open VS Code's source control panel then click on the changed file in the "Changes" section. This should open up a split view where I can see the changes to the file. In there,
I'll select only the portion I want to commit.
Once the selection is made, I can either use the command palette or click the "More Actions" (three dots) button in the top right.
In the menu, I can choose from one of three options related to selected ranges. In this case I would choose
"Stage Selected Ranges".
From there I can commit and push my changes while including a specific and more practical commit message. In this case I might write "Added doAnalytics() function". Now I know that if something breaks, it's definitely this function that caused the problem and I'm not left wondering.
I can repeat the same process for the other function I added, then write my new commit message accordingly.
Pretty useful to be able to do this with such precision. This definitely makes your commit messages more readable and usable should you have problems in the future.