Some git learnings

Jason Gong
4 min readAug 22, 2021

As a programmer, we interact with many different technologies on a daily basis. The one I want to talk about today is git version control. I will assume you know what git is and have some experience with it. If you do not, I have a couple links below for beginners.

It seems like there are limitless things to learn and git version control has never been a priority for me to master or even delve deeper into past what I need from it. I did some paired coding recently and learned some git commands that seems useful that I would like to share along with some stuff I have learned along the way.

I’ll take a moment here to touch on why I think the below is useful. With editors these days adding plugins that replicates git commands inside the editor, we are all less reliant on the terminal to execute those actions. But in terms of efficient workflow, at least to me, executing git from the terminal is the way to go. Being able to execute everything from the terminal feels much more fluid and easy if you know your way around git.

Now that I’ve given my two cents about git on the terminal, let us get to some learning!

Help

Not some much a command, but a flag for any command. This has been the most useful for me each time I am learning something new in git. Before trying something new in git, use the help flag with the command to have the terminal bring up the docs for you. Take a skim of it. You might find something useful. More often than not, I do.

git <command> --help or git show --help

History

So useful when trying to remember commands while working with a new technology.

history

  • Gives you a list of commands that you have previously run in your terminal

HEAD

The HEAD represents the most recent commit on the current working branch. Along with the tilde ~ symbol and a number, you can target a commit on a parent commit on the current branch. For example,

git show HEAD

  • shows most recent commit for the current branch

git show HEAD~2

  • shows the commit that is two previous from the most recent

Diff

Diff will show the code changes between two commits. When I am running tests in the terminal and there is a bug, I can quickly see the diff from the terminal instead of having to navigate elsewhere for it.

Combining HEAD with diff is a nice tool to have when debugging. You can easily go back and see the code changes on the current branch.

git diff HEAD~1

  • shows code changes versus the previous commit

git diff HEAD~1 -- <filepath> or git diff HEAD~1 -- lib/pry/prompt.rb

  • You can even target a specific file

Checkout

Checkout is a special mention here because it is normally used for switching branches. It does have another function that I have learned to use the other day. It is also able to restore working tree files from a previous commit. Along with a large number of other options, you are able to specify a certain commit along with adding a filename.

If you wanted to restore a file to the state of the previous commit:

git checkout HEAD~1 -- <filename> or git checkout HEAD~1 -- app.js

Detached Head

Do not worry if you ever run into this, the actual state is not as bad as the terminology sounds. A detached head means you are not working from the latest commit on the branch. Most likely, you have checked out a previous commit. Also, you are not able to commit new code from this state. To commit any code, you must be working off of a HEAD commit.

In the case you forgot that you were working off of a past commit and have new code that you want to commit, create a new temporary branch and merge that branch back into your main. I will walk you through it below:

git checkout -b temporary-branch

git checkout main

git merge temporary-branch

I will also leave a couple links to helpful articles about git below.

Best beginner guide that I have seen:

A very nice cheatsheet:

Once you have the basics down, this article takes you to the next level. Alias commands and learn some useful flags for commands that are commonly used:

That is it for this article. I hope this has been helpful. Please leave any thoughts and/or comments below. Happy coding!

--

--