git commit, push - Commit only part of a file in Git

git commit meaning / git / git-commit

When I make changes to a file in Git, how can I commit only some of the changes?


Answer #1

As one answer above shows, you can use git add --patch filename.txt

or the short-form git add -p filename.txt

... but for files already in you repository, there is, in s are much better off using --patch flag on the commit command directly (if you are using a recent enough version of git): git commit --patch filename.txt

... or, again, the short-form git commit -p filename.txt