Two Alternative Ways To Git Add And Git Commit

Gabriella’s Journey
3 min readMar 5, 2018

--

While pairing with Felipe last week I got the chance to learn a lot of new stuff. Some of the new stuff I learned from Felipe include some cool git commands I didn’t know before.

Now, someone already elected Felipe ‘Java Jesus’ :). I’d like to give him another official appointment: ‘King of Git’.

An alternative way to git add: git add -p

Since last week, before pairing with Felipe, I had been used to staging changes of my code with git add ., if I wanted to stage everything, or git add <file path>, if I wanted to stage just one single or more files, but not all of them.

So, if for example I wanted to stage just the changes contained in the file lib/hangman_rules.rb below, I’d copy and paste the whole file path in the terminal (you can use tab to autocomplete the path name as well):

Another way to stage only some of the files changed is with the command git add -p.

The cool thing of this command is that it allows you not only to add one single file to stage, but just one piece of code changed/added instead of the entire file.

The patch mode ( -p ) goes through the list of files changed from the top and allows you to choose whether to stage a hunk of code included in each file or not:

To stage the relevant hunk of code type y otherwise type n (as per the picture above, there are further options for you when prompting to stage the hunk: I haven’t used them yet apart from the y and the n which stand for yes and no respectively, and won’t talk about these in this blog post.)

Once an action upon the hunk of code to stage is made, git shows you the next piece of code to take an action on:

and so forth, until you’re done with staging your code, and you’re ready to commit.

An alternative way to git commit: git commit -v

Now the parts of code you staged are ready to be committed. The command I would use to commit since last week is git commit -m 'commit description' (the-m standing for message.)

If you use git commit -v ( verbose mode) you’re able to see all the code to commit by scrolling the page down if necessary and to add the description for the commit in one go:

In the first line you add the description for the commit and then save and quit to get back to the main terminal session.

Another way to see all the changes of the code that need committed is by using the command git diff . This allows you to scroll down the terminal page and to see all your changes, but only before staging the code.

If you have already added the code to stage, then you can use git diff —-cached. Once you see all the changes you can then add and/or commit.

git commit -v is just another way to achieve all of this, but it’s more powerful in the fact that you can do all of the above in one single go without having to jump from one terminal session to another. Also, it is more easy to come up with a good commit description if you can visualise all the code to commit and work on writing an appropriate description simultaneously.

--

--