There are a lot of guides to use git and GitHub, link below.
In this page I just put some commands and reminders.
Git download and installation
Download (win) : https://git-scm.com/download/win
Initialize a folder directly and in visual studio code
Initializing a folder means make it ready to work with git, when a folder got initialized will contain a hidden .git folder.
Once git is installed you can initialize it by the mouse right click, click on git bash here and type: git init
On visual studio code, it is possible to initialize a project selecting source control (ctrl+shift+g) and than click “initialize Repository”
Visual studio bash terminal and extensions
An useful extension for git on visual studio is gitlens (is recommended to check history commits etc inside visual studio)
On Visual studio code, for work with git bash on the terminal, you have to select Git Bash form the terminal options
Basic Git commands
The first time using git you have to add name and email, using the follow commands:
git config –global user.email “your”
git config –global user.name “Yourname@yourmail.com”
touch nomefile.txt
Create a file in the directory called nomefile.txt (this is a bash command)
git add nomefile.txt (add the file nomefile.txt to the commit list)
git add . (add all the file to the the commit list)
git reset nomefile.txt (remove the file nomefile.txt to the commit list)
Note: In visual studio code the file added for commit have the A icon, if not the U icon
git commit -m “description” This command commit the items (the description can be multiline using ender before closing with “)
git branch get the current branch name in green and the list of the other branches
git checkout -b pippo Create a new branch called pippo (and auto switch in that branch)
git branch master Go back to the master branch
git log –pretty=format:”%h – %an, %ar : %s” Show the all the commit in a compressed way
GitHub Authentication
SSH key authentication: click here
Https authentication: click here
GitHub link the remote origin to local master git
Create a repository on the GitHub website and follow the instructions on the repository page (note you have to select the SSH option if you use the SSH key to authenticate)
HTTPS git remote add origin https://github.com/username/projectname.git
SSH git remote add origin git@github.com:username/projectname.git
Switch from HTTPS to SSH and vice versa
If your repository is already connect to https and you want to switch to ssh-key you can use the following command
git remote set-url origin git@github.com:username/repo.git
Vice versa if you want to switch from ssh to https the command is the same but you have to use the https url.
Note: If you are already authenticated with https and switch to ssh-key you may like to delete your credentials on the local machine. For doing this on windows 10 you have to go on the control panel > credential manager and delete the github connection on the generic credentials.
GitHub commands Push and Pull
Push our committed files to the online repository
$ git push origin master This will push the content of master on the “origin” of GitHub
Adding -u will cut the command for future calls: git push -u origin master will set the upstream of the current local branch so the next calls can be simply $ git push
Pull download the content from the online repository to the local one
$ git pull origin master
Online courses about Git and GitHub
Questions? Suggestions? Please leave a comment below.