Git-Tricks

Refs


Awesome GitHub Commands Reference Sheet (Quick Reference)

HEAD^       # 1 commit before head
HEAD^^      # 2 commits before head
HEAD~5      # 5 commits before head

Branches

# create a new branch
  git checkout -b $branchname
  git push origin $branchname --set-upstream
# get a remote branch
  git fetch origin
  git checkout --track origin/$branchname
# delete local remote-tracking branches (lol)
  git remote prune origin
# list merged branches
  git branch -a --merged
# delete remote branch
  git push origin :$branchname

# go back to previous branch
  git checkout -

Collaboration

# Rebase your changes on top of the remote master
  git pull --rebase upstream master

# Squash multiple commits into one for a cleaner git log
# (on the following screen change the word pick to either 'f' or 's')
  git rebase -i $commit_ref

Submodules

# Import .gitmodules
  git submodule init
# Clone missing submodules, and checkout commits
  git submodule update --init --recursive
# Update remote URLs in .gitmodules
# (Use when you changed remotes in submodules)
  git submodule sync

Diff

Diff with stats

git diff --stat
app/a.txt    | 2 +-
app/b.txt    | 8 ++----
2 files changed, 10 insertions(+), 84 deletions(-)