4 features of Git you may not know

These tips I want to share is based on the assumption that you use Git for source code version control. Git is open source version control software, and is the backbone of several popular code sharing platforms online. Git is the not first version control software available but it is one of the easiest to learn and use. I’ve been using it for over 5 years now and find it very useful, and crucial even for staying productive when developing apps. I learned about these features over the years, they’ve become part of my daily workflow. Even more awesome-sauce is that with these tips I’m sharing with you, you won’t need to install anything further.

Here are some features you can use to be more organized and thus more productive with software development. I’ve listed them here, and below you can find descriptions on what they mean.


1. Stashing changes with a note

Sometimes in the middle of refactoring the code, your client might ask for you to change something. That’s not a big deal because you can stash your changes, such as:

git stash save in the middle of refactoring when interrupted

…then, make changes, and come back to the code you were working on. This is also great when you’re working on something that you’re not ready to commit to just yet :smile:

git stash save blue as main color

git stash save red as main color

All you got to do is git stash pop and you’re back to where you were. Read the docs for details on more options with the flag syntax.

2. Viewing history of changes

Any given week I may be actively juggling a dozen different repositories and I won’t remember all the details like when I last made changes. With git log you can format the history view in a number of ways depending on your preferences.

  • Maybe you want a one liner just to refresh your memory after the weekend: git log --oneline --decorate --color --graph

  • Maybe you want to know how many commits you’ve made over time: git shortlog -sn

  • Maybe you want to know how many lines you added and deleted over time: git log --author="katychuang" --pretty=tformat: --numstat | awk '{inserted+=1; deleted+=2} END {printf "Commit stats:\n- Lines added (total).... %s\n- Lines deleted (total).. %s\n", inserted, deleted}' -

You can format in all these various ways! Read here for the docs

3. Tracking dependencies

Sometimes we want to reference to the copy of another library. For example, with creating data visualizations in Haskell I’m borrowing functions from the Plots library. The situation is such that I’m not maintaining the Plots library but would like to be kept updated with the latest versions so I can include it as a submodule to my main library, MyProject.

The first step is to add the git repo address with the command, such as:

git submodule add https://github.com/cchalmers/plots.git

and whenever I’m working on MyProject, I’ll check for updates and download updates with git submodule update --init --recursive. I did this with my version of ihaskell, since I wanted a specific combination of dependencies with ihaskell (compatible with GHC8) and plots.

The benefits of using this submodule feature compounds as you work with a team of developers and all need to have the same references.

Read the docs for more information!

4. Using alias shortcuts

Although I shared this tip last year, it is so handy that it bears repeating. Alias commands are awesome when using the command line in your workflow. It’s actually a command line feature rather than git… so it’s relevant only if you use command-line git in your workflow.

What you can do with this feature, for instance, with alias test_drive='git add -p' . you can type test_drive instead of the command it references, which will allow you to preview diff changes in git.

You can even replace sudo with a very polite alias please='sudo'.

Furthermore, you can save the flags so you can git ls which will be what you assign it (see some examples).


So there you are…! 4 ways to effectively use Git features. One small action in git, one giant leap in productivity. Did you like this post, is there anything you want to learn? Reach out to me on my FB page, or an email directed to :incoming_envelope: hello [at] macbookandheels [dot] com