Git tips #1
From a guy that makes manual backups of git repos
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? 😅
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 😃
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:
yAdd this patch
nDo not add this patch now (it remains available for future uses)
qQuit (you do not stage any of the remaining patches)
aAll (add current hunk and following patches in the current file )
dSkip this patch and all other patches in the current file
eEdit the current patch
?Help (like this list)
After the selection stage, what remains to do is a
git commit 😃
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?
git stash command saves your changes in a hidden space of the local repository called stash.
Imagine editing a file named example.txt:
Now someone asks you to make a change to a specific file etc…
You commit the changes and want to continue writing the example
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 …)
Or by appending the following text to
You can now enjoy your new git aliases using
git a file1 file2,
git c -m "Add a bug" and finally
git ps 😃
gitin 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
~/.zshrc depending on the shell you use in your system.
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.
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). 😃