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:- Type
in the command linemkdir myFolder
- Then type
to move into the recently created foldercd !$
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:
, but you forgot to add the src foldergit commit -m'feat: Add new kickass feature'
- Then you can write
, saving you from typing the whole commit message againgit commit src/ !$
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.- Use
to add your changes to the previous commit.git commit --amend
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.
to add to the previous commitgit commit --amend
to override the changes in the repogit commit origin yourBranch --force
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:
will reset the last commit, and you keep your workspace changes. (passinggit reset HEAD~1 --soft
instead of--hard
means you will discard your workspace changes)--soft
- Now you can change branch, and commit the changes to the correct one.
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.
to undo the commit, but keep the changes in our workspacegit reset HEAD~1 --soft
to change the git history in our repogit push origin yourBranch --force
- Now you can change branch, and commit the workspace changes.
Summary
replaced with last used argument!$
append changes to the previous commitgit commit --amend
undo the latest commitgit reset HEAD~1
keep workspace changes--soft
discard workspace changes--hard
overwrite existing code in repository--force