Ssh (Connect to remote repository)

Set up a ssh key

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

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

Remotes

Viewing remote repositories:

git remote -v

Set it (git clone will do it automatically):

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

Multiple remotes:

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

Git Structure  

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

[repository] — checkout —> [workspace]

Clone, Fetch, Pull, Push

Clone

git clone ssh://<remote repo url>

Fetch/Pull/Push

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

https://help.github.com/en/articles/changing-a-commit-message

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

<TBD>

Status

git status

Log

git log --graph --oneline

Branching & Merging

Branch

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

Merging

merge (branch) to your branch

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

Rebase

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

https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

Squash (Why Rebase)

https://blog.carbonfive.com/2017/08/28/always-squash-and-rebase-your-git-commits/

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

List

git stash list


Git Cherry Pick

Detached head <TBD>

Git Prune

To prune objects not used by your repository  <TBD>

Git Flow (Branching Strategy)

https://nvie.com/posts/a-successful-git-branching-model/

References

https://blog.angularindepth.com/become-a-git-pro-by-learning-git-architecture-in-15-minutes-9c995db6faeb

http://ndpsoftware.com/git-cheatsheet.html