Skip to content

matheusjardimb/git_tips_and_tricks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

General Git tips and tricks

Opinionated list of tips and tricks for using git. This page is UNDER DEVELOPMENT, feel free to contribute via PR's.

Setting up your environment

Installing Git and other tools

Run the following command for installing git and gitk:

sudo apt-get install git gitk

Setting user information

General information which needs to be set to identify commits' authors:

git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "MY_NAME@example.com"

# Switch 'nano' with your preferred editor
git config --global core.editor "nano"

Run these commands inside a repo with --local (instead of --global) to override such values at project level.

Check the stored settings with:

# Change the param name to check a specific value
git config user.name

# Or list all settings with
git config -l

Adding Git shortcuts

Add the following lines at ~/.gitconfig to create aliases globally:

[alias]
    # Short-formatted version of 'git log'
    l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short

    a = add
    aa = add -A
    ap = add -p

    c = commit --verbose
    ca = commit -a --verbose
    cm = commit -m
    cam = commit -a -m
    m = commit --amend --verbose

    d = diff
    ds = diff --stat
    dc = diff --cached

    s = status -s
    st = status

    co = checkout
    cob = checkout -b

    # Remove files from staging area
    unstage = restore --staged

    # List branches sorted by last modified
    b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"

    # List aliases
    la = "!git config -l | grep alias | cut -c 7-"

Displaying current branch on console

Add the following snippet at the end of ~./bashrc to display the current branch on the console (took from askubuntu):

### Shows git branch name
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt

Setting pre-commit hooks

TODO: fill this section

Popular conventions

Squash commits while merging code

TODO: fill this section

Commit message verb tense preference:

TODO: fill this section

Gitflow

TODO: fill this section

References:

Gitflow

Useful Resources

Useful commands to keep in mind

TODO: fill this section

# Links the origin target to the local master branch
git push -u origin master

# Newer version of: git checkout -b BRANCH
git switch -c BRANCH

# Lists local branches and their respective upstream (remote repo)
git branch -r

# TODO add comments on reflog
git reflog

Useful links

About

General tips and tricks for git users

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages