Tuesday, September 1, 2015

Staging Patches in Git

Don't typically do this at work, but learned this a little while back when there was a large file that required only partial changes. If you can avoid having a large file to partially stage, I highly recommend not doing it, but Git makes it easy to do this.

git add -p (optional pathname)

This goes into a mode where you can accept chunks to stage or alter those hunks to stage a patch. Think of it like an awesome patch editing tool.

For each hunk, say y to accept, n to not, s to split, e to edit. In e, if you replace the "-" at beginning of line with space, it won't stage that line. If you delete a line starting with "+", it won't stage that. Through this, you can do single line changes. Split is helpful in some cases, but even more helpful is the ability to use regular expressions to search for partial hunks to stage or unstage.

Similarly, git reset -p let's you undo staged changes.

There is even git checkout -p and git stash save -p, if needed.

No comments: