5 Git commands that will make your life easier
Tags: git, github, gitlab, bitbucket
Published: 2020-08-10 12:01:33

Git is a tool that is obligatory to any software project nowadays. If for some very weird reason it isn’t, then you are doing a big mistake. Versioning and maintaining the code is what allows the software to live much longer life and make us, the developers much happier. My experience with it has been only superior, therefore I wanted to share some of this in couple of hidden, or rarely used commands that made my life much much easier.

# git log — oneline — decorate — all — graph

The\* \*`git log` is a no brainer really. If you want to check the history of your repository, then that's the command that you will be using mostly. The problem is that the default output is too verbose in my opinion. It includes the information you don't really need and doesn't include the one you might be interested in. So the default output looks like this:

![](https://cdn-images-1.medium.com/max/1764/0*Z5xGFPnpbV7E9Fw2.png)

As you can see it includes many of the information you might not need. I am using standard sized terminal windows, and I could only fit 3 latest commits. The great alternative to this is to use the extra flags in the _log_ command. I am applying the *online, graph, decorate *and _all_ flags. Now the output look like this:

![](https://cdn-images-1.medium.com/max/1908/0*7DP1rhd4zHgxMY2r.png)

Now the _log_ command shows much more information in condensed form. Let’s focused on the flags that I have added. Each of these have very distinct yet useful role in making the _log_ output more usable.

- `--oneline` - This is a shorthand for "--pretty=oneline --abbrev-commit" used together. Instead of showing the full 40-byte hexadecimal commit object name, show only a partial prefix.

- `--decorate` - This flag prints out the ref names of any commits that are shown.

- `--all` - This command makes all the refs in `refs/`, along with `HEAD`, listed on the command line as _<commit>_

- `--graph` - This draws a text-based graphical representation of the commit history on the left hand side of the output.

Read more [here](https://git-scm.com/docs/git-log).

# git rebase -i

The _rebase_ itself is used a lot to change or update the base of our working tree. The not so commonly used version of it is the **interactive rebase**. The rebase in this mode allows us to rewrite the history of our tree a bit, by changing various elements of the commits that have been made. The typical output shown in the terminal, after running the `git rebase -i` may look like this:

![](https://cdn-images-1.medium.com/max/1908/0*dmn_EkZdXscWqN1S.png)

On this screen you can see the plethora of options to choose from. Additionally, you can reorder the commits if you need to, so that in the history, they will appear upside down or any combination you like.

Read more [here](https://git-scm.com/docs/git-rebase).

# git commit — am

I have came across many situations where my commit message seemed to be okay, but after couple thoughts I wanted to change them to something more descriptive. The `git commit --am` command is what allows you to do it.

![](https://cdn-images-1.medium.com/max/1908/0*uingmIl9rwAcVQdE.png)

On the image above you can see the screen that you get, after applying the command mentioned above. From this point it’s very easy to change the message in the commit as needed. Very easy to remember and very useful in daily workflow.

Read more [here](https://git-scm.com/docs/git-commit).

# git cherry-pick

The `git cherry-pick` is probably one of the least known commands in the Git, but it's very useful in my opinion. It allows you to apply some other commit into your working tree by using it's hash. It's quite useful when someone working on completely different branch has made some valuable or interesting changes and you want to test them in your branch/tree without having to merge or rebase.

The first thing you need is the hash of the commit you want to _pick_. You can use the `git log` command shown above to retrieve it. You don't need the full hash, the short version will do as well. Then we use `git cherry-pick` command with commit's hash added at the end.

![](https://cdn-images-1.medium.com/max/1908/0*LeQUKjFiSwklYwPM.png)

On the image above you can the change that _cherry-pick_ will make to our working tree. Initially it was clean, and after successful applying the commit, we are 1 ahead of _origin/master_. It is surprisingly useful command, especially when you exchange code a lot in your team.

Read more [here](https://git-scm.com/docs/git-cherry-pick).

# git checkout -

The last one is quite simple and it may be known to you already. The dash ( — ) flag applied to _checkout_ command indicates last used branch. So if you were on a _master_, switched to _develop_ and what to switch back, you don’t have to specify whole name of it.

![](https://cdn-images-1.medium.com/max/1908/0*TvnZBpLnwj3jB_OP.png)

Read more [here](https://git-scm.com/docs/git-checkout).

That will be it for this post. I hope you will find the commands here useful and they will ease up you Git workflow. If you have your favorite Git command, you want to share, please don’t hesitate in doing so in the comments section below or any of the social medias :)

{% twitter 1222945353722363905 %}