Make Your Life Easier With These Git Workflows: Part Iโ€‚by@johnedvard

Make Your Life Easier With These Git Workflows: Part I

John Edvard Reiten HackerNoon profile picture

John Edvard Reiten


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
    mkdir myFolder
    in the command line
  • Then type
    cd !$
    to move into the recently created folder

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:

  • git commit -m'feat: Add new kickass feature'
    , but you forgot to add the src folder
  • Then you can write
    git commit src/ !$
    , saving you from typing the whole commit message again

I know that you can press the up key,

, to get the previous used command, and sometimes that can be just as easy. pressing
, to move the caret to the beginning of the command, and then use
alt + โ†’ 
to move one word at a time, and input
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

to accomplish this.

  • Use
    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

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 commit
  • git commit origin yourBranch --force
    to override the changes in the repo


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:

  • git reset HEAD~1 --soft
    will reset the last commit, and you keep your workspace changes. (passing
    instead of
    means you will discard your workspace changes)
  • 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.

  • git reset HEAD~1 --soft
    to undo the commit, but keep the changes in our workspace
  • git push origin yourBranch --force
    to change the git history in our repo
  • Now you can change branch, and commit the workspace changes.


  • !$
    replaced with last used argument
  • git commit --amend
    append changes to the previous commit
  • git reset HEAD~1
    undo the latest commit
  • --soft
    keep workspace changes
  • --hard
    discard workspace changes
  • --force
    overwrite existing code in repository


Signup or Login to Join the Discussion


Related Stories