Using Git

Ssh (Connect to remote repository)

Set up a ssh key

ssh-keygen -t rsa -b 4096 -C ""

Copy ~/.ssh/ contents to your online provider account (bitbucket > manage > ssh keys -> add).


Viewing remote repositories:

git remote -v

Set it (git clone will do it automatically):

git remote set-url origin

Multiple remotes:

git remote set-url origin2 ssh://...

Git Structure  

[workspace]   — stage —> [index] — commit —> [repository]

[repository] — checkout —> [workspace]

Clone, Fetch, Pull, Push


git clone ssh://<remote repo url>


git fetch: Update local repo with remote changes
git pull: fetch and merge into workspace
git push: update remote with local changes

Stage and Commit Changes

git commit -am "comment"

Revert File Changes

git checkout -- file

Amend Commit Message

git commit --amend

Older commits

git rebase -i HEAD~3 # Displays a list of the last 3 commits on the current branch

Note about force push, this can override other peoples changes unless you are the only one working on the branch. Use this instead

git push --force-with-lease

The Head



git status


git log --graph --oneline

Branching & Merging


git checkout master|feature branch
git checkout -b feature/new-feature (off master)


merge (branch) to your branch

git checkout branch-to-merge
git pull
git checkout your-branch
git pull
git merge branch-to-merge


Instead of merging incorporate changes from source branch and add your changes on top.

git checkout branch-to-rebase
git pull
git checkout your-branch
git pull
git rebase branch-to-rebase

Git Alias

$ git config --global checkout
$ git config --global branch
$ git config --global commit
$ git config --global status

Squash (Why Rebase)

Avoid overriding someones changes

git push --force-with-lease

Git Stash

Useful trick when you need to work on another branch while you have changes in the current branch. Stash your changes, and then you will be able to checkout the other branch. Once done with that branch checkout the previous branch and pop the stash to get back your changes.

Save current set

git stash <name>

Retrieve current set

git stash pop


git stash list

Git Cherry Pick

Detached head <TBD>

Git Prune

To prune objects not used by your repository  <TBD>

Git Flow (Branching Strategy)