Key notes while working with git

Published on
7 mins read
  • avatar
    Tuan Anh Huynh (Leo)
Photo by Yancy Min on Unsplash

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.


$ git config --global alias.<shortcut> <original-command>

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:

$ git config --global status
# Now instead of `git status`, use `git st`
$ git st
On branch v2
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: components/Twemoji.js
modified: css/tailwind.css
modified: data/blog/git-notes.mdx
no changes added to commit (use "git add" and/or "git commit -a")

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

$ git config --global alias.s 'status --short'
# Now instead of `git st`, use `git s`
$ git s
M components/Image.js
M data/blog/git-notes.mdx
?? public/static/images/force-with-lease.jpg

Much clearer results and much faster typing, right?

Git commit

$ git config --global 'commit -m'

Commit changes (add/stage changes before):

$ git cm "Initial commit"

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 config --global 'commit -am'
# Now instead of 2 git commands
$ git add style.css # `style.css` is already existed, not new file!
$ git cm "Update style"
# Use only 1 command
$ git cam "Update style"

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:

# Too short to create an alias
$ git stash

Applying the stashed changes after pulling:

$ git stash pop

Create an alias for it:

$ git config --global alias.sp 'stash pop'
# Now
$ git sp
# Is equal
$ git stash pop

Git pull/push

Always pull rebase and force push to have a clean commit tree!

  • pull rebase

    $ git config --global alias.prb 'pull origin --rebase'
    # Now
    $ git pull origin --rebase main
    # Is equal
    $ git prb main
    # Or
    $ git prb master
  • What if a conflict occurs after rebasing?

    List all the conflicts with git diff and create an alias for this command:

    $ git config --global 'diff --name-only --diff-filter=U'
    # List all the conflicts
    $ git cf
    # Reolve all conflict then stage changes
    $ git add .
    # Finish rebasing
    $ git rebase --continue
  • force push

    When you finish resolving the conflicts that occured after rebasing, we need to force push the changes to the remote repo:

    $ git config --global 'push --force-with-lease'
    # Now after rebasing
    $ git pf

    Why not --force?


    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

$ git config --global 'checkout'
# Eg
$ git co main

Create a new branch:

$ git config --global alias.cob 'checkout -b'
# Eg
$ git cob feature-x

Tip: use git co - to checkout to the previous branch.


$ git branch
* feature-x-y-z__ISSUE_ID
# The current branch is `feature-x-y-z__ISSUE_ID`
# Checkout to `dev`
$ git co dev
# Do something
# Commit ...
# Now to come back to `feature-x-y-z__ISSUE_ID` use
$ git co -
# Instead of
$ git checkout feature-x-y-z__ISSUE_ID

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).

$ git config --global alias.d 'diff'
# Eg
$ git d style.css


All your aliases can be found in ~/.gitconfig file (MacOS). You can open this file directly and edit any alias you want.

$ vim ~/.gitconfig
# Find the alias part in the config file
s = status --short
st = status
cm = commit -m
# ...

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)

# Stash changes
$ git stash
# Update changes from upstream
$ git prb main
# Apply stash changes
$ git sp
# Resolve conflict if existed
# Work
# Check working status
$ git s
# Check file changes (if needed)
$ git d # or git d file.ext
# Stage changes
$ git add .
# Commit
$ git cm "commit message"
# Or skip stage changes if no new file created/deleted
$ git cam "commit message"
# Update changes again
$ git prb main
# If there're conflicts, resolve all then
$ git add file.ext
$ git rebase --continue
# Force push
$ git pf
# Making pull request

Here is my entire workflow but only the highlighted commands are the most commonly used

.gitignore and .gitkeep


Useful ready to use templates

Tip: ignore all files inside a directory but keep 1 specific file

# Ignore all file in a directory
# Keep only this file


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