Git & GitHub Step-by-Step: Part 1
Why is git around?
Git is a version control system. It helps to keep track of the files and changes in files.
It allows:
-> Easy recover of files
-> Who introduced issue and when
-> To rollback to previous working state
Will local version control system will not work? (Local VCS)
It will allow for tracking of the file as well as rollback but, it will not help if in case any hardware damage such as flood or anything affects your computer.
Will centralized version control system help? (Centralized VCS)
It allows multiple users to work on same file giving track to all other working members, no history is tracked, since it will be of no use in case server get damaged.
Will distributed version control system help? (Distributed VCS)
Allows for complete backup to every user of their project/work. It will allow the backup even if the server get damaged.
Git is a distributed VCS.
Git features:
-> It snapshots the difference in your project
-> Allows the record of history
-> Almost every operation is local then you can push it to repository
-> Git has integrity, no one can damage your original pushed file
-> Allows checksum, as it allow to track if anyone manipulated the file while you downloaded it
-> Git generally only adds data
How to download Git?
To download just visit the website : https://git-scm.com/downloads and finish as you wish.
Check if it is installed correctly using command: (on git bash terminal)
git –version [note: — is equivalent to -]
Shortcut to open git bash in any folder?
Shift + right click (in that folder)
How to tell git (via git bash) that who we are?
Command 1: git config –global user.name “Rememberful”
Command 2: git config –global user.email “adii.utsav@gmail.com”
Command 3: git config –list
Command 1 will help to set the global username and command 2 will help to set the email address of that username. Also, command 3 will let you recheck what you had entered as username and email address.
Now how to specifically check the name and email address of it?
Command: git config user.name (for user name recheck)
Command: git config user.email
Git: Three structure architecture
First let’s understand few terms:
Commit — it means to take snap of the current state of the project.
So, why three stage of architecture is there? For a reason if out of 5 files 3 get changed by you, but the 3rd change is not working in your code, then git allows you to save only those changes which will work, as it allows you to change. The process of pushing the updated code is falls under staging area.
So, what’s this three-stage architecture?
It consists of three parts:
i. Working directory: The directory which shows the files which we have in a folder.
ii. Staging area: The set of files which go into next snapshot.
iii. Git repository: The hidden file which helps to get the proper versions of required file.
Since, we can skip staging areas by commands, although don’t skipping it is a good practice.
How to check if folder is a git repository?
In git bash command:
git status
How to make any folder as git repository?
In git bash command:
git init
Then check status of it using:
git status
How to put all files of folder in git staging area?
In git bash command:
git add — a
Then check status of it using:
git status
How to commit the current stage with some comments?
To commit with one comment:
git commit -m “Initial commit”
Then check status of it using:
git status
How to know which commit I did and when?
To get that in git bash:
git log
If suppose we changed something in first.txt then we saved it, then we can check the status of it using:
git status
Suppose we want to change something in ppt as well but don’t want to commit, then how can we specifically commit first.txt?
First make the change in ppt and save it, then use the following to check status:
git status
Then to specifically add First.txt.txt
git add First.txt.txt
The above ss indicates that the first.ppt.pptx is not committed yet it’s just in staging area, while first.txt.txt is committed.
To end the git repository (to delete .git file from that folder):
In git bash type:
rm -rf .git
Then check git status using:
git status
How to clone a git repository from github?
Open the browser and go to the website of git hub which have that repository. Copy url of it to clipboard https://github.com/tensorflow/tensorflow.git . Now, go to git bash, and type the command:
git clone https://github.com/tensorflow/tensorflow.git
It will create a directory inside that Git tuts in which we had opened the git bash. That folder will contain all the files of the copied github link.
Can we make some changes in the files of tensorflow?
YES! Now, we can make some change in the Readme file over there, then we can commit the changes, using the following commands:
To get into the tensorflow directory:
pwd
ls
cd tensorflow
Now, to make changes added:
git add –-a
This will be followed then by adding some comment as well as checking the git status
We can also see who had made changes in the tensorflow till date, using the command:
git log
While to get out of that, we can write q + enter.
File status life cycle:
We start making git repo using
git init
Now, it will create an empty git repo, which can then be called untracked. Suppose, we made files in it, then in order to make it in staging area we use the command:
git add –-a
So, till git add –a all files in the repo were untracked, while after it, they became unmodified.
Now, if we make changes in any of the files which are in unmodified state, then those files be in sate of modified as well as stage.
Then we stage the file to make It staged.
But how can we do so?
The above commands will help to make a new get repo inside the Git tuts folder.
Now, check the status of the git repo, we will see that “Untracked files” are written there, so we are at the extreme left stage of the above flow chart.
To track the files we will use the command:
git add — — a
This will make the files of the folder to be in state of unmodified.
Then check the status of the project using the command:
git status
Now, we actually made it enter in staging area, but we will call this here as unmodified state.
Now, let’s change something in the text document. And save it.
Now, check status of it using:
git status
We will observe that the text file is now in both state: modified and staging. This is because it was earlier in staging area (which is by default planned to be committed) but we modified it before committing. So, now if we will try to commit them, we will find that git will commit only those files which be in staging area, not the one which be modified.
So, now we can make the edited file to be in staging area as well using the following command:
git add first.txt.txt
git status (to check the status)
Ignoring files in Git:
Ignoring files in git means we don’t want it to save in the repo (e.g., log files etc) then we use to ignore that file.
Let’s create a file named error.log in the git tuts using the ‘touch’ command.
To make it ignore we will create one more file, named “.gitignore” using the command:
touch .gitignore
So, in case we want to ignore the error.log file, we will open the “.gitignore” file and write full name as well as the extension of the file and save it.
Now, when we check the status of the repo, using the command:
git status
We will find that the list of untracked files get changed, and error.log file will not be present over there. Before and After status of git status:
Basically error.log file is ignored.
Now, to stage all the files, command:
Get Aditya Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
git add -- a
So, now even if we make some changes in error.log, git status will say no changes.
What if we have enormous number of files to be ignored(say log files)?
To do so we have to edit the ‘.gitignore’ and write the following “*.log” instead of error.log
Before:
Now, changing .gitignore:
After:
Can we also ignore the folder inside our git repo?
Yes, lets first create a folder named “dir”. Then put some log files inside it to test.
Directory existence:
Files inside the ‘dir’ directory.
Now, check status of the repo:
Clearly, the dir directory is in untracked list, but we in case want to ignore it, then in .gitignore we will write the folder name as well. We should keep in mind that all folders or subfodlers which will be named as ‘dir’ will be ignored.
What if we want only the outermost dir to be ignored?
We can manage to recognize the 2nd dir directory if we write ‘/dir/’ inside the .gitignore file.
So, if 2nd dir is in static folder with some content then, with ‘dir/’ we will find that it get ignored.
Now, let’s change the .gitignore file and recheck the git status.
We can also give the whole path to specify which file we want to ignore specifically.
So what can we say about this below screenshot?
This ss indicates that toto.txt is been modified, as well as earlier placed to add in staging area. Since, before commit we again made changes to the toto.txt it should be taken care of and maintained separately as well.
How to get difference as noticeable?
We can use command ‘git diff’ to get the difference between the files at staging area and modified area.
So, basically it compares staging area with working directory.
The above ss says that ‘-hey’ which was earlier(staging area) in the toto.txt is replaced with ‘+hey hey hey’ (working directory).
So what if we first use ‘git add — a’ then use the ‘git diff’ for above case?
When we use git add — — a then the staging and working directory will become same thus nothing will be shown by git diff.
So can we compare staging area with the old commit?
Yes, we can do so easily, using the command:
git diff –-staged
The above ss indicates that the error.log is removed from .gitignore and *.log as well as dir/ is added to it. While in toto.txt we can find that ‘hey hey hey’ is added.
Thus, it compares your previous commit with the current staging area.
So, what’s the status of the repo now? The status of the repo is to show that the edited text files are yet to be committed.
Can we skip the staging area?
Yes, we can skip the staging area.
First, let’s create one more file in the git tuts folder as second.txt. as well as modify the first.txt
Now, check if changes are recognized or not:
Now, in order to commit the staging file only, not the untracked file, then we use the command:
git commit -a -m “direct commit”
Then checked the status of it using:
git status
Thus, above ss says that second.txt is still untracked and first.txt.txt is committed.
We, can manually add the second.txt to repo as:
git add second.txt
We can then use commit it, and then check status.
Can we rename the files/folder using git bash?
Yes, we can do so.
First see the manual way, right click the file and rename it as we always do. Say, second.txt is renamed to third.txt.
So, as per the git the second.txt is now deleted and third.txt which is renamed earlier is now in untracked. Since, only name is changed as shown.
But when we just add the third.txt to staging area we will find that git recognize it as renaming.
Now, how to rename via git bash?
Thus, it get renamed as well as staged as shown above. We can now commit changes with description.
Can we delete the third.txt (or any file) from the repo?
Yes we can, first manually deleting.
Will git recognize this deletion?
Yes, as:
But, if we do ctrl+z in git tuts folder to undo the deletion then, third.txt will come back, as well as git bash will also don’t consider it as deleted anymore.
Now, let’s remove it by git bash:
git rm third.txt
So, basically the removal via git bash allows to remove the file as well as creating the staging again which was not there in manual way.
Suppose we have a file named first.pptx which is to be ignored using .gitignore, then what if after doing so, we edited the first.pptx? Will git still ignore it?
No, we need to explicitly give the instruction to ignore it.
But before it, lets edit the .gitignore as:
Now, what will be the response of git status? It will say that .gitignore has been modified.
But, what if we again made any changes to that pptx file?
The git still recognized the changes we made, although we just earlier marked it as to be ignored. It means it hadn’t explicitly ignored it.
So, how to make it ignore like so?
Command:
git rm — -cached first.ppt.pptx
It’s written that first.ppt.pptx is deleted although it’s not deleted.
So, any changes now made in the pptx file is not reflected in git bash.
How to see who committed what in any online git repo?
We can do it by using the following steps:
1. First remove that repo
Using: rm rf .git
2. Then import the library from the git hub
Using: git clone https://github.com/pandas-dev/pandas.git mypanda
3. Go to the mypanda folder and open git bash using shift+right click and seleting it.
4. To see who committed:
git log
5. To check who committed what
git log -p
6. In order to see specific number of commit we use: (for 3 commits only)
git log -p -3
7. To get the short summary of the changes made by that person:
git log –stat
8. To print all commits in one line
git log — -pretty=one line
9. To print the commit in short
git log — -pretty=short
10. To get the name of author(who created) as well as name of commit (one who committed)
git log — -pretty=full
11. To see the changes or commits made in last 2 days (for months/years/weeks replace .days with it)
git log — -since=2.days
12. To get abbreviated hash commit and author name we use
git log –pretty=format:”%h — %an”
13. If we want to merge the changes made by us to any other commit made by people
First make any change in any file of it
Now, to merge, the editor will be opened and you can make changes int the comment section made by others. Using:
git commit –amend
Now, checking the changes, using:
git log -p -1
Contact Me:
📧 Email: adii.utsav@gmail.com
🔗 LinkedIn: https://www.linkedin.com/in/aditya-kumar-3241b6286/
💻 GitHub: https://github.com/Rememberful








