Git and Github
- Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
- Github is a famous online version control platform made over git and currently owned by Microsoft.
What is version control or source control?
Version control, also known as source control, is the practice of tracking and managing changes to software code. It is used to maintain project in a remote location so people working on the project can come together in a common understanding while updating or developing features for the software.
Basic git example
Must have
- git installed on your machine, downloading git will give you a software called GitBash which is a kind of terminal. Linux user do not require git installation.
- Github account. Make a Github account and remember the username, email and password.
Example
- On your desktop folder, create a folder called
myproject
- Inside
myproject
make a filecode.txt
- Write "hello git!" inside the
code.txt
and save it and close the file. - Now open git bash inside myproject folder (do right click and Git Bash here).
- In the terminal do the following serially:
- git init
- git add .
- git commit -m "init"
- If the terminal shows something like
git config --global user.name ...
follow these steps else skip this step and move to next- git config --global user.email "your-github-email@gmail.com"
- git config --global user.name "Your Name"
- Adding git config is done only for the first time you've installed git on your machine
- After your ADD and COMMIT successfully
- go to github.com and login
- create a new public repository (uncheck create readme.md if it is checked)
- after creating a new repository you will see a lot of instructions on how to push the code to Github.
- find a line somewhere mentioned as
git remote add origin {url}
, COPY this. - paste this to your Git Bash terminal and press enter.
- this will now link your git project on your machine to the git project on Github, this basically says that when you PUSH on git bash terminal, the content where you did
git init
in this case insidemyproject
folder will now go and update on your Github repository that you created few minutes ago. - After adding remote to origin, do
git push -u origin master.
- After you have successfully pushed your BRANCH
master
to Github, you can now refresh Github and see the update with commit message on the top. - In this way you've successfully pushed your local file
code.txt
to your Github repository.
Branching
As we now know how to push a project folder contents to our own Github repository, now lets see branching. Branching is a very important part of version controlling where we make multiple branches of our master branch and add/remove/update the contents.
Example
- In your Git Bash inside the
myproject
folder, dogit branch
, this will show you all the branches that we currently have ON our local machine. - Currently you will only see
master
or sometimesmain
as by default git when initializing will create only master/main branch. - The content of the
code.txt
currently has "hello git!" in this master branch. - do
git branch dev
, this will createdev
branch which is an exact copy ofmaster
branch. - do
git checkout dev
, with this we are now insidedev
branch and every change we do now will only be indev
branch.code.txt
file inmaster
branch will NOT have any change. - change "hello git!" to "hello Github dev branch!", save the file. do not forget to SAVE!.
- every code change you do must be saved before ADDING and PUSHING.
- now do,
git add .
, this is meant to add all the new changes you've done insidemyproject
folder. - now do,
git commit -m "new changes on dev"
, which is an informative message to let you/others know what change have you done in this commit. - now do
git push origin dev
, remember the 1st time we didgit push -u origin master
?-u
in only required for the very first push. - check your
dev
branch in Github, the filecode.txt
will now have a different content than what we have onmaster branch
. - you can keep on editing->saving->adding->pushing on the
dev
branch or creating more branches formdev
ormaster
with different contents as you wish.
Sharing
- You can share your Github repo to your friend, ask him to
git clone {your project git}
. - Initially the clone will ONLY FETCH the first branch that we pushed. In this case
master
. - To get branches other than
master
, your friend must dogit fetch origin dev
. This will copy yourdev
branch from your repo to your friend's machine.
Assignment 01
Create a new project with a
details.json
file in a folder with following content and push it to a NEW git repository named as mad-assignment-01{ "name":"Your name", "class":"Your class", "Roll":"Your role", "Contact":"Your contact" }
Remember that the repository should be PUBLIC so I can evaluate your task, and give you a grade! Best of luck~