What is git
Version history for code
Allow to save past version of the code and able to view
Project setup
We use powershell(command line) to use git
Command line is where we give instructions/commands for computers to follow
PS C:\Users\TSHEPO\Desktop\git-tutorial> ls
ls- list the files and folders in the current folder
all the commands run in a specific folder
when running the command line the commands are running in a special folder called
$HOME folder
in order to use git we must change the command to run in git-tutorial(folder desktop)
PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial
o cd- change directory(directory = folder)
o using command ls – src and config.js appear
creating a version
creating the 1st version of the code
PS C:\Users\TSHEPO\Desktop\git-tutorial> git init
git init- set up git inside the folder and now git is tracking all of the file in the folder
before changes
PS C:\Users\TSHEPO\Desktop\git-tutorial> git status
git status- tells us what changes are made since the previous version
o tells us that src and config.js are not tracked in the version history
PS C:\Users\TSHEPO\Desktop\git-tutorial> git add config.js
config.js we only be added to the next version and source folder will be left out
if we want to add all the files including sun folders
PS C:\Users\TSHEPO\Desktop\git-tutorial> git add .
o Dot(.) Represent the current folder the command line is running in
PS C:\Users\TSHEPO\Desktop\git-tutorial> git commit –m “Version 1”
git commit –m(commit = version) We use it to create a new version
-m – attach a message to these commit to descript what you changed
If an error were to occur
We use
It’s important to have your name and email so that if you were to add a version that
crashes the system. Your team mates will know who broke it
To take a look at the version history
o PS C:\Users\TSHEPO\Desktop\git-tutorial> git log
If you want a file to be in the previous commit(version)
o First add all the git add . folders then run git commit –m “Version 1” --amend
--Amend - mean insead of creating a new commit, everything is saved
in the previous commit
Visualizing git + git fundamental
On a code editor(visual studio) git add . is the plus sign button on the source control
When we ran git add . the changes were saved in Staged Changes( staging area)
o Staging area - Changes here will go to the next version
o Under the staging area is all the changes we are working, we haven’t used git
add . (Working Area)
Taking changes out the staged area press the – sign button
The git command is PS C:\Users\TSHEPO\Desktop\git-tutorial> git reset
o > git reset config.js - It will only take config.js out of the staged area
o > git reset src - Take out all the file in the folder including subfolders
o > git reset . - take out all the changes out the staged area in the current folder
To reset the changes in the working area The git command is PS C:\Users\TSHEPO\
Desktop\git-tutorial > git checkout –
o git checkout -- change.js – undo all the changes
o git checkout -- src - undo all the file in the folder including subfolders
o git checkout -- . - undo all the changes in the current folder
Completing the version history
on git you must create your own version unlike on google
because we want to make sure the code is good to go before we put it in the version
viewing previous version of the code
C:\Users\TSHEPO\Desktop\git-tutorial > git checkout c93462b02ee8b5399f7b19185e8b234b93d
the big long string is called commit hash is the id of the commit
o back to version 2
C:\Users\TSHEPO\Desktop\git-tutorial> git log --all
shows all the previous versions of the code
o (HEAD) – means the current version
Branches
Series of commits, 1 branch of commit
When you want to ignore a file from going to the next version
Place every file you want to get ignored in .getignore
To delete git from your files
C:\Users\TSHEPO\Desktop\git-tutorial> rm –rf .git
All the data in git including commits and changes is saved in the folder .git and rm –rf
basically deletes anything in a specified folder
GitHub
Git repository – folder that is being tracked by git
GitHub specifically designed for git repositories
Local repository – file in our computer
Remote repository – file in github
1. Go to your profile at github then press repository furthermore press new then pick the folder
you want to add
2. Upload local repository to remote repository on powershell we –
PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial
3. Create a link between the local and remote repository
PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git remote add origin
https://github.com/A-PULE/git-tutorial.git
o Origin – is the nickname for the file
o The link is a HTTPS link from github
To check if you have created the link between the 2
o PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git remote
Shows the nickname
o PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git remote –v
Shows the link
Upload to github – (push) & download from github - (fetch)
If you want to remove a link to the repository
o PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git remove origin
4. Configure git with our github username
PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git push origin master
Master - is from the branch
Powershell will ask for a password but I setup a token key to log in
Sync change from computer to github
Origin/master is a remote tracking branch, tells us how the master branch looks like in origin
When we add a new commit “Version 11”
(Master) branch is ahead of the (origin/master) which is github since we haven’t
updated in github
How we update from local to remote repository
We run PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git push origin master
o Then refresh the page
Origin/master is up to date with the master
Shortcut PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git push origin master --set-upstream
Next time you run PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git push because it
has memorized origin and master
If changes are not in a commit git push will not change anything
Instead of creating a new commit we are going to overwrite the previous commit
PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/ git commit --amend –m “Version 12”
o The is 2 branches because github still has a commit that we overwritten to
version 12
o Need to force a commit using PS C:\Users\TSHEPO> cd ~/Desktop/git-tutorial/
git push origin master -f
Sync change from github to computer
Copy the https download/clone link then run
PS C:\Users\TSHEPO> cd ~/Desktop/ cd ~/Desktop
After that you also run PS C:\Users\TSHEPO\Desktop> git clone https://github.com/A-
PULE/git-tutorial.git new-git-tutorial
o The link is a https download link and new-git-tutorial is a new file name going to
be created in the desktop(folder name)
Go to the new repository we create a new commit and push it to github
PS C:\Users\TSHEPO> cd ~/Desktop/ cd ~/Desktop/new-git-tutorial command line must
run to the new repository
Then we run PS C:\Users\TSHEPO> cd ~/Desktop/ cd ~/Desktop/new-git-tutorial/ git add
.
Commit a new version PS C:\Users\TSHEPO> cd ~/Desktop/ cd ~/Desktop/new-git-
tutorial/ git commit –m “Version 13”
We then push it to github PS C:\Users\TSHEPO> cd ~/Desktop/ cd ~/Desktop/new-git-
tutorial/ git push origin master
PS C:\Users\TSHEPO> cd ~/Desktop/ cd ~/Desktop/new-git-tutorial/git fetch Track all
the remote branches to the current state in github, since we haven’t updated the
remote tracking branches
Sync the new commit back to the local repository
o PS C:\Users\TSHEPO> cd ~/Desktop/ cd ~/Desktop/new-git-tutorial/git pull
origin master
Scenario
We have an existing project, we want to intergrate git and upload to github (git was never
involved in this project)
1. Git init(turn it into a git repository)
2. Create a commit
3. Create a github repository
4. Git push
We are starting a new project, we want the project to be on github
1. Create some code
2. Git init
3. Create a commit
4. Create a github repository
5. Git push
- What we have been doing
Or….
1. Create a repository on github first
2. Git clone
3. Create code
- Helps us save some setup steps, no need to run git init
We joined a new team, their project is already on github. We want to download the project and
contribute to the code
1. Git clone the team repository
2. Work with the code as normal
3. Create a commit
4. Git push