DEV Community

Henrique Leite
Henrique Leite

Posted on • Updated on

Git: How to boost your performance

Git is an amazing tool that help us developers a lot to control the version of our projects, it has a lot of build in features, but we can improve it to have a better experience.

In this article, I'll share my configuration of git and a documentation about what each thing does.

Template

const name = ""
const email = ""
const defaultBranch = "master"

const foo = () => {
    if (name === "") {
        throw new Error("You must fill your name")
    }
    if (email === "") {
        throw new Error("You must fill your email")
    }

    console.log(`
# Version: 1.6.0

[user]
    name = ${name}
    email = ${email}

[init]
    defaultBranch = ${defaultBranch}

[pull]
    default = current

[push]
    default = current

[core]
    eol = lf # Defines eol using Linux format
    autocrlf = input

[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

[alias]
    # Clean
    gone = "!f() { git fetch -p && git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -D; } ; f" # https://medium.com/darek1024/how-to-clean-local-git-branches-that-were-removed-on-the-remote-4d76f7de93ac
    cln = prune -v --progress # Remove Branches That No Longer Exists In Repository
    ignore = "!f() { git rm --cached \`git ls-files -i -c --exclude-from=.gitignore\`; } ; f" # Removes Files That Are In .gitignore From The Repository
    # Clone
    cn = clone # Clone Project
    cnsb = "!f() { git clone -b $* $*; } ; f" # Clone specific branch from project
    # Fork
    updfork = git fetch upstream # Update your fork
    # Remote
    rls = remote -v # Return a List of All Branches In Repository
    rrem = "!f() { git remote remove origin; } ; f" # Remove Origin Repository
    radd = "!f() { git remote add origin $*; } ; f" # Add a Repository as Origin
    ratt = "!f() { git remote remove origin && git remote add origin $*; } ; f" # Updates the Origin Repository
    ups = "!f() { git branch --set-upstream-to=origin/$1 $1; } ; f" # Set Branches Upstream
    # ${defaultBranch} Branch
    pum = pull origin ${defaultBranch} # Pull From ${defaultBranch}
    pom = push origin ${defaultBranch} -u # Push to ${defaultBranch}
    # Add
    a = add . # Stage All Changes
    # Pull
    pl = pull # Get Project From Repository
    # Push
    ps = "!f() { git push -f; } ; f" # Push Changes to Repository
    psn = "!f() { git push -f --no-verify; } ; f" # Push Changes to Repository
    psu = "!f() { git push --set-upstream; } ; f" # Create a Link Between Local Branch And Repository Branch
    acips = "!f() { git a && git ci $* && git ps; } ; f" # Stage Changes, Create Commit And Push To Repository
    acaps = "!f() { git a && git ca $* && git ps; } ; f" # Stage Changes, Amend Commit And Push To Repository
    acipsn = "!f() { git a && git commit -m \"$*\" --no-verify && git push -f --no-verify; } ; f" # git acips With --no-verify
    acapsn = "!f() { git a && git commit --amend -m \"$*\" --no-verify && git push -f --no-verify;  } ; f" # Stage Changes, Amend Commit And Push To Repository
    # Commit
    ci = "!f() { git commit -m \"$*\"; } ; f" # Stage Changes and Create Commit
    ca = "!f() { git commit --amend -m \"$*\"; } ; f" # Stage Changes and Amend Commit
    author = "!f() { git commit --amend --author=\"${name} <${email}>\"; } ; f" # Change Commit Author
    # Branch
    b = branch # List All Local Branches
    bd = "!f() { git branch -D $*; } ; f" # Delete Local Branch
    bn = "!f() { git branch -m $*; } ; f" # Change Branch Name
    # Checkout
    ckm = "!f() { git checkout ${defaultBranch} && git pull origin ${defaultBranch}; } ; f" # Change To ${defaultBranch} Branch And Git Pull
    ckd = "!f() { git checkout dev && git pull origin dev; } ; f" # Change To dev Branch And Git Pull
    ckp = "!f() { git checkout $* && git pull; } ; f" # Change Branch And Git Pull
    ck = "!f() { git checkout $*; } ; f" # Change Branch
    cb = "!f() { git checkout -b $*; } ; f" # Create New Branch
    # Rebase
    rbd = "rebase dev" # Rebase Actual Branch With dev Branch
    rbm = "rebase ${defaultBranch}" # Rebase Actual Branch With ${defaultBranch} Branch
    rbh = "!f() { git rebase -i HEAD~$*; } ; f" # Rebase commits (Merge multiple commits in one)
    rbc = "!f() { git a && git rebase --continue; } ; f" # Incase of conflict, you will have to fix it, and then, use this command to continue
    rmm = "!f() { git rebase -i origin/${defaultBranch}~$* ${defaultBranch}; } ; f" # Merge all the commits of ${defaultBranch} branch
    # Stash
    sts = stash
    sta = stash apply
    std = stash drop
    stl = stash list
    stc = stash clear
    # Merge
    mg = merge --no-ff
    cat = checkout --theirs . # Resolve all conflicts accepting INCOMING changes
    cao = checkout --ours . # Resolve all conflicts accepting CURRENT changes
    # History
    sf = show --name-only
    lg = log --pretty=format:'%Cred%h%Creset %C(bold)%cr%Creset %Cgreen<%an>%Creset %s' --max-count=7 # Show the 7 latest commits minified
    # Diff
    st = status # List Changes
    su = "!f() { git status --short | grep --color -E '^(AA|UU)'; } ; f"
    ss = "!f() { git status --short | grep --color -E '^(M |A |C )'; } ; f"
    incoming = !(git fetch --quiet && git log --pretty=format:'%C(yellow)%h %C(white)- %C(red)%an %C(white)- %C(cyan)%d%Creset %s %C(white)- %ar%Creset' ..@{u})
    outgoing = !(git fetch --quiet && git log --pretty=format:'%C(yellow)%h %C(white)- %C(red)%an %C(white)- %C(cyan)%d%Creset %s %C(white)- %ar%Creset' @{u}..)
    # Undo
    unstage = reset HEAD --
    undo = checkout . # Undo Changes
    rollback = reset --soft HEAD~1 # Undo last commit
    # Transfer
    tsf = "!f() { git show $1:$2 > $2 } ; f" # Move changes from a file to another
`)
}

