Setting up GIT
Just imagine how you pack a Lunch Box. Three basic steps that you do is:
- “Add” food to the Lunch Box.
- “Push” the Lid of the Box.
- “Commit” / Give the Box to the person.
The above three activities summarizes the way GIT works. Firstly you add some contents or make some change to your file, which will remain local ( inside the lunch box) next you push the changes from your workspace to the staging(closing the lid symbolizing the box is ready to be taken) and finally you commit the changes to the repository ( Handing over the box to the kid / person who will carry the Lunch ).
Common guys, let’s setup Git in our lab and see its working realtime….
Step 1: Login to any ubuntu machine , run the command : apt-get install git
If you are not the root user, run with sudo command and enter you ldap password.
Step 2: You have to configure your user name and email id which you will be using to work with GIT repository so that the changes can be distinguished.
>git config — global user.name “Siddarth.M”
>git config — global user.email “abcd@gmail.com”
Step 3: Create a Projects directory and cd to it.
>mkdir projects
>cd projects
Step 4: Run the command : gwsh — init
Initialized empty Git repository in /home/siddarth/projects/.git/
This will initialize the Projects Folder and create a hidden file called git.[hidden files are identifies as .filename eg: .git]
>ls -altr
total 12
drwx — — — 29 user1 staff 4096 May 25 18:49 ..
drwxr-xr-x 3 user1 staff 4096 May 25 18:49 .
drwxr-xr-x 7 user1 staff 4096 May 25 18:49 .git
Step 5: Just create some files inside projects folder.
vi app1.py [ I am adding some print statements inside this file ]
vi app2.py [ Just some example pythons script ]
Step 6:Observe the status of git before we do anything.
>git status
git status
On branch master
Initial commit
Untracked files:
(use “git add <file>…” to include in what will be committed)
app1.py
app2.py
nothing added to commit but untracked files present (use “git add” to track)
We see that git is asking us to add the files first. Same as adding food in the lunch box. Let’s go and add the files one by one.
Step 7: Let’s use the “add” command to add the files to git.
git add app1.py
git add app2.py
Check the status now: >git status
On branch master
Initial commit
Changes to be committed:
(use “git rm — cached <file>…” to unstage)
new file: app1.py
new file: app2.py
Step 8: You see, in the above step git says to commit the changes so that it can track in the repository. Let us use the ‘commit’ command now with a message.
>git commit -m “Changes for Daily Release”
[master (root-commit) 457f01b] Changes for Daily Release
2 files changed, 2 insertions(+)
create mode 100644 app1.py
create mode 100644 app2.py
Step 9: To see the commits made use the command: >git log
commit 457f01be64e10f0b747de98a83a622bcabee779b
Author: Siddarth.M <abcd@gmail.com>
Date: Tue May 25 19:16:04 2021 +0530
Changes for Daily Release
Step 10: If you would want to see the changes made in the files, use the diff command. git diff. Example I added a new line in app1.py.
>git diff
diff — git a/app1.py b/app1.py
index 7019e47..ea36d3b 100644
— — a/app1.py
+++ b/app1.py
@@ -1 +1,2 @@
print(“Hello! Welcome to my App!”)
+print(“Hey! This is second line added!”)
So when you add a file, it moves from your working copy to staging area. And when you commit your files, they move from staging area to local repository and finally when you do a push, the files move from local repository to remote repository ( cloud )
Step 11: Next we have to push this changes to remote repository.
For those who dont have a github account, you can signup and then create a new repository and copy the link from there.
eg: https://github.com/sidthedev/newrepository.git
On your ubuntu terminal, create a label to this remote repository as below.I have given my label ad “origin”, which will represent the above URL.
>git remote add origin https://github.com/sidthedev/newrepository.git
Next simply push to Master ( main branch ):
>git push origin master
Username for ‘https://github.com': sidthedev
Password for ‘https://sidthedev@github.com':
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 629 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To https://github.com/sidthedev/newrepository.git
- [new branch] master -> master
Step 12:
Local Git Repository < = = = = = = = = > Online Git Repository
git clone : entire repository will get cloned / creating duplicate repo either locally or remote.
git fetch : Only the metadata of the changes will get download not the files
>git fetch origin
[ Only the metadata will get fetched not the files ]
git pull: Will pull the changes
> git pull origin master
[ Entire repro will get copied locally ]
git push : To push the changes to Remote Repository. You have to first add the files to staging by running git add . ( this will add all the files ) and then you can run git push <label> master.
example: > git add .
> git push origin master
Now refresh the online github repository, you will start seeing the files.
Note: Always do a git add so that the files can be pushed to the repository!