Git Reference and Session Patterns

Git Reference

Below I will summarize the list of most important Git commands. Note that the names put in the hyperlink-format have to be substituted with the actual names specific to your work.

Working with remotes

  1. git clone url_or_path_to_repository-you-copy.git

    Clones (copies everything from) another repository, located at url_or_path_to_repository-you-copy.git to your current repository

  2. git remote add remote-name remote-url

    Adds the remote you can pull from and push to. The remote-name will be a short name of the remote, whereas remote-url is the actual link to the remote repository (ending with .git)

  3. git remote rename old-remote-name new-remote-name

    To rename repository

  4. git remote rm remore-name

    To remove repository the remote remore-name from your list of available remotes. This doesn't change the remote repository, of course

  5. git pull 

    takes the latest version of your remote (usually called "origin") master (by default) branch and merges it into you presently chosen branch.

  6. git pull remote-name remote-branch

    takes the latest version of the remote-branch branch on the remote repository remote-name and merges it into you presently chosen branch.

  7. git push 
    git push remote-name remote-branch

    Same as "git pull" and its version, only you send your code back - from your local repositry to the remote (called upstream repository)

Development commands

  1. git init

    Creates a new, empty repository

  2. git add files-or-directories

    This will add the files-or-directories to the so-called "staging" area (think of active space in excited dynamics) - something you are working with at the moment.

  3. git commit -m "Some-message"

    This will commit the files in the "staging" area to the development history of the code - this way you save the most valuable and recent state of the project. The message should be informative and must describe clearly what changes have you made in the present version w.r.t. the older state of the code.

  4. git branch branch-name

    Will create a new branch called branch-name. The present repository in its present state will be copied into the new branch.

  5. git checkout branch-name

    Switch into the branch-name branch. You will see a corresponding set of files. The uncommited changes in the branch from which you are switching will be forgotten (i think)

  6. git merge branch-name

    Will delete the branch called branch-name into the branch where you presently are.

  7. git branch -d branch-name

    Will delete the branch called branch-name.

Info commands

  1. git status

    shows the current state of your project

  2. git log

    shows the history of all commits

  3. git log --graph --pretty=format:'%C(magenta)%h%C(blue)%d%Creset %s %C(blue bold)- %an, %ar%Creset'

    shows the history of all commits and the development tree, highlights the sections

  4. git diff

    shows changes in the present state made w.r.t. the latest commit

  5. git branch

    shows the list of available branches

  6. git remote -v

    shows the list of available remote repositories

Git utilization patterns

Below, I will show some typical sessions with git, I had so far. I may be using specific names for the repositories - but you'll have to adapt them to your specifics, of course

Typicall coding session

  1. work on the file "main.cpp"

  2. git add main.cpp
  3. work on several files in the "src" directory of my project

  4. git add src/
  5. git commit -m "Added new features in the main program and some documentation in other source files"