foo()
Enter fullscreen mode Exit fullscreen mode

How to setup

  • Copy the template
  • Paste it on your browsers console
  • Add your name and email to the variables at the beginning of the template
  • Execute it
  • Copy the output
  • Open your .gitconfig file, usually at ~/.gitconfig
    • I personally use VSCode, so I can run the command code ~/.gitconfig to open it
  • Paste the output in there and save
  • Restart your terminal to apply the changes

Documentation

# Version: 1.4.1
Enter fullscreen mode Exit fullscreen mode

This is the version of the gitconfig file (probably it's different from the template, because I'll for sure forget do update it in here, but you get the ideia), to help you know when new things are added or things are fixed.

[init]
    defaultBranch = ${defaultBranch}
Enter fullscreen mode Exit fullscreen mode

This is for when you start a new git repository, you default branch is called master, you can change it if you want.

[pull]
    default = current

[push]
    default = current
Enter fullscreen mode Exit fullscreen mode

This defines that wherever you run git pull or git push it understands that you want to pull/push the current branch and not some other branch.

[core]
    eol = lf # Defines eol using Linux format
    autocrlf = input
Enter fullscreen mode Exit fullscreen mode

This part is extremely important when you are working in projects where people may use different OS. To know more about it, please here this article.

[alias]
Enter fullscreen mode Exit fullscreen mode

This is the beginning of the git aliases definition.

    # Clean
    gone = "!f() { git fetch -p && git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -D; } ; f" # https://medium.com/darek1024/how-to-clean-local-git-branches-that-were-removed-on-the-remote-4d76f7de93ac
    cln = prune -v --progress # Remove Branches That No Longer Exists In Repository
    ignore = "!f() { git rm --cached \`git ls-files -i -c --exclude-from=.gitignore\`; } ; f" # Removes Files That Are In .gitignore From The Repository
Enter fullscreen mode Exit fullscreen mode

