Git basics


Introduction

This is a quick reference file to help new users to configure git.

Create an SSH Key - Mandatory

This guide (coming soon) is to be used with an SSH Key. This is how you can easily generate one.

Basic global configuration

This step is only required for the first time configuration, or if using multiple git accounts. Enter the credentials for the account to be used.

  git config --global user.name "My Name String"
  git config --global user.email "[email protected]"

Creating repository

For newbies, this is what I believe is the simplest way to create a new project with an online Git hosting account (Github, Gitlab. etc.), and sync a folder with your computer.

Create new project

Go to your online hosting repository and create a new project. For this tutorial the project name is GitBasics.

Select ONE of the options

Create a new local repository ("I don't have any local files yet")

This will:

  • create a sub-directory with the name of your new project.
  • create a blank read-me file locally.
  • tell git to track the new read-me file.
  • commit the new file (tell git to save the tracked changes so far).
  • send the new file to the online repository.
  #cd root folder where a sub-directory will bre created for the new project
  git clone [email protected]:username/gitbasics.git
  #gitrepo == online repository. Ex.: gitlab.com
  cd gitbasics
  touch README.md
  git add README.md
  git commit -m "add README"
  git push -u origin master

Create new local repository and sync an existing local folder ("I already have some stuff in my computer")

This will:

  • Create a local git repository.
  • Sync this local repository with the online repository.
  • Add existing local files to git for tracking changes.
  • Commit changes (tell git to save the changes of the existing files as they are).
  • Send the local files to the online repository.
  cd existing_folder
  git init
  git remote add origin [email protected]:username/gitbasics.git
  git add .
  #this will add ALL the files in the folder. If you don't wnat to add all the files, check the section for .gitignore
  git commit -m "Initial commit"
  git push -u origin master

Send an existing local repository to the new online repository (I have a local folder already tracking changes with git)

This will:

  • Rename the current repository to old-origin.
  • Sync this local repository with the new online repository.
  • Send everything to the new online repository.
  cd existing_repo
  git remote rename origin old-origin
  git remote add origin [email protected]:username/gitbasics.git
  git push -u origin --all
  git push -u origin --tags

Gitignore

If there are files in your repository folder you do not want to track changes or keep synced with your online repository, simply create a file called .gitignore and add the name of the files to be ignored (one line for each file).

  touch .gitignore
  echo donottrackme.ext >> .gitignore
  #.gitignore should be added to the remote repository if you want the same files to be ignored in different computers
  git add .gitignore
  #to complete the sync, remember to commit and push