Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Commit only part of a file in Git

Tags: hunk
Today we will be discussing How to Commit only a Part of File in Git , Instead of Commiting the whole file , basically useful if you have dev ,prod ,and other environment parameters in same file Lets Take an Example of a file named database.yml which has three environment(development, staging and production).I have changed the Credentials for Development/staging/Production, and I need to commit the file for production/staging change frequently keeping development changes in my local.So, Instead of reverting the changes of development and commiting the files we can Patch the changes into chunks and commit. Syntax : git add --patch filename.x (or -p for short) Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]? And here the meaning of each option: y stage this hunk for the next commit.n do not stage this hunk the next commitq quit; do not stage this hunk or any of the remaining onesa stage this hunk and all later hunks in the filed do not stage this hunk or any of the later hunks in the file g select a hunk to go to / search for a hunk matching the given regex j leave this hunk undecided, see next undecided hunkJ leave this hunk undecided, see next hunk k leave this hunk undecided, see previous undecided hunk K leave this hunk undecided, see previous hunk s split the current hunk into smaller hunks e manually edit the current hunk ? print help In Our Example:

git add --patch config/database.yml
So It will break the changes into chunk and ask :

production:

adapter: oracle_enhanced

- database: //localhost:29801/mydb

- username: mydb

- password: mydb

+ database: //192.168.1.45:1521/ORCL

+ username: santosh

+ password: santosh

Commit only part of a file in Git

Stage this hunk [y,n,q,a,d,/,K,g,s,e,?]? y
Continue Providing the Options for Development & Production . Finally you will see a Staged Config/database.yml and a unstaged config/database.yml So this is how we commit part file in git . Thanks To Santosh for writing this post .


This post first appeared on JavaRoots, please read the originial post: here

Share the post

How to Commit only part of a file in Git

×

Subscribe to Javaroots

Get updates delivered right to your inbox!

Thank you for your subscription

×