Here’s a scenario for you developers:
1. You are happily hacking on a feature from the beach.
2. But, someone on your team is waiting for your changes to go in because they need few methods on a class you will add as part of your changes.
3. You have written those methods but the whole functionality still needs work so you can’t commit it in.
4. You don’t want the other members of team to wait since time is money.
6. They could work of your branch but that would mean their work will now wait for your work to go in first.
5. Or you just selectively add a couple of methods you have written in the class for them to use.
6. But, is there a way to selectively add and commit files in git?
Yes, you can with git’s interactively adding a file feature. Below is a better explanation of the same feature with a little scenario:
1. Imagine I have a git repository with one file called first.txt. The file’s contents already committed to remote repository are something like:
2. I am working through this file and update the contents to something like:
3. Now, I only want to commit the last two three lines of this code. I have two options, either, I cut all except last 3 lines that I’ve added since the last commit and check it in and then paste the lines I cut back into this file. That could work, but, imagine, if you have to do the same across 3-4 files. Not very efficient, is it?
4. Or you could use the interactive tag for the git’s add command. We will take the same file (I’ve been talking about) as an example and try to add and commit only the last 3 lines of this file.
5. On running the following in a working directory
git add -i
you will see a menu with a list of files and commands one can run on the file.
6. Since, we would like to add only some selected lines we need to enter p(which stands for patch). This will drop you into a patch submenu like the one below. In the patch submenu, you will be able to select the file that you would like to patch. In this case, the letter ‘f’ is the cue for it. Entering ‘f’ on this submenu and returning will show the same menu again, but this time, you will see that there’s a ‘*’ next to the file number on the left. In this case ’1′ became ‘*1′.
7. After the file selection has been made, pressing enter at patch update prompt will drop you into the file, like the screenshot below. You can see the lines that have been added since the last commit. At the bottom, you can seen a set of commands that can be run at this stage. You can see the explanation of these commands by typing ‘?’ and pressing enter.
8. Since, I’d like to only add the last 3 lines, I would type ‘e’ and press enter. ‘e’ stands for manual editing. This will open up the same file in your default editor. Usually, that’s determined by $EDITOR environment variable.
9. After editing and saving the file, I will be returned to the first menu again. Type ‘s’ and press enter to check the status of the files in the working directory. The status shows that 3 lines have been staged and there are still 7 unstaged lines remaining for file first.txt which is what we set out to achieve.
There are a lot of other commands in the menu that shows up on git add’s interactive tag and please feel free to explore them in your own time. It seems like a bit long-winded when you do it the first time, but after a few times it feels very straightforward and easy.
Hope it helps.