Can I commit in GIT only parts of a file I’ve modified? What’s git add -p

Hey everyone

Today I come with a Git tip that rocks basically :).

It has happen to me a lot of times that I’ve modified a lot of lines in a file. For¬†clarity, I want my commits to be atomic, so I want a certain commit to only have one part of the modifications I’ve done. I thought this was impossible, but GIT (the SCM Swiss army knife in my opinion) has a solution :)

So, let’s suppose I have a file with texts commited to the repo like the following:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

So, I first modify the second line of the file and change “Line 2″ to “Line 20″
Then, I modify the last line of the file to “Last line”

And now, I want make a commit first with the Last Line modification.

So what do I do? git add -p :)
After that, you’re going to get something like this

This is only the first change. And basically here, I can say “y” or “n” if I want this in my commit. In my case, I don’t want this in my commit, as this is the Line 2 to Line 20 change. So I choose n.

After this, I get the next chunk.

In this case, I do want this change in the commit, so I type y.

Now, you’ll see that the file is twice in the git st. One part is ready to be commited and another one is not staged to be¬†committed.

Awesome right? Go and tell your friends that you know more GIT than them hahaha.

Related Posts

F%#ck Github Pages for my Jekyll blog. Why I decided to use Digital Ocean instead?

Gulp is awesome, but do we really need it?

New Play Framework 2 Video Course: How to create WebApps

comments powered by Disqus