This post is written for developers who prefer working with git
via command-line
like me. If you love GUI, hope you still can find something useful here
Git alias
Git aliases are a powerful workflow tool that create shortcuts to frequently used Git commands
git alias
in the simpleset term is creating a shortcut (short command
) for the long ones, make them easier to remember and you can type it faster.
Syntax
Use --global
flag to tell git that the alias will be used in all projects (otherwise, it will only work on your current working project!)
Use quotes (''
) if the original-command
includes space(s).
For me, I create aliases for almost all commands that I work with daily.
Git status
Check the changes before committing:
Tip: Use
git st
with--short
flag or-s
to see the short-format of the changes, and... you know it - create an alias for this command too
Much clearer results and much faster typing, right?
Git commit
Commit changes (add/stage
changes before):
TIP
If the changes are only for existing files (neither new file nor deleted file), use --all
or -a
flag so you don't have to add or stage changes before committing
Git stash
Stash the changes in a dirty working directory away
Like the definition, use git stash
when you need to "stash" the changes before pulling new stuff from remote repo:
Applying the stashed changes after pulling:
Create an alias for it:
Git pull/push
Always pull rebase
and force push
to have a clean commit tree!
pull rebase
What if a conflict occurs after rebasing?
List all the conflicts with
git diff
and create an alias for this command:force push
When you finish resolving the conflicts that occured after rebasing, we need to force push the changes to the remote repo:
TL;DR
The
--force
flag will make git overwrite the remote repo with local changes without comparing with possible updates in the remote after rebasing, which can be dangerous if 2 developers working on the same branch.--force-with-lease
in the opposite way, make sure you can push only when no updates on the upstream exist.
Git checkout
Create a new branch:
TIP
Use git co -
to checkout to the previous branch.
Example:
Git diff
Check the changes before commit (Usually, I use this to make sure no debug
, hardcode
or console.log
is left in my code).
Note
All your aliases can be found in ~/.gitconfig
file (MacOS). You can open this file directly and edit any alias you want.
Prerequisite to be able to edit this file: knowing vim
Git workflow
My daily workflow working with git
(all aliases explained in the above section)
Here is my entire workflow but only the highlighted commands are the most commonly used
.gitignore
and .gitkeep
.gitignore
Tip: ignore all files inside a directory but keep 1 specific file
.gitkeep
How to push an empty directory to the remote repo?
Create a
.gitkeep
file, put it in the empty directory, then you can push the directory to the upstream!
This it not an official feature of git
itself! Just a convention of some random dev out there .
Explannation: the trick here is to make the directory non-empty (it has a file inside!). So, we can push it to the upstream. Thus, .gitkeep
could be any file that you think about (empty or not doesn't matter). Choose .gitkeep
causes it easy to understand and remember.
Wrapping up
Those are all my notes while working with git
, how I understand the concepts and how I work with it faster. Would love to see your use cases in the comment section!
Happy sharing