Git Quick Reference

For a complete reference to git, visit the git website. There are also plentiful git tutorials online.

For branching in particular, we recommend Learn Git Branching.

Note

In the following examples, a line indented and starting with “$” is a command to be typed at your command prompt of choice (cmd, powershell, bash, zsh, etc).

code.q2developer.com Setup

Note

This will require your public SSH key to be added in code.q2developer.com

  • Generate an SSH key if it hasn’t already been done

    ssh-keygen -f ~/.ssh/id_rsa -P ''

  • Print out the contents of the public half of that key

    cat ~/.ssh/id_rsa.pub

  • Copy/Paste your public SSH key into code.q2developer.com

    • Click Profile -> Settings

    • Click on SSH Keys located on the side panel

    • Paste your SSH Key into the key box, and Add your key

Now that this is done, you can clone your repo:

If coding from Shared Dev

  • Open a SSH terminal to sdk-shared-dev.q2devstack.com and run clone_from_gitlab && source ~/.bashrc

If coding locally

  • run caliper_admin from the directory you would like to clone your code into

  • Choose Start New project -> Clone from existing Git repo

  • Paste in clone url from code.q2developer.com interface

Creating Repositories

This part is already done for you in the shared dev machine

Create a new local repo

$ git init

Create a local repo from an existing project

$ git clone  <url-of-project>

Local Changes

Get the current status of the working directory

$ git status

Look at changes of tracked files

$ git diff

Add all changed files to stage

$ git add .
$ git add --all

Add certain files to the stage

$ git add <file>

Add some changes in <file> to the stage

$ git add -p <file>

Commit all staged files with commit message

$ git commit -m "<message>"

View Commit History

Show all commits

$ git log

Show changes over time for a specific file

$ git log -p <file>

Show who made specific changes and when

$ git blame <file>

Branching

List all existing branches

$ git branch -av

Switch branches

$ git checkout <branch>

Create new branch and switch to it

$ git checkout -b <new-branch-name>

Delete a local branch

$ git branch -d <branch>

Pushing and Pulling

List all currently configured remotes

$ git remote -v

Show information about a remote

$ git remote show <remote>

Download all changes from a remote, but don’t integrate the changes

$ git fetch <remote>

Download all changes and integrate

$ git pull <remote> <branch>

Push code to a remote

$ git push <remote> <branch>

Undo Changes

Discard all local changes in a working directory

$ git reset --hard HEAD

Discard local changes in a specific file

$ git checkout HEAD <file>

Reset the HEAD pointer to a previous commit and preserve all changes as unstaged

$ git reset <commit>

Reset the HEAD pointer to a previous commit and discard all changes

$ git reset --hard <commit>