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.
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:mkdir myFolder
in the command linecd !$
to move into the recently created folderIn 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:git commit -m'feat: Add new kickass feature'
, but you forgot to add the src foldergit commit src/ !$
, saving you from typing the whole commit message againI 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.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.git commit --amend
to add your changes to the previous commit. 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.
git commit --amend
to add to the previous commitgit commit origin yourBranch --force
to override the changes in the repoUsing
--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.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:
git reset HEAD~1 --soft
will reset the last commit, and you keep your workspace changes. (passing --hard
instead of --soft
means you will discard your workspace changes)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.
git reset HEAD~1 --soft
to undo the commit, but keep the changes in our workspacegit push origin yourBranch --force
to change the git history in our repo!$
replaced with last used argumentgit commit --amend
append changes to the previous commitgit reset HEAD~1
undo the latest commit--soft
keep workspace changes--hard
discard workspace changes--force
overwrite existing code in repository