Git comes with lots of commands, you learn only a few of them and then you blame git because there’s something wrong, is this true? Well, I’ve written some git commands I use approximately every day.

Change the last commit

Have you ever committed your code and then realized you should have added another file or used a different commit message so you create a new useless commit? :sweat_smile:

Luckily, the amend option solves this problem. What you need to do is to add the files you’ve forgotten (I mean using git add) and then issue git commit --amend! After that, you’ll see your editor (hopefully vim) showing the previous commit’s message, edit it, save, quit and the job is done :smiley:

You can amend pushed commits too but be aware of consequences of rewriting git history in public repositories.

Using patches

Not a lot of time ago I was trying the git GUI of IntelliJ IDEA and I was amazed by the feature that allows to stage only some parts of your edits. If you prefer smaller and specific commits over bigger ones, look at patches!

To use this command you have to edit some files and then add them using git add --patch (or git add -p if you belong to the category of lazy developers). Once you issue this command, you’ll be shown an interactive menu with a snippet of code, your changes marked in green, deletions in red and you’ll be asked to decide what to do with that code.

Luckily you have a hint to interact with the menu:

  • y :(fas fa-long-arrow-alt-right): Add this patch
  • n :(fas fa-long-arrow-alt-right): Do not add this patch now (it remains available for future uses)
  • q :(fas fa-long-arrow-alt-right): Quit (you do not stage any of the remaining patches)
  • a :(fas fa-long-arrow-alt-right): All (add current hunk and following patches in the current file )
  • d :(fas fa-long-arrow-alt-right): Skip this patch and all other patches in the current file
  • e :(fas fa-long-arrow-alt-right): Edit the current patch
  • ? :(fas fa-long-arrow-alt-right): Help (like this list)

After the selection stage, what remains to do is a git commit :smiley:

Stash

The stash is useful for storing uncommitted changes and re-apply them later. This is useful in situations in which someone asks you to work on something else. In a normal case, you make a backup of the current folder, do the job and then restore the copy with your changes. But what if I tell you to avoid doing this because there’s a better way?

The git stash command saves your changes in a hidden space of the local repository called stash.

Imagine editing a file named example.txt:

$ echo "This is an example" > example.txt

$ git status 
On branch dev 
Changes not staged for commit:
	modified: example.txt

Now someone asks you to make a change to a specific file etc…

$ git stash push

$ git status
On branch dev
nothing to commit. Working tree is clean

You commit the changes and want to continue writing the example

$ git stash pop

$ git status 
On branch dev
Changes not staged for commit:
	modified: example.txt

As you can see, this is a straightforward way to put aside your current situation and resuming it later.

Free extra: use aliases

I’ve written a small extra tip for using git in a faster way: Using aliases.

Git is made by developers and developers are very lazy, so why did they put these looong commands? Why didn’t they make them shorter?

Jokes aside, git commands are self-explanatory and you can’t use git without knowing their proper name, but there are two solutions.

Setting aliases using git

You can set aliases using the command line (bash, zsh, Powershell …)

git config --global alias.c commit
git config --global alias.a add
git config --global alias.ps push
git config --global alias.ca "commit -a"
git config --global alias.rs "reset --hard HEAD"

Or by appending the following text to ~/.gitconfig file

[alias]
	c = commit
	a = add
	ps = push
	ca = "commit -a"
	rs = "reset --hard HEAD"

You can now enjoy your new git aliases using git a file1 file2, git c -m "Add a bug" and finally git ps :smiley:

You have to write git in full otherwise the alias won’t be interpreted. If you are lazy down to the bone, read the next method.

Setting aliases using a *nix shell

The previous method is for hard-working devs, isn’t it? Luckily we can use bash or zsh to set custom alias and avoid writing git every time.

To create new aliases, you just have to append the following content to your ~/.bashrc or ~/.zshrc depending on the shell you use in your system.

alias gc="git commit"
alias ga="git add"
alias gcm="git commit -m"
alias glo="git log --oneline"
alias gst="git status"

As you can see, these are just normal bash commands so you can figure out an alias behavior by running it in your terminal.

Therefore, you have to use source ~/.bashrc or source ~/.zshrc to apply these aliases in your current shell and ultimately ga file1 file2 to add those 2 files, gcm "Add two bugs" to commit and write a short message.

Do NOT overwrite any command with aliases. You risk damaging your OS.

That’s all, folks!

These are some git features I’ve discovered recently and I hope you’ve liked at least one of them. Thank you for taking the time to read this post and feel free to contact me on telegram (link on the homepage). :smiley: