Skip to content

Instantly share code, notes, and snippets.

@vithonch
Created November 14, 2019 09:51
Show Gist options
  • Save vithonch/4d11579e76ccb90b06a50462d07882d0 to your computer and use it in GitHub Desktop.
Save vithonch/4d11579e76ccb90b06a50462d07882d0 to your computer and use it in GitHub Desktop.
Git - tips and tricks for daily usage

Git - tips and tricks for daily usage

Some commands I use daily, kept here for my future environments

GIT

1. Branch checkout shortcut

Go back on latest branch checked out

$ git checkout -

2. Reopen last commit

"Cancel" previous commit, keeping its changes staged

$ git reset --soft HEAD^

3. Add new changes to last commit

Add staged changes to previous commit

$ git commit --amend -C HEAD

4. Squash commits into one

Combine last (e.g.) 3 commits into a single one

  1. Run

    $ git rebase -i HEAD~3
  2. Editor 1: Squash all commits except the first

    pick d701f467 Three commits ago
    s 5aa7d581 Two commits ago
    s 514f6795 Last commit
    
  3. Editor 2: New commit message

Tip: to cancel the rebase, save an empty commit message in Editor 2

5. Force push

As (2), (3) and (4) are rewriting history, you need to force push (not recommended on shared remote branches)

$ git push --force-with-lease

6. Stage/add changes interactively

To avoid committing unwanted local changes, it's safer to review each change individually. Just don't use git add ., instead:

$ git add -p

7. More compact git status

Only show changes, without unnecessary info

$ git status -s

Bash

Oh my Zsh (!)

Better Terminal with ohmyzsh

Aliases

alias gss = 'git status -s'
alias gap = 'git add -p'
alias gc  = 'git commit -m'
alias gp  = 'git push'
alias gco = 'git checkout'
alias gg  = 'git log --graph --oneline --decorate'
alias ggd = 'gg --cc'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment