I will describe how to make your life easier as a programmer with these useful tips on using Git. Before reading this, I recommend that you have a basic understand on how Git works.

Essential Command Line Argument

My most used argument with git is probably
!$
. It is actually a command line argument. When you type this, the last used argument is replaced. For instance:
In the above example,
cd !$
will be translated into
mkdir myFolder
, and this is a bit simple, but think about the times you forgot to stage your files before committing:
I know that you can press the up key,
, to get the previous used command, and sometimes that can be just as easy. pressing
ctrl+a
, to move the caret to the beginning of the command, and then use
alt + → 
to move one word at a time, and input
src/
where it needs to be. This is also something I use quite often.

Include Changes In the Previous Commit

I almost always catch typos, excess whitespace, or missing new lines after I make a commit. My best advice for adding these changes is to add them to the previous created commit. You don't want to mess up the git history more than you have to. Use the
--amend
to accomplish this.
By default, on Mac at least, the terminal will open Vim, and you will see the previous commit message. Unless you want to add more to the message, you can type
:wq
which saves the commit message as is, and quits Vim.
But I already pushed the changes to the repository. I cannot change the commit anymore.
Don't worry. You can still amend the last commit, but you need to force push the changes to the repository. If you try to amend a commit that is already pushed to the repo, you will get an error message similar to this
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]
You might get a hint, telling you to pull the changes from repo, but this will create a conflict in your repository, and you need to create a new merge commit to fix it. This is not what we want. Instead, force push the changes you amended.
Using
--force
is a bit scary, because you could loose work permanently. Use it with care, and when you are 100% sure you know what you are doing. It is a powerful command, and with great power comes great responsibility.

I Want to Undo My Previous Commit

This is somewhat the opposite of the previous section. Instead of adding changes, we want to undo them.
I make a lot of mistakes as a programmer, and one of them is committing the wrong files to the wrong branch. What do I do? I want to keep my changes, but remove them from the branch. In order to do this, we need to reset the change:
But I already pushed the changes to the repository. I cannot change the commit anymore.
Don't worry. We have a way of solving it, and it involves a force push this time as well.

Summary