These are commands for cleaning your git things.

gone [WIP] prunes all the branches that doesn't exists in the remote repository (like GitHub).

cln [WIP] prunes all the branches that doesn't exists in the remote repository (like GitHub).

ignore remove all the files that were added to .gitignore. Ex: You commit foo.txt and later adds it to .gitignore, it will continue to exists and be tracked by git. To prevent it, you need to run this command.

    # Checkout
    ckm = "!f() { git checkout ${defaultBranch} && git pull origin ${defaultBranch}; } ; f" # Change To ${defaultBranch} Branch And Git Pull
    ckd = "!f() { git checkout dev && git pull origin dev; } ; f" # Change To dev Branch And Git Pull
    ckp = "!f() { git checkout $* && git pull; } ; f" # Change Branch And Git Pull
    ck = "!f() { git checkout $*; } ; f" # Change Branch
    cb = "!f() { git checkout -b $*; } ; f" # Create New Branch
Enter fullscreen mode Exit fullscreen mode

ckm change to the defaultBranch and runs git pull

ckd change to the dev branch and runs git pull

ckp change to the branch that you specifies and runs git pull

ck change to the branch that you specifies (shortcut for git checkout)

cb creates a new branch and checkouts to it(shortcut for git checkout -b)

    # Rebase
    rbd = "rebase dev" # Rebase Actual Branch With dev Branch
    rbm = "rebase ${defaultBranch}" # Rebase Actual Branch With ${defaultBranch} Branch
    rbh = "!f() { git rebase -i HEAD~$*; } ; f" # Rebase commits (Merge multiple commits in one)
    rbc = "!f() { git a && git rebase --continue; } ; f" # Incase of conflict, you will have to fix it, and then, use this command to continue
    rmm = "!f() { git rebase -i origin/${defaultBranch}~$* ${defaultBranch}; } ; f" # Merge all the commits of ${defaultBranch} branch
Enter fullscreen mode Exit fullscreen mode

WIP

    # Stash
    sts = stash
    sta = stash apply
    std = stash drop
    stl = stash list
    stc = stash clear
Enter fullscreen mode Exit fullscreen mode

Shortcuts to work with git stash.

    # Merge
    mg = merge --no-ff
    cat = checkout --theirs . # Resolve all conflicts accepting INCOMING changes
    cao = checkout --ours . # Resolve all conflicts accepting CURRENT changes
Enter fullscreen mode Exit fullscreen mode

WIP

    # History
    sf = show --name-only
    lg = log --pretty=format:'%Cred%h%Creset %C(bold)%cr%Creset %Cgreen<%an>%Creset %s' --max-count=7 # Show the 7 latest commits minifiedwhite)- %C(cyan)%d%Creset %s %C(white)- %ar%Creset' @{u}..)
Enter fullscreen mode Exit fullscreen mode

Shows the commits history.

WIP

    # Diff
    st = status # List Changes
    su = "!f() { git status --short | grep --color -E '^(AA|UU)'; } ; f"
    ss = "!f() { git status --short | grep --color -E '^(M |A |C )'; } ; f"
    incoming = !(git fetch --quiet && git log --pretty=format:'%C(yellow)%h %C(white)- %C(red)%an %C(white)- %C(cyan)%d%Creset %s %C(white)- %ar%Creset' ..@{u})
    outgoing = !(git fetch --quiet && git log --pretty=format:'%C(yellow)%h %C(white)- %C(red)%an %C(white)- %C(cyan)%d%Creset %s %C(white)- %ar%Creset' @{u}..)
Enter fullscreen mode Exit fullscreen mode

Shows the difference between a branch and another.

WIP

    # Undo
    unstage = reset HEAD --
    undo = checkout . # Undo Changes
    rollback = reset --soft HEAD~1 # Undo last commit
Enter fullscreen mode Exit fullscreen mode

Undo your changes.

WIP

    # Transfer
    tsf = "!f() { git show $1:$2 > $2 } ; f" # Move changes from a file to another
Enter fullscreen mode Exit fullscreen mode

Kinda complicate, but very useful in some cases.

WIP

Top comments (0)