Categories
Git Quick Tips Quick Tips

How to review your changes with git piece by piece before committing

2 min read

Have you ever run git diff in your terminal to see the code changes you plan to git add and commit, but found that seeing all of them in one big diff is overwhelming?

Try out git add -p (the ‘patch’ option) – it will display your code changes in small chunks, and for each one it will ask you whether you want to add it to be committed (git calls this "staging"). Give it a try when you next have some changes ready to commit!

Screenshot of a terminal showing the git add patch option in action.

Bonus tip: After running git add -p you can type ? and hit enter to see the help for this option.


Martin Frost commented with his own handy git tip when I shared this post on Dev. He explained how you can selectively discard code changes that you’ve made:

If you want to selectively discard a specific piece of your total diff, you can use the same -p option, but on the git checkout command. Be very sure about what you are doing when doing this, however, since it will discard the change completely as if you never wrote it.

It can be useful when you have a diff with many parts and you want to only discard a few of them but you don’t want to have to select y for all of the ones you want when using git add -p.

I can see git checkout -p working well alongside git add -p – I’m looking forward to using it in my git workflow.

Thanks for the tip, Martin!