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!
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
So useful when trying to remember commands while working with a new technology.
- Gives you a list of commands that you have previously run in your terminal
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 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.
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 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
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:
What is Git? A Beginner's Guide to Git Version Control
Git is a version control system that developers use all over the world. It helps you track different versions of your…
A very nice cheatsheet:
Git Cheat Sheet - 50 Git Commands You Should Know
Git is a distributed version control system that helps developers collaborate on projects of any scale. Linus Torvalds…
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:
Git Command Overview with Useful Flags and Aliases
This post is a short guide to making your git usage a little more efficient. We're not going to cover how git works in…
That is it for this article. I hope this has been helpful. Please leave any thoughts and/or comments below. Happy coding!