Install then open GIT BASH as an administrator
To make a folder on the desktop
$cd /C (will navigate to C drive root if needed)
$cd desktop
$mkdir gitTest
to delete this directory from the desktop type $rm -d gitTest
Now open this directory
$cd gitTest
Now lets create a couple of files in this directory
$touch index.php james.css
If on my desktop, I now deleted these files, they would be gone forever (forget I have a recycle bin for a moment in windows). If however I use GIT, they will not be gone. Let’s see how this works.
To use git, you first have to initialize git in your folder. Since we’ve already cd’d into the folder (above), we type
$git init
This puts a .git tracking file in the project folder.
We can view what branch we’re currently in by using
$git status
Since we’ve just initialized git, we’ll be on the master branch. We can rename this branch if we want using the following to rename the ‘master’ branch to ‘development’
$git branch -m development
If we now check the $git status again, we see
For the moment we’ll just rename it back to ‘master’
$git branch -m master
So we’re now ready to use git. We can save all of our progress on the files in our project folder using
$git add .
We could alternatively save an individual file to git, e.g.
$git add index.php
Note: if we just added index.php and deleted the css file in the project folder, the css file will be lost forever because we only added the index.php file.
So in this example, we will use the following to save everything.
$git add .
So far, by using ‘add’, we’re added these files to the ‘staging’ area. However, to actually commit these files to memory, we need to use
$git commit -m ‘My 1st commit: adding index.php and james.css’
In the above -m is used followed by a message describing the changes/the save
Commit is like setting a restore point. We can go back here in the future!
These files are now saved in GIT.
We can demonstrate this by deleting one or both of the files.
$rm index.php
The ls command in the terminal shows us what is now in our project folder
We now see only ‘james.css’ is present. Please note that the .git is still present but it’s hidden. We can see this in file explorer
Let’s now add another file to our project folder
$touch jamesSpecialJavascript.js
So now we’ve made some more changes to our project, so we can save it again
$git add .
$git commit -m ‘deleted index.php and added js file’
So we’ve saved our project twice now.
Note: $clear will clear the terminal screen
By typing the following, we can see the progress of or changes (each time we commit the files)
$git log
Note: each commit has it’s own special code to identify it so we can go back to any commit we want by using this code
$git checkout f1fa209f332e43ee97728a9ed50cfc594500908e
Note the above, now that we’re ‘checking out’ a previous commit with ‘checkout’ we’re now NOT in the master branch any more. If we now type
$git branch
We get
We can see that we detached from the master branch.
We do however have access to the original files.
$ls
To now link this to a repository on github, create a repository and get the link as shown in the below image
So in the terminal now type
$git remote add origin git@github.com:jamesfroggatt/gitTest.git
We now want to push out commit to GITHUB website
$git push -u origin master
push = push your changes to the remote server
origin = remote Server origin
master = Master branch
We can now see all of our commits in GITHUB
To make a new branch
$git checkout -b myNewBranch
Let’s add a new file to this branch
$touch myNewFile.html
Now add this file and commit
$git add .
$git commit -m ‘add new html file’
To now add myNewBranch back to the original branch we type
$git push origin myNewBranch
myNewBranch will now appear on github
It’s very important for the code on your local computer to be synchronized with github. People can ‘pull’ data from github and ultimately the data can be modified there by other people. To do this type
$git pull origin master
…however this happens
To merge the myNewBranch with the master, we need to first be in the master branch
$git checkout master
the merge the myNewBranch
$git merge myNewBranch
Now we can pull as before and it will work
$git pull origin master
Branches
To show all the branches for the project
$git branch
To switch to a different (but existing branch)
$git checkout myNewBranch
To create a new branch (‘theLatestBranch’ is the new name of the new branch)
$git checkout -b theLatestBranch
If we go to the master branch with
$git checkout master
We can then attempt merge another branch into the master branch using
$git merge theLatestBranch