Git is a version control system (vcs)

Get on Focus account

Step 01 - Install

The very first thing we need is to install it in our system. You can install Git using Homebrew, MacPorts, or by downloading the Git installer package https://git-scm.com/downloads


Step 02 - Version

What is my git version? git --version. Currently I am running version 2.14.3 (Apple Git-98) 28 of Jul 2020


Step 03 - Project setup

Download a project file like https://startbootstrap.com/templates/heroic-features/. You can create your own project in your desktop directory and include two files to it to get started. For instance, index.html and main.js.


Step 04 - Init

To start working with git we need to initialise a repository and the way we do it is git init Navigate to your root project folder you just downloaded or created your own and we need to initialise the folder as a git repository by running the cmd git init. This cdm will create a .git folder in your project root directory. This folder is hidden by default. After Initialised this project as a git repo we can start running git cmds. To visualize hidden file I can use the cmd ls -a or using the keyboard Command + Shift + Dot


Step 05 - Config

Our next step now is to configure the username and email for the git repo.

This is not use to login. This is only use to track who made what changes.

git config --global user.name 'NAME' git config --global user.name 'NAME'

git config --global user.email 'EMAIL' git config --global user.email 'EMAIL'

Confirm that you have set the Git username correctly: git config user.name

Confirm that you have set the Git email correctly: git config user.email


Step 06 - add

Adding files to the staging area. It means adding files to be tracked

Single file: git add index.html

Two files: git add index.html app.js

All files: git add .

All html files git add*.html

Bear in mind that after the stagging area we still need to commit our files This cmd it does not return anything but we can run git status


Step 07 - status

It shows you what file needs to be added to the staging area or committed git status


Step 08 - remove

To remove files from the staging area: git rm --cached index.html


Step 09 - commit

Commit files git commit -m 'Initial Project'

Bear in mind that after commiting you still need to push it to GitHub


Step 10 - .ignore

Create .gitignore touch .gitignore - This is also a hidde file

In the .gitignore add the file you want to ignore. For example:

index.html

log.txt

Or if if yoy want to ignore the whole directory /src

Bear in mind each file or folder to ignore goes in one line


Step 11 - remote - GitHub account

Go to your GitHub account https://github.com/getonfocus

Create a new repository and call it HeroicFeatures

Add this repository we just created as a remote repository - git remote add origin https://github.com/getonfocus/HeroicFeatures.git

Check the remote connection git remote or git remote -v

Remote is a way of telling git that this is a remote place where I want to push code up to or retrieve code from

origin is like a variable name which will hold the repository url. For the remote repository We can think as origin as the repositiory url. You could name whatever you want but we normally keep this one


Step 12 - push

git push -u origin master - Pushes to master branch

git push -u origin anotherbranch - Pushes to master anotherbranch

error - remote: Permission to getonfocus/HeroicFeatures.git denied to fabiosoli.

To fix the issue I had to: "Updating credentials from the OSX Keychain"

https://docs.github.com/en/github/using-git/updating-credentials-from-the-osx-keychain

Run the cmd again: git push -u origin master


Step 13 - branch

The main branch created by default is called master. If I run the cmd git branch, it will list all the branches in my project and the "star" at the beginning will highlight the one I am currently using.

To create a new branch I can run the cmd git branch <dd-mm-yyyy-hh-mm> I nornally use day-month-year-hour-minute

To navigate to the branch git checkout dd-mm-yyyy-hh-mm

Branchs are like folder structure. When you create a branch you still need to add files to your branch

git branch is not showing all the branches

Remember to run the cmd git fetch --all to get all branches from the repo

To see all the branches git branch -a


Step 14 - merge

git merge dd-mm-yyyy-hh-mm


Step 15 - gitconfig upstream

git branch --set-upstream master origin/master


[branch "master"]
remote = origin
merge = refs/heads/master

Now whenever you say git pull it will fetch from origin master.


Step 16 - clone

git clone "GITURL"

Step 17 - pull

Let's say some contributor commited some changes to you GitHub project and you need to get these changes to your local project

git pull origin master - To get the latest files update

git pull - To get the latest files update

git pull origin master will pull changes from the origin remote, master branch and merge them to the local checked-out branch.

where as git pull will fetch new commits from all tracked branches from the default remote (origin). You can also configure default remote and branch name in gitconfig file. https://stackoverflow.com/questions/23953125/difference-between-git-pull-and-git-pull-origin-master#:~:text=git%20pull%20origin%20master%20will,the%20default%20remote(origin).


Step 18 - checkout "time machine"

git checkout <commit-hash> - To get the latest files update


Step 19 - log

To get a history of commits we can use the cmd git log

git log will list all the changes in the current branch


Step 20 - fetch

To pull all additional branches. Fetch all branches from all remotes like this:

git fetch --all or git fetch <branch Name>

After running git fetch --all if you run git branch -a. It will list all the branches

Things to bear in mind

When you make changes to your project you need to take two steps: step 01: add and step 02: commit

Step 01: git add .

Step 02: git commit -m "Add all default files"


Add collaborators to my GitHub project

Go to your GitHub account > settings > collaborator


Fabio Rocha account

Step 01 - clone

git clone https://github.com/getonfocus/HeroicFeatures.git


Step 02 - clone

Create a new branch git branch 03-08-2020-18-20

Make some changes in the index.html file and


Step 03 - checkout

Move to the newly branch created git checkout 03-08-2020-18-20


Step 04 - add

Add to staging area git add .


Step 05 - commit

Commit files git commit -m "change title index.html"


Step 06 - push

git push -u origin 03-08-2020-18-20


usr: getonfocus
pwd: xxxxxxxxxxxxxx"^


Get on Focus account

Step 01 - fetch

To pull all additional branches. Fetch all branches from all remotes like this:

git fetch --all or git fetch <branch Name>


Step 02 - branch -a

List all branches git branch -a


Step 03 - merge

git merge 03-08-2020-18-20

git merge origin/03-08-2020-18-20


How do I know what GitHub account my local git is configured?

https://docs.github.com/en/github/using-git/updating-credentials-from-the-osx-keychain